How to Convert List of Lists to HashSet in Java Using Lambda Expression?
Last Updated :
12 Feb, 2024
A lambda expression is a short code block that accepts parameters and outputs a result. Similar to methods, lambda expressions can be used directly within the body of a method and do not require a name. Converting a List of Lists to a HashSet is one typical situation. Java 8 introduces a feature called Lambda Expressions, which can be used elegantly to accomplish this change.
In this article, we will learn How to Convert a List of Lists to HashSet in Java Using Lambda Expression in Java.
Methods to Convert Lists of Lists to HashSet Using Lambda Expressions
There are certain methods to implement List if lists to HashSet using Lambda Expression mentioned below:
- Using flattening the nested lists
- Using the forEach() method along with the addAll() method of the HashSet.
Program to Convert a List of Lists to HashSet in Java Using Lambda Expression
Method 1: Using Flattening the Nested Lists
By flattening the nested lists and gathering the elements into a HashSet, we can use Lambda Expressions in Java to transform a List of Lists to a HashSet.
Java
import java.util.*;
import java.util.stream.Collectors;
public class GFG
{
public static void main(String args[])
{
List<List<Integer>> list = new ArrayList<>();
list.add(Arrays.asList( 10 , 20 , 30 ));
list.add(Arrays.asList( 30 , 50 , 60 ));
list.add(Arrays.asList( 70 , 20 , 90 ));
Set<Integer> res = list.stream()
.flatMap(List::stream)
.collect(Collectors.toSet());
System.out.println( "Result HashSet: " + res);
}
}
|
Output
Result HashSet: [50, 20, 70, 10, 90, 60, 30]
Explanation of the above Program:
In the above program,
- We have a list of lists named
list
, where each inner list contains integers.
- We use Java Streams to convert the list of lists into a single stream of integers using the
flatMap
operation.
- The
flatMap
operation flattens the nested lists into a single stream of elements.
- We collect the elements of the stream into a HashSet using the
collect(Collectors.toSet())
terminal operation.
- At last, it prints the contents of the resulting HashSet
res
.
Method 2: Using the forEach() Method along with the addAll() Method
We first build a HashSet, and then we loop over each inner list using the forEach method on the outer list. The addAll() method is used inside the lambda expression to add every element from the inner list to the HashSet.
Java
import java.util.*;
import java.util.stream.Collectors;
public class GFG
{
public static void main(String args[])
{
List<List<Integer>> list = new ArrayList<>();
list.add(Arrays.asList( 10 , 20 , 30 ));
list.add(Arrays.asList( 40 , 50 , 60 ));
list.add(Arrays.asList( 70 , 80 , 90 ));
Set<Integer> res = new HashSet<>();
list.forEach(innerList -> res.addAll(innerList));
System.out.println( "Result HashSet: " + res);
}
}
|
Output
Result HashSet: [80, 50, 20, 70, 40, 10, 90, 60, 30]
Explanation of the above Program:
In the above program,
- We have a list of lists named
list
, where each inner list contains integers.
- We use a lambda expression with
forEach
to iterate over each inner list (innerList
) in the list
.
- For each inner list, we add all its elements to a HashSet named
res
using addAll()
.
- At last, it prints the contents of the resulting HashSet
res
.
Share your thoughts in the comments
Please Login to comment...