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
// 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));
}
}
}
OutputUnique 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);
}
}
OutputUnique 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);
}
}
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);
}
}
Output1
2
1
4
5
[1, 2, 4, 5]
Similar Reads
Get Unique Values from ArrayList in Java Let's see how to get unique values from ArrayList. Convert ArrayList to HashSet to insert duplicate values in ArrayList but on the other hand, HashSet is not allowing to insert any duplicate value. While converting ArrayList to HashSet all the duplicate values are removed and as a result, unique val
2 min read
How to Maintain Insertion Order While Getting Unique Values from ArrayList in Java? ArrayList is a part of collection framework and is present in java.util package. It provides us with dynamic arrays in Java. Though, it may be slower than standard arrays but can be helpful in programs where lots of manipulation in the array is needed. This class is found in java.util package. If we
3 min read
Get Values with Unique Elements from a List in Java In Java, we can write programs to get values with unique elements using loops, conditional statements, and string manipulation. The basic idea is to go through a set of numbers, check each number for repeating digits, eliminate them if any repeated number occurs, and print unique numbers from that l
2 min read
How to Convert ArrayList to HashMap Before Java 8? ArrayList is a resizable array. It is under the java package java.util. It gives dynamic arrays in Java. ArrayList is useful in programs where a lot of changes in the array are required but these are also slower than the standard arrays. The elements in an ArrayList can be added and removed whenever
4 min read
How to Convert TreeMap to an ArrayList in Java? TreeMap is a part of the Java Collection framework. Java TreeMap contains values based on the key. It implements the NavigableMap interface and extends AbstractMap class. It provides an efficient means of storing key-value pairs in sorted order. Java TreeMap contains only unique elements. It cannot
4 min read
How to Convert ArrayList to HashSet in Java? ArrayList: In Java, ArrayList can have duplicates as well as maintains insertion order. HashSet: HashSet is the implementation class of Set. It does not allow duplicates and uses Hashtable internally. There are four ways to convert ArrayList to HashSet : Using constructor.Using add() method by itera
3 min read