Java 8 convert List to Map example

In this article we are going to see about how to convert a List of objects into a Map using Java 8 with example program

StockMarket.java

package com.ehowtonow.java8;

public class StockMarket {

 private int id;
 private String equity;
 private String intraDay;
 private String options;
 private String future;

 public StockMarket(int id, String equity, String intraDay, String options,
   String future) {
  this.id = id;
  this.equity = equity;
  this.intraDay = intraDay;
  this.options = options;
  this.future = future;
 }

 public int getId() {
  return id;
 }


 public void setId(int id) {
  this.id = id;
 }
 
 public String getEquity() {
  return equity;
 }

 public void setEquity(String equity) {
  this.equity = equity;
 }

 public String getIntraDay() {
  return intraDay;
 }

 public void setIntraDay(String intraDay) {
  this.intraDay = intraDay;
 }

 public String getOptions() {
  return options;
 }

 public void setOptions(String options) {
  this.options = options;
 }

 public String getFuture() {
  return future;
 }

 public void setFuture(String future) {
  this.future = future;
 }


}

ListToMap.java

package com.ehowtonow.java8;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class ListToMap {

 public static void main(String[] args) {
   List stockMarket = new ArrayList<>();
   stockMarket.add(new StockMarket(1, "RCOM", "HATHWAY","SBIN","DVISLAB"));
   stockMarket.add(new StockMarket(2, "IOB", "INDBank","Bank Nifty","Nifty 100"));
   stockMarket.add(new StockMarket(3, "RIL", "ICICIBank","Nifty 50","Sensex"));
   stockMarket.add(new StockMarket(4, "IDFC", "HDFC","SBIN","DVISLAB"));

  Map resultById = stockMarket.stream().collect(
    Collectors.toMap(StockMarket::getId, StockMarket::getEquity));
  
  System.out.println("Method 1  : ");
  resultById.forEach((k, v) -> {
   System.out.println(k + " : " +v);
  });
  
  Map resultById1 = stockMarket.stream().collect(
    Collectors.toMap(x -> x.getId(), x -> x.getEquity()));

  System.out.println("Method 2 : ");
  resultById1.forEach((k, v) -> {
   System.out.println(k + " : " +v);
  });
 }

}

Output
Method 1 :
1 : RCOM
2 : IOB
3 : RIL
4 : IDFC
Method 2 :
1 : RCOM
2 : IOB
3 : RIL
4 : IDFC


Ask your questions in eHowToNow Forum

Post your technical, non-technical doubts, questions in our site. Get answer as soon as possible, meanwhile you can help others by answering, unanswered questions.
To Ask new Question : Ask Question
Check our existing discussions : Questions & Answers

  1. Java 8 – filter the null values from Stream Example
  2. Java – Collectors Class
  3. Java 8 Stream Collectors groupingBy Example
  4. Java 8 Stream Collectors groupingBy with sort Example
  5. Java – IntSupplier Interface
  6. Java – LongConsumer Interface
  7. Java – IntPredicate Interface
  8. Java – ToDoubleFunction Interface
  9. Java – DoubleToLongFunction Interface
  10. Java 8 convert List to Map with Duplicate Keys example
  11. Java – ObjIntConsumer Interface
  12. Java 8 Convert a Stream to List
  13. Java 8 Sort Map by Key
  14. Java – IntBinaryOperator Interface
  15. Java – Consumer Interface

Be the first to comment

Leave a Reply