Monday 7, Apr 2025
We are moved to new domain
Click -> www.ehowtonow.com
Sunday, 24 September 2017

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.javatutorialcorner.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.javatutorialcorner.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

Shop and help us

Flipkart Offer Snapdeal offer Amazon.in offer Amazon.com offer

Related Posts:

  • Java Mail API - Introduction In this tutorials we are going to see introduction about Java Mail API.The Java Mail API used to send email programmatically.Java Mail API is platform and protocol independent API for sending and receiving email programmat… Read More
  • How to Convert Object to JSON String using GSONJSON – JavaScript Object Notation.JSON is the simple, easy and lightweight framework for data exchange.It is good replacement for XML format StringGSON GSON is java library for create JSON String. It was created for use insid… Read More
  • Email Receiver with AttachmentIn this tutorials we are going to see how to receive the email with attachment using java program.I used gmail SMTP server for receive email, you can use any SMTP server(provided by your host provider).The javax.mail.*; packa… Read More
  • Sending Simple Email Using Java Mail APIIn this tutorials we are going to see how to send the simple email using java program.I used gmail SMTP server for sending email, you can use any SMTP server(provided by your host provider).The javax.mail.*; package used for … Read More
  • Read JSON using GSON Stream In this tutorial we are going to see how to read JSON Data from file using GSON 1. Create project called JSONExample. 2. Create package called com.javatutorialscorner.gson  3. Create java class called GSONStreamR… Read More
  • Blogger Comments
  • Facebook Comments
  • Disqus Comments

0 comments:

Post a Comment

Item Reviewed: Java 8 convert List to Map example Rating: 5 Reviewed By: eHowToNow