Java ArrayList get unique values example shows how to get unique values from ArrayList in Java. The example also shows how to maintain the insertion order of the elements.
How to get unique values from ArrayList in Java (unique elements)?
ArrayList class does not prevent us from adding duplicate values. If you want to get unique values from the ArrayList, you can convert the ArrayList to HashSet which does not allow duplicate values.
Use constructor of HashSet class which accepts Collection as an argument to convert ArrayList to HashSet.
1 |
public HashSet(Colllection<? extends E> c) |
This constructor accepts any collection and creates a new set containing the elements of the specified collection.
Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
package com.javacodeexamples.collections.arraylist; import java.util.ArrayList; import java.util.HashSet; public class ArrayListGetUniqueValuesExample { public static void main(String[] args) { //Create ArrayList of String objects ArrayList<String> aListNumbers = new ArrayList<String>(); //add elements to ArrayList aListNumbers.add("One"); aListNumbers.add("Two"); aListNumbers.add("Three"); aListNumbers.add("One"); aListNumbers.add("Four"); /* * To get unique values from ArrayList, convert it to * HashSet using constructor of the HashSet class. * * public HashSet(Collection c) */ HashSet<String> hSetNumbers = new HashSet(aListNumbers); System.out.println("ArrayList Unique Values"); //iterate through HashSet for(String strNumber : hSetNumbers) System.out.println(strNumber); } } |
Output
1 2 3 4 5 |
ArrayList Unique Values Three One Four Two |
How to maintain insertion order while getting unique values from ArrayList?
As you may have observed from the output, the order of the elements retrieved from HashSet is not the same order in which we added elements to the ArrayList. That is because HashSet does not maintain the insertion order of the elements.
If you want to maintain the insertion order of the elements, you can use LinkedHashSet instead of HashSet. LinkedHashSet maintains the order in which the elements are inserted while retrieving.
1 2 3 4 5 6 7 |
LinkedHashSet<String> lhSetNumbers = new LinkedHashSet(aListNumbers); System.out.println("ArrayList Unique Values"); //iterate through HashSet for(String strNumber : lhSetNumbers) System.out.println(strNumber); |
Output
1 2 3 4 5 |
ArrayList Unique Values One Two Three Four |
How to get unique values from ArrayList using Java 8?
If you are using Java version 8, you can use stream
and distinct
to get unique values from the ArrayList in just one line as given below.
1 2 3 4 5 6 7 |
List<String> listUnique = aListNumbers.stream().distinct().collect(Collectors.toList()); System.out.println("ArrayList Unique Values"); //iterate through List for(String strNumber : listUnique) System.out.println(strNumber); |
Output
1 2 3 4 5 |
ArrayList Unique Values One Two Three Four |
How to prevent the addition of duplicate elements to the ArrayList?
How about preventing the addition of duplicate elements to the ArrayList in the first place? If you want to add only unique elements to the ArrayList, you should first check if the element already exists in the ArrayList. If the element exists then do not add it, otherwise add it. Use contains
method of ArrayList to check whether the element exists in the ArrayList.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
package com.javacodeexamples.collections.arraylist; import java.util.ArrayList; public class ArrayListGetUniqueValuesExample { public static void main(String[] args) { //Create ArrayList of String objects ArrayList<String> aListNumbers = new ArrayList<String>(); //add elements to ArrayList System.out.println( "Element Added? " + addElement(aListNumbers, "One")); System.out.println( "Element Added? " + addElement(aListNumbers, "Two")); System.out.println( "Element Added? " + addElement(aListNumbers, "Three")); System.out.println( "Element Added? " + addElement(aListNumbers, "One")); System.out.println( "Element Added? " + addElement(aListNumbers, "Four")); System.out.println("ArrayList contains: " + aListNumbers); } private static boolean addElement(ArrayList<String> aListNumbers, String str){ /* * Check if the element already exists in the ArrayList. * * If it exists, do not add it. Otherwise add it to * the ArrayList */ //if element does not exists in the ArrayList, add it if(!aListNumbers.contains(str)){ aListNumbers.add(str); return true; } //this means element was already there in the ArrayList, return false return false; } } |
Output
1 2 3 4 5 6 |
Element Added? true Element Added? true Element Added? true Element Added? false Element Added? true ArrayList contains: [One, Two, Three, Four] |
As you can see from the output, the element “one” was not added the second time.
Note : contains
method of ArrayList class internally uses equals
method of argument object to compare them with one another. Since the String class has implemented equals
method, the above example worked and it identified the duplicate “one” object. If your ArrayList contains objects of a custom class, then that class must implement equals
and hashCode
methods to work.
This example is a part of the Java ArrayList tutorial with examples.
Please let me know your views in the comments section.