CoderSathi
  • Tutorial
    • Java Tutorial
    • Swing Tutorial
    • JDBC Tutorial
    • Java String Tutorial
    • Servlet and JSP Tutorial
  • Mongo DB
  • AWS
  • DevOps
  • Linux
  • Git
Home > How to > How to convert List to Map in Java?

Java Tutorial

  • Introduction
    • What is Java
    • History Of Java
    • Install Java
    • What is JVM
    • JDK vs JRE vs JVM
    • Java Bytecode
    • OOP vs POP
    • Compile and Run Java
  • Tokens, Expressions and Control Structures
    • Primitive data types
    • Integers
    • Floating Points
    • Characters
    • Booleans
    • User Defined Data Type
    • Declarations
    • Constants
    • Identifiers
    • Literals
    • Type Conversion and Casting
    • Variables
    • Default Variable Initialization
    • Command Line Arguments
    • Arrays of Primitive Types
    • Comment Syntax
    • Garbage Collection
    • Expressions
    • Operators
    • Arithmetic Operator
    • Bitwise and Shift Operator
    • Comparison or Relational Operators
    • Logical Operators
    • Assignment Operators
    • Ternary Operator
    • Increment and Decrement Operator
    • Control Statements
  • OOP Concepts
    • Class and Object
    • Create Class Instance
    • Method
    • Abstraction
    • Encapsulation
    • this keyword
    • Constructor
    • Pass by Value
    • Access Modifier/Control
    • Polymorphism
    • Method Overloading vs Method Overriding
    • Recursion
    • Nested and Inner Class
  • Inheritance and Packaging
    • Inheritance
    • extends Keyword
    • super Keyword
    • Object Class
    • Abstract class
    • Final Class
    • Java Package
    • Interface
  • Handling Error/ Exceptions
    • What is Exception
    • Exception Handling Keywords
    • Common Java Errors
    • User Defined Exception
    • Throwing and re-throwing Exception
    • finally Block
  • Strings
    • Java String Tutorial
  • Threads
    • Introduction
    • Create Thread
    • Thread Lifecycle
    • Thread Priority
    • Thread Synchronization
    • Inner Thread Communication
    • Thread Deadlock
  • IO and Streams
    • java.io Package
    • Files and Directories
    • Byte Stream
    • Character Stream
    • Console Input and Output
    • Serializable and Deserializable
  • Core Packages
    • java.lang Package
    • Math
    • Wrapper Classes
    • java.lang.Number
    • Double
    • Float
    • Integers
    • java.lang.Byte
    • java.lang.Short
    • java.lang.Long
    • java.lang.Character
    • java.lang.Boolean
    • java.util package
    • Vector Class
    • Stack Class
    • Dictionary Class
    • Hashtable
    • Enumeration or Enum
    • Generate Random Number
  • Holding Collection of Data
    • Arrays
    • Map
    • List
    • Set
    • Collection Interface
    • Collections Class
    • ArrayList
    • HashSet
    • TreeSet
    • Comparator
  • Java Bean
    • What is Java Bean
    • Advantages and Disadvantages of Java Bean
    • Java Beans API
    • Introspection
    • Java Bean Properties
    • Bound and Constrained Properties
    • BeanInfo Interface
    • Customizers
    • Java Beans Persistence
    • BeanDescriptor
  • Home

How to convert List to Map in Java?

Learn the concepts, implementation details, and practical steps with a clean developer-focused walkthrough.

Yuba Raj Kalathoki
By Yuba Raj Kalathoki
Last updated: July 1, 2026 ยท 3 min read ยท 0 Comments
Share: in X

As a developer, the need to convert List to Map in Java is a common task. There are mainly two things that we want to achieve while converting to Map.

  • Converting to map by making some property as a Key and an Object as a Value or
  • Grouping the list by some property

In this post, we will learn both ways of converting List to Map in Java.

Let’s take an example of the following class and learn to convert List to Map

public class Customer {
    private Long id;
    private Long name;
    private Country country;
    // Other properties
}

Let’s assume we have a list of customer object like below.

List<Customer> customers;

Converting to map by making some property as a Key and an Object as a Value

Our expected output is the following:

Map<Long, Customer> idCustomerMap;

Before Java 8

Converting List to Map before Java 8 was

Map<Long, Customer> idCustomerMap = new HashMap<>();
for(Customer customer: customers){
    // here making id as a key and customer object as a value
    idCustomerMap.put(customer.getId(), customer);
}

After Java 8

Here, we can use Java Stream API along with Lamda expression.

Map<Long, Customer> idCustomerMap = customers.stream().collect(Collectors.toMap(Customer::getId, customer -> customer));

With these examples above, we have successfully converted a List to a Map.

Now, let’s look at the second approach.

Grouping the list by some property

Our expected output is

Map<Country, List<Customer>> countryCustomersMap;

The above map groups all the customers by country.

Before Java 8

In the below code, we are going to do the following steps inside our loop:

  1. Checking the map whether it contains the key country or not
  2. If the country is found on the map it means it already has a list of customers associated with this country
    1. Then, extract the list of customers associated with the country
    2. Add new customers to that list and
    3. Put this new list to map for the same country as a key
  3. If the country is not found on the map, that means this country is new and w need to add it to map
    1. Then, create a new list of customers
    2. Add new customers to that list
    3. Put this new list to map for the new country as a key

Let’s see the code below.

Map<Country, List<Custome>> countryCustomersMap = new HashMap<>();
for(Customer customer: customers){
    if(countryCustomersMap.containsKey(customer.getCountry())){
        List<Customer> customerList = countryCustomersMap.get(customer.getCountry());
        customerList.add(customer);
        countryCustomersMap.put(customer.getCountry(), customerList);
    }else{
        List<Customer> customerList = new ArrayList<>();
        customerList.add(customer);
        countryCustomersMap.put(customer.getCountry(), customerList);
    }
}

After Java 8

Map<Country, List<Customer>> countryCustomersMap= customers.stream().collect(Collectors.groupingBy(Customer::getId));

Conclusion

We have successfully converted List to Map in Java without using any third-party library. We also learn, how we used to create a Map and the new way of creating it.

Related Posts:

  • Spring Boot Thread Pool Configuration: Optimizing…
  • How to Use AWS CloudFront Signed URLs in Spring Boot?
  • How to Write User Stories and Acceptance Criteria: A…
  • Control Statements in Java
  • Most Frequently Asked Spring Boot Interview…
  • Host Static Website on S3 AWS: Step-by-Step Guide
Tags:java
Was this article helpful?
โ† Previous ArticleCreate an IAM user in AWS
Next Article โ†’What is Java? Exploring Its Key Features, Benefits, and Use Cases

Leave a Comment Cancel reply

You must be logged in to post a comment.

Recent Posts

  • How to implement Passwordless Authentication in Spring Boot: A Step-by-Step Guide
  • How to Use AWS CloudFront Signed URLs in Spring Boot?
  • How to Fix SSH Agent Forwarding on macOS: The Ultimate Guide for Developers
  • How to Read AWS Secrets Manager in Spring Boot (Step-by-Step)
  • How to Fix “Public Key Retrieval is not allowed” MySQL JDBC Error
CoderSathi

Your go-to resource for Java, Spring Boot, Microservices, AWS, and modern development tutorials.

Linkedin

Quick Links

  • About
  • Contact

Popular Topics

  • Java
  • Spring Boot
  • AWS
  • DevOps
  • MongoDB
  • Linux
  • Git
  • How to
ยฉ 2026 CoderSathi. All rights reserved. Privacy Policy ยท Sitemap