I have the following code:
LinkedHashMap<String,ArrayList<String>> h;
Set set = h.entrySet();
Iterator i = set.iterator();
while(i.hasNext()) {
System.out.println(i.next());
Map.Entry me = (Map.Entry)i.next();
String currentSegString = (String) me.getKey();
System.out.println(currentKey+"**************");
}
Prints out this:
1=[]
2**************
3=[A, B, C]
4**************
5=[]
But then I remove one line System.out.println(i.next());:
LinkedHashMap<String,ArrayList<String>> h;
Set set = h.entrySet();
Iterator i = set.iterator();
while(i.hasNext()) {
Map.Entry me = (Map.Entry)i.next();
String currentSegString = (String) me.getKey();
System.out.println(currentKey+"**************");
}
And it prints out this:
1**************
2**************
3**************
4**************
5**************
Why doesn’t it print ************** in the first case for each key?
That is because when you do :
You are skipping to the next line, and then the
Mapalso does.next()Therefor you are only seeing 2 out of the possible 5 lines.
Explanation:
Second code:
A way to fix this would be: