In Java, the Stream class was introduced as a part of the Java 8 Streams API that provides a functional way of processing collections of objects, i.e. a sequence of elements that can be processed in a functional manner.
With the help of the Stream class, you can perform various operations on collections like filtering, mapping, sorting, reducing, and more, without modifying the original collection.
Converting List to Set using Java 8 Stream API
Let's see the steps,
Step 1: We create a List of Strings (can be any other data type)
Step 2: We make use of the stream() to convert List to Stream.
Step 3: Finally, we make use of the collect() method to collect the Stream as set.
Code Example
package org.example;
import java.util.*;
import java.util.stream.Collectors;
/**
*
* Code Example in Java 8 to convert
* List to Set using Stream API
*
*
*/
public class ListToSet {
public static void main(String[] args) {
List<String> citiesList = new ArrayList<>();
citiesList.add("NYC");
citiesList.add("Chicago");
citiesList.add("Austin");
citiesList.add("Houston");
citiesList.add("Ohio");
citiesList.add("NYC"); //Duplicates
citiesList.add("Chicago"); //Duplicates
System.out.println("List Elements: " + citiesList);
//Map to List
Set<String> citiesSet = citiesList
.stream() //Step 1: Convert Set to Stream
.collect(Collectors.toSet()); // Step 2: Collect Stream as Set
System.out.println("Set Elements: " + citiesSet);
}
}
Output:
List Elements: [NYC, Chicago, Austin, Huston, Ohio, NYC, Chicago]
Set Elements: [Chicago, NYC, Ohio, Austin, Huston]
As you may see in the output, the set does not contain duplicate records.
Facing issues? Have Questions? Post them here! I am happy to answer!
- Deep Dive into Java 8 Predicate Interface
- Read and Parse XML file using Java DOM Parser [Java Tutorial]
- Java 8 Predicate Functional Interface isEqual() Method Example
- Convert Multidimensional Array toString In Java
- How to read int value using Scanner Class Java
- Spring Boot AI + LLM + Java Code Example
- Write to a File using Java Stream API
- Implementing Bubble Sort Algorithm using Java Program
- How to Fix XmlBeanDefinitionStoreException in Java SpringBoot ApplicationConfig.xml
- YAML Parser using Java Jackson Library Example
- [Fix] java: integer number too large compilation error
- Convert JSON String to Java GSON Object Example
- Read a file using Java 8 Stream
- Java Spring Boot 3 Web Hello World with Gradle in IntelliJ
- Ways Compare Dates in Java Programming with Examples
- Pretty Print JSON String in Java Console Output
- Java JDBC with Join Queries Example
- How to Check For Updates on Windows 11 (Step-by-Step)
- [Fix] java.net.MalformedURLException: unknown protocol
- How to display date and time in GMT Timezone in Java
- Error: LinkageError occurred while loading main class UnsupportedClassVersionError [Eclipse Java]
- How to convert a String to Java 8 Stream of Char?
- RabbitMQ Queue Listener Java Spring Boot Code Example
- 5+ Fibonacci number Series Java Program Examples [ 0 1 1 2 3 ..]
- Handling NullPointerException with Java Predicate
- How to Sort a Vector in Rust with Examples - Rust
- [Vi/Vim] How to move cursor to the start of a line - MacOS
- Visual Studio Code: Restricted Mode is intended for safe code browsing - MacOS
- Save a file as CSV Format TextEdit on Mac - MacOS
- How to get list of all Java versions installed on macOS - Java
- Show battery percentage on MacBook Menu Bar [Ventura 13] - MacOS
- Remove AirDrop Icon from macOS Menu Bar - MacOS
- How to Add Edit with Notepad++ Option to Windows 10 or 11 Right Click Menu Options - Windows-11