Saturday, 25 September 2010

Creating a Map That Retains Order-of-Insertion

Map map = new LinkedHashMap();

// Add some elements 
map.put("1", "value1");
map.put("2", "value2");
map.put("3", "value3");
map.put("2", "value4");

// List the entries 
for (Iterator it=map.keySet().iterator(); it.hasNext(); )
{
   Object key = it.next();
  Object value = map.get(key); 
} // [1=value1, 2=value4, 3=value3]

No comments:

Post a Comment