Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create hash map with entries from .properties file

I want to create a Hash Map with the entries from .properties file. 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.

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

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

like image 534
user5200742 Avatar asked Oct 19 '22 05:10

user5200742


1 Answers

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);
    }
}
like image 165
Vishal Jagtap Avatar answered Oct 27 '22 10:10

Vishal Jagtap