java 如何使用 .properties 文件中的条目创建哈希映射

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31870806/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 19:16:41  来源:igfitidea点击:

How to create hash map with entries from .properties file

javaexcelhashmapproperties-file

提问by user5200742

I want to create a Hash Map with the entries from .properties file. My property file looks like:

我想用 .properties 文件中的条目创建一个哈希映射。我的属性文件如下所示:

##AA
key1 = A1
key2 = A2
key3 = A3
##BB
key1 = B1
key2 = B2
key3 = B3
##CC
key1 = C1
key2 = C2
key3 = C3, C4
##DD
key1 = D1
key2 = D2
key3 = D3, D4

I will be maintaining AA, BB, CC, DD in an excel sheet.

我将在 Excel 表中维护 AA、BB、CC、DD。

row1 = AA
row2 = BB
row3 = CC
row4 = DD

I want to iterate through all the rows and when it is in 1st row, it should enter

我想遍历所有行,当它在第一行时,它应该输入

key1 = A1
key2 = A2
key3 = A3

into an hashmap

成一个哈希图

2nd row it should enter

应该输入第 2 行

key1 = B1
key2 = B2
key3 = B3

into an hashmap and so on....

到一个哈希图等等......

It should add the keys and values into the same hash map for every iteration and should clear the previous entries from the hash map

它应该为每次迭代将键和值添加到相同的哈希映射中,并且应该从哈希映射中清除以前的条目

回答by Vishal Jagtap

You can try something like below:-

您可以尝试以下操作:-

Properties MyPropertyFile= new Properties();
FileInputStream ip = new FileInputStream(".properties file path");
MyPropertyFile.load(ip);

String row="AA"; //write logic to get row value from excel sheet and update in a variable.

HashMap<String, String> map=new HashMap<String, String>();
Set<Object> keys = MyPropertyFile.keySet();

for(Object k:keys){
    String key=(String) k;
    String value=MyPropertyFile.getProperty(key);

    if(row.charAt(0)==value.charAt(0))// check row's first character and values first character are same.
        map.put(key, value);
    }
}

回答by Dinesh Chitlangia

As far as I understand your question, you want to pick a set of key-value pairs from the properties file, based on the comments (##AA, ##BB etc) that you have in the properties file.

据我了解您的问题,您想根据属性文件中的注释(##AA、##BB 等)从属性文件中选择一组键值对。

Remember, in general, the 'key' should not be repeated in a property file. If it is repeated, then it will always fetch the last value. For example, if you try to retrieve value for 'key1', it will return you 'D1' always. You could try naming your keys as key1AA, key2AA, key3AA, key1BB, key2BB and so on.

请记住,一般情况下,不应在属性文件中重复“键”。如果重复,那么它将总是获取最后一个值。例如,如果您尝试检索“key1”的值,它将始终返回“D1”。您可以尝试将您的密钥命名为 key1AA、key2AA、key3AA、key1BB、key2BB 等。

Also, if you try to retrieve 'key3', you will get the complete value 'D3, D4'.

此外,如果您尝试检索“key3”,您将获得完整的值“D3、D4”。

Here is an example I tried with your properties file:

这是我尝试使用您的属性文件的示例:

package com;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.PropertyResourceBundle;
import java.util.ResourceBundle;

public class PropertiesToMap {

    public static void main(String[] args) {
        FileInputStream fis;
        try {
            fis = new FileInputStream("D://MyProps.properties");
            ResourceBundle resources = new PropertyResourceBundle(fis);
            Map<String,String> map = new HashMap<String,String>();

            //convert ResourceBundle to Map
            Enumeration<String> keys = resources.getKeys();
            while (keys.hasMoreElements()) {
                String key = keys.nextElement();
                map.put(key, resources.getString(key));            
            }
            //Now you can use the 'map' object as you wish.

        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }


    }

}

回答by Paul Bilnoski

Properties files are commonly read using java.util.Properties. However, since you have the same keys defined multiple times, only one of the values for each key will be available after processing the file. This means you will need to read the file manually (perhaps BufferedReader), parse each line, and build the map you want.

属性文件通常使用java.util.Properties. 但是,由于您多次定义了相同的键,因此在处理文件后,每个键的值中只有一个可用。这意味着您需要手动读取文件(可能BufferedReader),解析每一行,并构建您想要的地图。

Clearing the hashmap between iterations doesn't make much sense though unless you are making a new map each iteration or doing something with the result. Again, a HashMap can only store a single value per key, so you will need another data structure to hold what it seems like you might need.

在迭代之间清除哈希图没有多大意义,除非您每次迭代都制作一个新地图或对结果做一些事情。同样,HashMap 只能为每个键存储一个值,因此您将需要另一个数据结构来保存您可能需要的内容。