This tutorial gives you simple way to find unique values in ArrayList using TreeSet and HashSet. HashSet is much faster than TreeSet (constant-time versus log-time for most operations like add, remove and contains) but offers no ordering guarantees like TreeSet.
If you have any of below questions then you are at right place.
- Get unique values from arraylist in java
- How to get unique values in List
- Create an ArrayList of unique values
- How to Remove Duplicates from ArrayList in Java
- Distinct value from ArrayList in JAVA
- How to create an ArrayList of unique values in Java
Let’s get started:
- Create a class
CrunchifyUniqueArrayList.java
.
Here is a what we will use in our program:
package crunchify.com.tutorials; import java.util.Arrays; import java.util.HashSet; import java.util.List; import java.util.Set; import java.util.TreeSet; /** * @author Crunchify.com * How to find Unique Values in ArrayList. * Create your own utility to find unique elements in ArrayList. */ public class CrunchifyUniqueArrayList { public static void main(String[] args) { List<String> crunchifyStringList = Arrays.asList("Facebook", "Twitter", "Google", "Facebook", "Google", "Twitter", "Google"); System.out.printf("\n====== Initial list: %s%n", crunchifyStringList); sortList(crunchifyStringList); // Let's create our own utility to find unique elements in ArrayList :) crunchifyPrintUniqueValues(crunchifyStringList, crunchifyStringList.size()); } static void crunchifyPrintUniqueValues(List<String> crunchifyArray, int crunchifySize) { // iterate through all elements System.out.print ("\n\n====== From crunchifyPrintUniqueValues() Method: "); for (int externalLoop = 0; externalLoop < crunchifySize; externalLoop++) { int internalLoop; for (internalLoop = 0; internalLoop < externalLoop; internalLoop++) if (crunchifyArray.get(externalLoop) == crunchifyArray.get(internalLoop)) break; // Print only if it's not printed before if (externalLoop == internalLoop) System.out.print(crunchifyArray.get(externalLoop) + " "); } } public static void sortList(List<String> myList) { // HashSet() constructs a new set containing the elements in the specified collection. // The HashMap is created with default load factor (0.75) and an initial capacity sufficient to contain the elements in the specified collection. Set<String> hashSetList = new HashSet<String>(myList); System.out.printf("\n====== Unique values using HashSet: %s", hashSetList); // TreeSet() Constructs a new tree set containing the elements in the specified collection, sorted according to the natural ordering of its elements. // All elements inserted into the set must implement the Comparable interface. // Furthermore, all such elements must be mutually comparable: e1.compareTo(e2) must not throw a ClassCastException for any elements e1 and e2 in the set. Set<String> treeSetList = new TreeSet<String>(myList); System.out.printf("\n====== Unique values using TreeSet: %s", treeSetList); } }
Output:
Here is a Eclipse Console Output. Just run an application as a Java program.
====== Initial list: [Facebook, Twitter, Google, Facebook, Google, Twitter, Google] ====== Unique values using HashSet: [Google, Twitter, Facebook] ====== Unique values using TreeSet: [Facebook, Google, Twitter] ====== From crunchifyPrintUniqueValues() Method: Facebook Twitter Google Process finished with exit code 0
Let me know if you face any issue running this java program.