Java Unique List

From EggeWiki
Revision as of 03:32, 3 October 2007 by Egge (talk | contribs) (New page: The built in Java collections do not provide a built in method to get the unique items from a list. Here's an easy an efficient way to get the unique items. Simply put your '''list''' in...)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

The built in Java collections do not provide a built in method to get the unique items from a list. Here's an easy an efficient way to get the unique items. Simply put your list into a set and then create a list again.

<geshi lang="java5"> ArrayList list = new ArrayList(); list.add("A"); list.add("B"); list.add("A"); Set set = new HashSet(list); ArrayList b = new ArrayList(set); assertEquals(2, b.size()); </geshi>