Java Unique List

From EggeWiki
Revision as of 23:53, 31 August 2008 by Egge (talk | contribs)

The Java collections bundled with the JDK 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>