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 Steam
.collect(Collectors.toSet()); // Step 2: Collect Steam 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!
More Posts related to Java,
- Java equals method - Tutorial
- Unbound classpath container: JRE System Library [JavaSE-1.7]
- Spring Boot: @RequestBody not applicable to method
- Java 8: Steam map with Code Examples
- Java Program: Random Number Generator
- Java java.time.Clock class code examples [Java Date Time API]
- Fix: type argument is not within bounds of type-variable T
- [Fix] java.net.MalformedURLException: unknown protocol
- Java 7 addSuppression() and getSuppression() Exception Handling
- Convert Java Array to ArrayList Code Example
- How to Word-Warp Console logs in IntelliJ
- Ways Compare Dates in Java Programming with Examples
- Remove Trailing zeros BigDecimal Java
- CRUD operations in Spring Boot + JDBC
- [Java Threads] Should we extend Thread Class or implement Runnable interface
- Json Serialization and Deserialization using Java Jackson
- Create simple struts2 project using maven commands
- How to install Java OpenJDK 11 on Alpine Linux
- Unsupported major.minor version 52.0 in java
- Error: Can not find the tag library descriptor for
- Java: Convert String to Binary
- How to run Java Unit Test cases with Apache Maven?
- Java: Testing Private Methods in JUnit using reflection API Example
- Java JDBC Connection with MySQL Driver in VS Code + Troubleshooting
- Java Join Strings with Comma Separator
More Posts:
- How to disable warnings while Python file execution - Python
- Install Bash on Alpine Linux - Docker - Docker
- How to get Mac Computer Name using Terminal - MacOS
- Change default language highlighting in Notepad++ - NotepadPlusPlus
- How to start or open a new bourne-again shell (bash) session on Windows using Command Line CMD - Bash
- Update Powershell Using Command Line - Powershell
- How to show SSL Certificate details using cURL Command - cURL
- How to create a Java Project as a Git Repository with IntelliJ - Java