
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to use the collect() method in Stream API in Java 9?
In this article, we will learn to use the collect() method in the Stream API in Java 9.
Java Stream API collect() Method
In Java, the collect() method in the Stream API collects all objects from a stream object and stores them in a type of collection.
The user has to provide what type of collection the results can be stored. We specify the collection type using the Collectors Enum. There are different types, and different operations can be present in the Collectors Enum.
Syntax
The following is the syntax for the collect() method declaration:
<R, A> R collect(Collector<? super T,A,R> collector)
Parameters for collect() Method:
- T: The type of elements in the stream.
-
R: The type of the result.
-
A: The intermediate accumulation type of the Collector.
- collector: The Collector describing the reduction.
Different Collection Types
The following are the three different collection types in the collect() method of the Stream API in Java:
Collecting to List
A List is an ordered collection of elements that can contain duplicates. It is an interface that extends the Collection interface and offers methods to add, delete, access, and manipulate elements in a sequence.
The collect() method provides the Collectors.toList() to accumulate stream elements into a list interface.
collect(Collectors.toList())
Example
Below is an example of the collect() method with a list interface:
import java.util.*; import java.util.stream.*; public class StreamCollectListTest { public static void main(String args[]) { List<String> list = List.of("a", "b", "c", "d", "e", "f", "g", "h", "i"); List<String> subset1 = list.stream() .takeWhile(s -> !s.equals("e")) .collect(Collectors.toList()); System.out.println(subset1); List<Integer> numbers = Stream.iterate(1, i -> i <= 10, i -> i+1) .collect(Collectors.toList()); System.out.println(numbers); } }
Output
[a, b, c, d] [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Collecting to Set
A Set is an interface that inherits the Collection interface and contains no duplicate elements. A Set interface has methods to add, delete, and check the existence of an element.
The collect() method provides the Collectors.toSet() to accumulate stream elements into a set interface.
collect(Collectors.toSet())
Example
Below is an example of the collect() method with a set interface:
import java.util.*; import java.util.stream.*; public class StreamCollectSetTest { public static void main(String args[]) { List<String> list = List.of("a", "b", "c", "d", "e", "f", "g", "h", "i", "a", "e"); Set<String> subset1 = list.stream() .takeWhile(s -> !s.equals("e")) .collect(Collectors.toSet()); System.out.println(subset1); Set<Integer> numbers = Stream.iterate(1, i -> i <= 10, i -> i+1) .collect(Collectors.toSet()); System.out.println(numbers); } }
Output
[a, b, c, d] [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Collecting to Map
A Map is an interface in the Collections framework used to store data in the form of key-value pairs. Each key in the map is unique and maps to some value.
The collect() method provides the Collectors.toMap() to accumulate stream elements into a Map interface.
collect(Collectors.toMap())
Example
Below is an example of the collect() method with a map interface:
import java.util.*; import java.util.stream.*; public class StreamCollectMapTest { public static void main(String args[]) { List<String> strings = List.of("Ravi", "Raja", "Krishna", "Teja"); Map<String, Integer> stringLengths = strings.stream() .takeWhile(s -> !s.equals("Teja")) .collect(Collectors.toMap( s -> s, s -> s.length() )); System.out.println(stringLengths); Map<Integer, String> numberedItems = Stream.iterate(1, i -> i <= 5, i -> i + 1) .collect(Collectors.toMap( i -> i, i -> "Item" + i )); System.out.println(numberedItems); } }
Output
{Raja=4, Krishna=7, Ravi=4} {1=Item1, 2=Item2, 3=Item3, 4=Item4, 5=Item5}