Java Unique List: Difference between revisions

From EggeWiki
(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...)
 
mNo edit summary
Line 1: Line 1:
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.
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">
<geshi lang="java5">

Revision as of 23:53, 31 August 2008

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>