Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

java
communitycreator
toset
collectors

What is the Collectors.toSet() method in Java?

abhilash

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Collectors class

Collectors is a utility class, which provides various implementations of reduction operations, such as grouping elements, collecting elements to different collections, summarizing elements according to various criteria, and so on. The different functionalities in the Collectors class are usually used as the final operation on streams.

toSet() method

toSet() is a static method of the Collectors class, which is used to collect/accumulate all the elements to a new Set. The type, mutability, serializability, and thread-safety of the Set that is returned are not guaranteed.

The toSet method is defined in the Collectors class. The Collectors class is defined in the java.util.stream package. To import the Collectors class, we check the following import statement:

import java.util.stream.Collectors;

Syntax


public static <T> Collector<T, ?, Set<T>> toSet()

Parameters

This method has no parameters.

Return value

This method returns a Collector that collects all the input elements to a Set in the encountered order.

Code

import java.util.Arrays;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class Main {
public static void main(String[] args)
{
List<String> stringList = Arrays.asList("educative", "io", "edpresso", "educative", "io", "edpresso");
System.out.println("Stream before modification - " + stringList);
Stream<String> stringStream = stringList.stream();
Set<String> uppercaseSet = stringStream.map(String::toUpperCase).collect(Collectors.toSet());
System.out.println("Resulting set after modification - " + uppercaseSet);
}
}
Implementation of the "toSet" method

Code explanation

  • Lines 1–4: We import the relevant packages.
  • Line 10: We define a list of strings called stringList.
  • Line 12: We print the stringList.
  • Line 14: We create a stream out of the stringList.
  • Line 16: We convert the stream elements to uppercase, using the map method and collect the resulting elements to a new set with the toSet method.
  • Line 18: We print the new set of uppercase elements.

RELATED TAGS

java
communitycreator
toset
collectors

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring