Thursday, June 25, 2009

How to Retain the Insertion Order in a Set


Use LinkedHashSet to retain the insertion order

Set set = new LinkedHashSet();

// Add some elements
set.add("1");
set.add("2");
set.add("3");
set.add("1");

// Iterate the set
for (Iterator it=set.iterator(); it.hasNext(); ) {
Object o = it.next();
}
// [1, 2, 3]

0 comments:

 

Sample Java Codes Copyright © 2008 Green Scrapbook Diary Designed by SimplyWP | Made free by Scrapbooking Software | Bloggerized by Ipiet Notez