简体   繁体   English

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

[英]How to create hash map with entries from .properties file

I want to create a Hash Map with the entries from .properties file. 我想用.properties文件中的条目创建一个哈希映射。 My property file looks like: 我的属性文件如下:

##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 到一个hashmap

2nd row it should enter 第二排应该进入

key1 = B1
key2 = B2
key3 = B3

into an hashmap and so on.... 到一个hashmap等等....

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 它应该为每次迭代将键和值添加到相同的哈希映射中,并且应该清除哈希映射中的先前条目

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);
    }
}

Properties files are commonly read using java.util.Properties . 通常使用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. 这意味着您需要手动读取文件(可能是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每个键只能存储一个值,因此您需要另一个数据结构来保存您可能需要的内容。

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. 例如,如果您尝试检索'key1'的值,它将始终返回'D1'。 You could try naming your keys as key1AA, key2AA, key3AA, key1BB, key2BB and so on. 您可以尝试将密钥命名为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();
        }


    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM