Wasim Wani
Wasim Wani

Reputation: 555

Adding new list dynamically to Map

I have an array like this 006247241 0590040100000124 0590020100000125 0590020450000124 006247242 0590040100000129 ...Now if the length of any value in the array is between 1 to 9 it become a key and the rest following it immediately become Values for which i use a Map. So in the above case 006247241 becomes a key and 0590040100000124 0590020100000125 0590020450000124 become values for that key and so on. My code is like this(All the above values are in an array cellValues[]).

Map<String, List<String>> map = new HashMap<String, List<String>>();
            List<String> list = new ArrayList<String>();

            String key="#";

            for(int k=0;k<cellValues.length;k++){
                if(cellValues[k]!=null){
                    if(cellValues[k].length()<=9 && cellValues[k].length()>0 ){
                        map.put(key,list);
                        //System.out.println("MAP:"+map);
                        key=cellValues[k];
                    }else{
                        list.add(cellValues[k]);
                    }
                }
            }

The problem i face here is how to add new list for each new key.The above code will just append all the values to each key in the array and the output would be like this

Key = 006247241 Values = [#, 0590040100000124,0590020100000125,0590020450000124, 0590040100000129

Key = 006247242 Values = [#, 0590040100000124,0590020100000125,0590020450000124, 0590040100000129

Upvotes: 0

Views: 3992

Answers (2)

GameDroids
GameDroids

Reputation: 5662

You are reusing the same list every time. If you want a new list for every key, you should create it everytime you put a new key,value pair into the map:

Map<String, List<String>> map = new HashMap<String, List<String>>();
String key="#";

for(String value : cellValues){
    if(value.length()<=9){
         // its a key
         key = value;  
         map.put(key, new ArrayList<String>());
    }else{
        // its a list value
        map.get(key).add(value);
    }
}

You wont need to declare a list anymore, since you are using a new list for every key and when adding to a list you get the reference to the list from the map by searching for it with the last key: map.get(key).add(value);

Upvotes: 2

Aaron C
Aaron C

Reputation: 343

I believe this is what you were after. If not, let me know.

Map<String, List<String>> map = new HashMap<String, List<String>>();

    String key="#";

    for(int k=0;k<cellValues.length;k++){
        if(cellValues[k]!=null){
            if(cellValues[k].length()<=9 && cellValues[k].length()>0 ){
                key=cellValues[k];
                map.put(key, new ArrayList<String>());
            }else{
                if(!map.get(key).contains("#")){
                    map.get(key).add("#");
                }
                map.get(key).add(cellValues[k]);
            }
        }
    }
    for(String k : map.keySet()){
        System.out.println("Key = " + k + " Values = " + map.get(k).toString());
    }
}

Upvotes: 1

Related Questions