<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: Fast Way to Get All Unique Values from a List in Java</title>
	<atom:link href="http://code.hammerpig.com/fast-way-to-get-all-unique-values-from-a-list-in-java.html/feed" rel="self" type="application/rss+xml" />
	<link>http://code.hammerpig.com/fast-way-to-get-all-unique-values-from-a-list-in-java.html</link>
	<description>Tips and short tutorials on various programming technologies</description>
	<lastBuildDate>Sun, 05 Feb 2012 17:17:05 -0600</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.1</generator>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>By: Spiro</title>
		<link>http://code.hammerpig.com/fast-way-to-get-all-unique-values-from-a-list-in-java.html/comment-page-1#comment-651</link>
		<dc:creator>Spiro</dc:creator>
		<pubDate>Wed, 30 Mar 2011 12:48:45 +0000</pubDate>
		<guid isPermaLink="false">http://code.hammerpig.com/?p=246#comment-651</guid>
		<description>The Oleg&#039;a idea for a ArrayList:



public static ArrayList make_unique( ArrayList inArray){
		String[] tmpLst= new  String[inArray.size()];
		for(int i=0; i&lt; inArray.size(); i++){
			tmpLst[i] =inArray.get(i);
			}
		Arrays.sort(tmpLst);
		inArray.clear();
		int lastArray =-1;
		for(int i=1; i&lt; tmpLst.length; i++){
			if(lastArray==-1 &#124;&#124; !tmpLst[i].equals(inArray.get(lastArray))) ){
				inArray.add(tmpLst[i]);
				lastArray++;
				}
			}
		return inArray;
		}</description>
		<content:encoded><![CDATA[<p>The Oleg&#8217;a idea for a ArrayList:</p>
<p>public static ArrayList make_unique( ArrayList inArray){<br />
		String[] tmpLst= new  String[inArray.size()];<br />
		for(int i=0; i&lt; inArray.size(); i++){<br />
			tmpLst[i] =inArray.get(i);<br />
			}<br />
		Arrays.sort(tmpLst);<br />
		inArray.clear();<br />
		int lastArray =-1;<br />
		for(int i=1; i&lt; tmpLst.length; i++){<br />
			if(lastArray==-1 || !tmpLst[i].equals(inArray.get(lastArray))) ){<br />
				inArray.add(tmpLst[i]);<br />
				lastArray++;<br />
				}<br />
			}<br />
		return inArray;<br />
		}</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Help on my Java program</title>
		<link>http://code.hammerpig.com/fast-way-to-get-all-unique-values-from-a-list-in-java.html/comment-page-1#comment-438</link>
		<dc:creator>Help on my Java program</dc:creator>
		<pubDate>Mon, 10 May 2010 17:23:05 +0000</pubDate>
		<guid isPermaLink="false">http://code.hammerpig.com/?p=246#comment-438</guid>
		<description>[...] Instead of looping through each letter of the given letter list (letters), Get a sub-list of all of the unique letters in the letter list (link: Fast way to get all unique value from a list in Java) [...]</description>
		<content:encoded><![CDATA[<p>[...] Instead of looping through each letter of the given letter list (letters), Get a sub-list of all of the unique letters in the letter list (link: Fast way to get all unique value from a list in Java) [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Oleg</title>
		<link>http://code.hammerpig.com/fast-way-to-get-all-unique-values-from-a-list-in-java.html/comment-page-1#comment-316</link>
		<dc:creator>Oleg</dc:creator>
		<pubDate>Thu, 17 Dec 2009 08:30:47 +0000</pubDate>
		<guid isPermaLink="false">http://code.hammerpig.com/?p=246#comment-316</guid>
		<description>one way to do it efficiently is to use Arrays.sort method on your array, using the default comparator for the types which implement Comparable or defying a new Comparator for your custom types. Than just iterate through the array to get unique values like in the code below

		int[] a = new int[6];
		a[0] = 10; a[1] = 100; a[2] = 100;
		a[3] = 20; a[4] = 10; a[5] = 10;
		Arrays.sort(a);
		int value = a[0];
		int i = 1;
		while(i &lt; a.length) {
			if(a[i] == value) {
				if(i == (a.length - 1)) {
					System.out.println(a[i]);
				}
			} else {
				System.out.println(a[i - 1]);
				value = a[i];
				if(i == (a.length - 1)) {
					System.out.println(a[i]);
				}
			}
			i++;
		}

this will fine as long as a proper Comparator is provided to Arrays.sort() method</description>
		<content:encoded><![CDATA[<p>one way to do it efficiently is to use Arrays.sort method on your array, using the default comparator for the types which implement Comparable or defying a new Comparator for your custom types. Than just iterate through the array to get unique values like in the code below</p>
<p>		int[] a = new int[6];<br />
		a[0] = 10; a[1] = 100; a[2] = 100;<br />
		a[3] = 20; a[4] = 10; a[5] = 10;<br />
		Arrays.sort(a);<br />
		int value = a[0];<br />
		int i = 1;<br />
		while(i &lt; a.length) {<br />
			if(a[i] == value) {<br />
				if(i == (a.length &#8211; 1)) {<br />
					System.out.println(a[i]);<br />
				}<br />
			} else {<br />
				System.out.println(a[i - 1]);<br />
				value = a[i];<br />
				if(i == (a.length &#8211; 1)) {<br />
					System.out.println(a[i]);<br />
				}<br />
			}<br />
			i++;<br />
		}</p>
<p>this will fine as long as a proper Comparator is provided to Arrays.sort() method</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Titom</title>
		<link>http://code.hammerpig.com/fast-way-to-get-all-unique-values-from-a-list-in-java.html/comment-page-1#comment-315</link>
		<dc:creator>Titom</dc:creator>
		<pubDate>Thu, 17 Dec 2009 01:25:42 +0000</pubDate>
		<guid isPermaLink="false">http://code.hammerpig.com/?p=246#comment-315</guid>
		<description>The example only works because the items you add to the List are added following a natural order. If you test it using:
x.add(&quot;def&quot;);
x.add(&quot;abc&quot;);
x.add(&quot;abc&quot;);
x.add(&quot;ghi&quot;);
x.add(&quot;def&quot;);
Lists.GetUniqueValues(x) will return a List that contains (&quot;abc&quot;, &quot;def&quot;, &quot;ghi&quot;) which is not what is expected... What is expected is (&quot;def&quot;, &quot;abc&quot;, &quot;ghi&quot;).

What I suggest is one of the following solutions:
1- You can include external free libraries in your application? Go with the Apache library Commons Collections, the SetUniqueList is what you are looking for(http://commons.apache.org/collections/api-release/org/apache/commons/collections/list/SetUniqueList.html),
2- You can&#039;t include any external libraries? Create a new class UniqueArrayList that extends ArrayList and overrides the add methods so they check if the object to add is already in the list to prevent duplicates.</description>
		<content:encoded><![CDATA[<p>The example only works because the items you add to the List are added following a natural order. If you test it using:<br />
x.add(&#8220;def&#8221;);<br />
x.add(&#8220;abc&#8221;);<br />
x.add(&#8220;abc&#8221;);<br />
x.add(&#8220;ghi&#8221;);<br />
x.add(&#8220;def&#8221;);<br />
Lists.GetUniqueValues(x) will return a List that contains (&#8220;abc&#8221;, &#8220;def&#8221;, &#8220;ghi&#8221;) which is not what is expected&#8230; What is expected is (&#8220;def&#8221;, &#8220;abc&#8221;, &#8220;ghi&#8221;).</p>
<p>What I suggest is one of the following solutions:<br />
1- You can include external free libraries in your application? Go with the Apache library Commons Collections, the SetUniqueList is what you are looking for(http://commons.apache.org/collections/api-release/org/apache/commons/collections/list/SetUniqueList.html),<br />
2- You can&#8217;t include any external libraries? Create a new class UniqueArrayList that extends ArrayList and overrides the add methods so they check if the object to add is already in the list to prevent duplicates.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: admin</title>
		<link>http://code.hammerpig.com/fast-way-to-get-all-unique-values-from-a-list-in-java.html/comment-page-1#comment-310</link>
		<dc:creator>admin</dc:creator>
		<pubDate>Sun, 13 Dec 2009 02:24:58 +0000</pubDate>
		<guid isPermaLink="false">http://code.hammerpig.com/?p=246#comment-310</guid>
		<description>Stuart and Oleg suggest this is not the best way to approach the solution. What do you (or someone else) suggest as a way of doing it better? This forum is about sharing information.</description>
		<content:encoded><![CDATA[<p>Stuart and Oleg suggest this is not the best way to approach the solution. What do you (or someone else) suggest as a way of doing it better? This forum is about sharing information.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Oleg</title>
		<link>http://code.hammerpig.com/fast-way-to-get-all-unique-values-from-a-list-in-java.html/comment-page-1#comment-309</link>
		<dc:creator>Oleg</dc:creator>
		<pubDate>Sat, 12 Dec 2009 23:22:24 +0000</pubDate>
		<guid isPermaLink="false">http://code.hammerpig.com/?p=246#comment-309</guid>
		<description>not to mention it is not the most efficient way to do it</description>
		<content:encoded><![CDATA[<p>not to mention it is not the most efficient way to do it</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Stuart</title>
		<link>http://code.hammerpig.com/fast-way-to-get-all-unique-values-from-a-list-in-java.html/comment-page-1#comment-250</link>
		<dc:creator>Stuart</dc:creator>
		<pubDate>Fri, 14 Aug 2009 14:48:22 +0000</pubDate>
		<guid isPermaLink="false">http://code.hammerpig.com/?p=246#comment-250</guid>
		<description>There&#039;s only one problem I can see with both of those solutions: It relies on there not being any collision between hashCode() values. In general it&#039;s the case, but the contract of the hashCode() method doesn&#039;t guarantee it, so you could potentially lose unique values that hash to the same as something else.</description>
		<content:encoded><![CDATA[<p>There&#8217;s only one problem I can see with both of those solutions: It relies on there not being any collision between hashCode() values. In general it&#8217;s the case, but the contract of the hashCode() method doesn&#8217;t guarantee it, so you could potentially lose unique values that hash to the same as something else.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: shahzad</title>
		<link>http://code.hammerpig.com/fast-way-to-get-all-unique-values-from-a-list-in-java.html/comment-page-1#comment-163</link>
		<dc:creator>shahzad</dc:creator>
		<pubDate>Thu, 27 Nov 2008 10:11:22 +0000</pubDate>
		<guid isPermaLink="false">http://code.hammerpig.com/?p=246#comment-163</guid>
		<description>return new ArrayList(new HashSet(values)); will serve the same purpose.</description>
		<content:encoded><![CDATA[<p>return new ArrayList(new HashSet(values)); will serve the same purpose.</p>
]]></content:encoded>
	</item>
</channel>
</rss>

