0

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

asked Oct 24, 2014 at 16:38
6
  • What is the intended output? Commented Oct 24, 2014 at 16:40
  • Key = 006247241 Values = [#, 0590040100000124,0590020100000125,0590020450000124, 0590040100000129 Key = 006247242 Values = [#, 0590040100000300,0590020100000325,0590020450000312, 0590040100000229 Commented 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. Commented Oct 24, 2014 at 16:53
  • @JBNizet Thats what i am looking for but can't figure how to do that in the above case Commented Oct 24, 2014 at 16:57
  • Are the keys supposed to have the same values for their list or are they separate? Commented Oct 24, 2014 at 16:58

2 Answers 2

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

answered Oct 24, 2014 at 17:05
1
  • @aaron c Thats it...i was just not adding new Aaraylist ..you made my day Commented Oct 24, 2014 at 17:15
1

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());
 }
}
answered Oct 24, 2014 at 17:00
1
  • Thats it...i was just not adding new Aaraylist ..you made my day Commented Oct 24, 2014 at 17:16

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.