How to Get Unique Values from ArrayList using Java 8?
Last Updated :
03 Nov, 2022
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
import java.util.*;
import java.util.stream.Collectors;
public class Main {
public static void main(String[] args)
{
ArrayList<Integer> Numbers
= new ArrayList<Integer>();
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" );
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
import java.util.ArrayList;
import java.util.HashSet;
public class Main {
public static void main(String[] args)
{
ArrayList<Integer> Numbers
= new ArrayList<Integer>();
Numbers.add( 1 );
Numbers.add( 2 );
Numbers.add( 1 );
Numbers.add( 4 );
Numbers.add( 2 );
HashSet<Integer> hashSetNumbers
= new HashSet<Integer>(Numbers);
System.out.println( "Unique Values of ArrayList" );
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
import java.util.*;
public class Main {
public static void main(String[] args)
{
LinkedHashSet<Integer> UniqueList
= new LinkedHashSet<Integer>();
UniqueList.add( 1 );
UniqueList.add( 2 );
UniqueList.add( 3 );
UniqueList.add( 3 );
UniqueList.add( 2 );
List<Integer> ls
= new ArrayList<Integer>(UniqueList);
System.out.println(ls);
}
}
|
Method 4 : Using ArrayList ,add() and contains() method
Java
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]
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...