Is there any nice class for this situation?
Hello guys!I have an problem with a HashMap when I iterate over the keys.
Below is an example cod:
package test;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;
public class ScrapTest {
/**
* @param args
*/
@SuppressWarnings("unchecked")
public static void main(String[] args) {
HashMap hashmap = new HashMap();
hashmap.put("a", "object 1");
hashmap.put("c", "object 2");
hashmap.put("b", "object 3");
hashmap.put("f", "object 4");
hashmap.put("g", "object 5");
hashmap.put("e", "object 6");
Set keys = hashmap.keySet();
Iterator iterator = keys.iterator();
while( iterator.hasNext() )
{
String key = (String)iterator.next();
System.out.println(key);
}
}
}
This gave me this output:
f
g
e
b
c
a
I want to get this output (the order that they are inserted):
a
c
b
f
g
e
Is there a way to manage this?
Perhaps whit some other class?
So if you guys got any ideas please let me know!
Best regards
Fredrik
