Sunday, December 29, 2024
Google search engine
HomeLanguagesJavaHow to Get Unique Values from ArrayList using Java 8?

How to Get Unique Values from ArrayList using Java 8?

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));
        }
    }
}


Output

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);
    }
}


Output

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);
    }
}


Output

[1, 2, 3]

Method 4 : Using ArrayList ,add() and contains() method

Java




// Maintaining order with no duplicate elements insertion in
// the ArrayList.
 
import java.util.*;
 
public class Main {
 
    public static void main(String[] args)
    {
 
        List<Integer> ls = new ArrayList<Integer>();
        ls.add(1);
        ls.add(2);
        ls.add(1);
        ls.add(4);
        ls.add(5);
        List<Integer>ls1 = new ArrayList<Integer>();
        for(int i=0;i<ls.size();i++)
        {
            System.out.println(ls.get(i));
            int x;
            if(!ls1.contains(ls.get(i)))
            {
                x=ls.get(i);
                ls1.add(x);
            }
        }
        System.out.println(ls1);
    }
}


Output

1
2
1
4
5
[1, 2, 4, 5]
Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments