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
-
What is the intended output?Aaron C– Aaron C2014年10月24日 16:40:55 +00:00Commented Oct 24, 2014 at 16:40
-
Key = 006247241 Values = [#, 0590040100000124,0590020100000125,0590020450000124, 0590040100000129 Key = 006247242 Values = [#, 0590040100000300,0590020100000325,0590020450000312, 0590040100000229Wasim Wani– Wasim Wani2014年10月24日 16:43:42 +00:00Commented Oct 24, 2014 at 16:43
-
Each key has its own list. So, every time you put a key in the map, you also need to create the new list that goes with the key.JB Nizet– JB Nizet2014年10月24日 16:53:21 +00:00Commented Oct 24, 2014 at 16:53
-
@JBNizet Thats what i am looking for but can't figure how to do that in the above caseWasim Wani– Wasim Wani2014年10月24日 16:57:09 +00:00Commented Oct 24, 2014 at 16:57
-
Are the keys supposed to have the same values for their list or are they separate?Aaron C– Aaron C2014年10月24日 16:58:52 +00:00Commented Oct 24, 2014 at 16:58
2 Answers 2
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);
-
@aaron c Thats it...i was just not adding new Aaraylist ..you made my dayWasim Wani– Wasim Wani2014年10月24日 17:15:49 +00:00Commented Oct 24, 2014 at 17:15
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());
}
}
-
Thats it...i was just not adding new Aaraylist ..you made my dayWasim Wani– Wasim Wani2014年10月24日 17:16:25 +00:00Commented Oct 24, 2014 at 17:16