Last Updated : 15 Jul, 2025
Introduced in Java 8, the
Stream APIis used to process collections of objects. A stream is a sequence of objects that supports various methods which can be pipelined to produce the desired result. In this article, the methods to convert a stream into a
mapis discussed.
Method 1: Using Collectors.toMap() FunctionThe
Collectors.toMap()method takes two parameters as the input:
The following are the examples of the
toMapfunction to convert the given stream into a map:
// Program to convert
// the Stream to Map
import java.io.*;
import java.util.stream.*;
import java.util.Arrays;
import java.util.Map;
class GFG {
// Function to convert the string
// to the map
public static Map toMap(String input)
{
Map<String, Integer> lengthMap
= Arrays.stream(input.split(" "))
.collect(Collectors.toMap(
value
-> value,
value -> value.length()));
return lengthMap;
}
public static void main(String[] args)
{
String input = "Geeks for Geek";
System.out.println(toMap(input));
}
}
Output:
{Geek=4, for=3, Geeks=5}In the above example, the toMap collector takes two lambda functions as parameters:
// Program to convert User[] into
// Map<userId, User>
import java.util.Arrays;
import java.util.Map;
import java.util.stream.*;
// Implementing the User class
public class User {
// Attributes of the user class
private int userId;
private String name;
private String city;
// Constructor
public User(int userId, String name,
String city)
{
this.userId = userId;
this.name = name;
this.city = city;
}
// Getters of the user class
public int getUserId() { return userId; }
public String getName() { return name; }
public String getCity() { return city; }
// Overriding the toString method
// to return the custom string
@Override
public String toString()
{
return "User [userId = "
+ userId + ", name = "
+ name + ", city = "
+ city + "]";
}
}
class GFG {
// Function to convert the User
// to the map
public static Map toMap(User user1, User user2,
User user3)
{
Map<Integer, User> userMap
= Arrays.asList(user1, user2, user3)
.stream()
.collect(Collectors.toMap(
user
-> user.getUserId(),
user -> user));
return userMap;
}
// Driver code
public static void main(String[] args)
{
// Creating users
User user1
= new User(1, "User1", "Pune");
User user2
= new User(2, "User2", "Mumbai");
User user3
= new User(3, "User3", "Nagpur");
System.out.println(toMap(user1, user2,
user3));
}
}
Output:
{1=User [userId = 1, name = User1, city = Pune], 2=User [userId = 2, name = User2, city = Mumbai], 3=User [userId = 3, name = User3, city = Nagpur]}
The
groupingBycollector takes one function as input and creates a group of stream objects using that function. The following are the examples to convert a stream into a map using groupingBy collector.
// Java program to convert the User[]
// into Map<city, List<User>>
import java.util.Arrays;
import java.util.Map;
import java.util.List;
import java.util.stream.*;
// Implementing the User class
public class User {
// Parameters of the user class
private int userId;
private String name;
private String city;
// Constructor of the User class
public User(int userId, String name,
String city)
{
this.userId = userId;
this.name = name;
this.city = city;
}
// Getter functions
public int getUserId() { return userId; }
public String getName() { return name; }
public String getCity() { return city; }
// Overriding the toString() method
// to create a custom function
@Override
public String toString()
{
return "User [userId = "
+ userId + ", name = "
+ name + ", city = "
+ city + "]";
}
}
class GFG {
// Function to convert the user
// object to the map
public static Map toMap(User user1,
User user2,
User user3,
User user4,
User user5)
{
Map<String, List<User> >
cityUserListMap
= Arrays.asList(user1, user2, user3,
user4, user5)
.stream()
.collect(Collectors.groupingBy(
User::getCity));
return cityUserListMap;
}
// Driver code
public static void main(String[] args)
{
// Creating new users
User user1
= new User(1, "User1", "Pune");
User user2
= new User(2, "User2", "Mumbai");
User user3
= new User(3, "User3", "Nagpur");
User user4
= new User(4, "User4", "Pune");
User user5
= new User(5, "User5", "Mumbai");
System.out.println(toMap(user1, user2,
user3, user4,
user5));
}
}
Output:
{Nagpur=[User [userId = 3, name = User3, city = Nagpur]], Pune=[User [userId = 1, name = User1, city = Pune], User [userId = 4, name = User4, city = Pune]], Mumbai=[User [userId = 2, name = User2, city = Mumbai], User [userId = 5, name = User5, city = Mumbai]]}
// Java program to convert User[]
// into Map<city, countOfUser>
import java.util.Arrays;
import java.util.Map;
import java.util.stream.*;
// Implementing the user class
public class User {
// Parameters of the user class
private int userId;
private String name;
private String city;
// Constructor
public User(int userId, String name,
String city)
{
this.userId = userId;
this.name = name;
this.city = city;
}
// Getter functions
public int getUserId() { return userId; }
public String getName() { return name; }
public String getCity() { return city; }
// Overriding the toString() method
// to create a custom function
@Override
public String toString()
{
return "User [userId = "
+ userId + ", name = "
+ name + ", city = "
+ city + "]";
}
}
class GFG {
public static Map toMap(User user1,
User user2,
User user3,
User user4,
User user5)
{
Map<String, Long>
cityUserCountMap
= Arrays.asList(user1, user2, user3,
user4, user5)
.stream()
.collect(
Collectors.groupingBy(
User::getCity,
Collectors.counting()));
return cityUserCountMap;
}
// Driver code
public static void main(String[] args)
{
// Creating new users
User user1
= new User(1, "User1", "Pune");
User user2
= new User(2, "User2", "Mumbai");
User user3
= new User(3, "User3", "Nagpur");
User user4
= new User(4, "User4", "Pune");
User user5
= new User(5, "User5", "Mumbai");
System.out.println(toMap(user1, user2,
user3, user4,
user5));
}
}
Output:
{Nagpur=1, Pune=2, Mumbai=2}
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4