Java Unique List: Difference between revisions
(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 | 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 03:53, 1 September 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>