How to Get Unique Values from ArrayList using Java 8? - GeeksforGeeks

08 Dec, 2020
ArrayList in Java do not prevent the list from having duplicate values. But there are ways if you want to get unique values from the ArrayList and each way is explained with an example.  
Method 1(Using Stream API’s distinct() Method): For Java 8, You can use Java 8 Stream API. To get distinct values, the distinct() method is an intermediate operation that also filters the stream to pass the next operation. Java Stream collect() is used to collect the stream elements to a collection (in this case a list).
Syntax:
Stream<T> distinct()
Return type: Stream is an interface and the function. It returns a stream consisting of distinct elements. 
Java
// Java program to demonstrate// how to Get Unique Values from ArrayList  import java.util.*;import java.util.stream.Collectors;  public class Main {      public static void main(String[] args)    {          // Create ArrayList of Integers        ArrayList<Integer> Numbers            = new ArrayList<Integer>();          // add elements to ArrayList        Numbers.add(1);        Numbers.add(2);        Numbers.add(1);        Numbers.add(4);        Numbers.add(2);          List<Integer> UniqueNumbers            = Numbers.stream().distinct().collect(                Collectors.toList());          System.out.println("Unique Values of ArrayList");          // iterate through List        for (int i = 0; i < UniqueNumbers.size(); ++i) {            System.out.println(UniqueNumbers.get(i));        }    }}
Unique Values of ArrayList
1
2
4
Method 2(Using HashSet): One can convert the ArrayList to HashSet which does not allow duplicate values. For conversion, use the constructor of HashSet class which accepts Collection as an argument.
Java
// Java program to demonstrate// how to Get Unique Values from ArrayList  import java.util.ArrayList;import java.util.HashSet;  public class Main {      public static void main(String[] args)    {          // Create ArrayList of Integer        ArrayList<Integer> Numbers            = new ArrayList<Integer>();          // add elements to ArrayList        Numbers.add(1);        Numbers.add(2);        Numbers.add(1);        Numbers.add(4);        Numbers.add(2);          /*Converting List to HashSet using constructor.         */          HashSet<Integer> hashSetNumbers            = new HashSet<Integer>(Numbers);          System.out.println("Unique Values of ArrayList");          // iterate through HashSet        for (Integer strNumber : hashSetNumbers)            System.out.println(strNumber);    }}
Unique Values of ArrayList
1
2
4
Maintaining order with no duplicate element’s insertion in the ArrayList.
LinkedHashSet is an ordered version of HashSet that maintains a doubly-linked List across all elements. Let us iterate through the elements in the order in which they were inserted. Following is the code that maintains insertion order and does not allow duplicate elements.
Java
// Maintaining order with no duplicate elements insertion in// the ArrayList.  import java.util.*;  public class Main {      public static void main(String[] args)    {          // Using LinkedHashSet we can prevent        // insertion of duplicate elements        // as it implements the Set interface        LinkedHashSet<Integer> UniqueList            = new LinkedHashSet<Integer>();          // Adding elements to LinkedHashSet        UniqueList.add(1);        UniqueList.add(2);        UniqueList.add(3);        UniqueList.add(3); // duplicate 3 wont be inserted        UniqueList.add(2); // duplicate 2 wont be inserted          // Converting LinkedHashSet to List        List<Integer> ls            = new ArrayList<Integer>(UniqueList);          // Printing list        System.out.println(ls);    }}
[1, 2, 3]
Java-ArrayList
Picked
Java
Java Programs
Java
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Initialize an ArrayList in Java
Interfaces in Java
Multidimensional Arrays in Java
Singleton Class in Java
Stack Class in Java
Initializing a List in Java
Convert a String to Character array in Java
Java Programming Examples
Convert Double to Integer in Java
How to Iterate HashMap in Java?