How to create a hashmap with entries from a .properties file

I want to create a Hashmap with entries from a .properties file. My properties file looks like this:

##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 maintain AA, BB, CC, DD on excel sheet.

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

      

I want to iterate over all the lines and when it is on the 1st line it should type

key1 = A1
key2 = A2
key3 = A3

      

to hashmap

The second line should enter

key1 = B1
key2 = B2
key3 = B3

      

into hashmap, etc ...

It has to add keys and values ​​to the same hashmap for every iteration and clear the previous entries from the hashmap

+3


source to share


3 answers


You can try something like: -



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 first character and values first character are same.
        map.put(key, value);
    }
}

      

+1


source


Property files are usually read with 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 that you will need to read the file manually (probably BufferedReader

), parse each line and build the map you want.



Removing the hashmap between iterations doesn't make a lot of sense, though, unless you do a new map on each iteration or do nothing with the result. Again, a HashMap can only store one value per key, so you'll need a different data structure to store what you think you might need.

0


source


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

Remember that in general the "key" should not be repeated in the properties file. If it repeats, it will always fetch the last value. For example, if you try to get a value for "key1", it will always return "D1". You can try naming your keys as key1AA, key2AA, key3AA, key1BB, key2BB, etc.

Also, if you try to recover "key3", you will get the full value "D3, D4".

Here's 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();
        }


    }

}

      

0


source







All Articles