In this article we are going to see about how to convert a List with duplicate key into a Map using Java 8 with example program.
ListToMap.java
Output
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) { ListstockMarket = 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")); stockMarket.add(new StockMarket(5, "IOB", "HATHWAY","Nifty 50","Nifty 100")); 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); }); // IOB is duplicate key here - it will throw error Map resultById1 = stockMarket.stream().collect( Collectors.toMap(StockMarket::getEquity, StockMarket::getId)); //To solve duplicate key error //(oldValue, newValue) -> oldValue) - If the key is duplicated, do you prefer oldKey or newKey? resultById1 = stockMarket.stream().collect( Collectors.toMap(StockMarket::getEquity, StockMarket::getId, (oldValue, newValue) -> oldValue)); } }
Output
Method 1 :
1 : RCOM
2 : IOB
3 : RIL
4 : IDFC
5 : IOB
Exception in thread "main" java.lang.IllegalStateException: Duplicate key 2
at java.util.stream.Collectors.lambda$throwingMerger$114(Collectors.java:133)
at java.util.HashMap.merge(HashMap.java:1245)
at java.util.stream.Collectors.lambda$toMap$172(Collectors.java:1320)
at java.util.stream.ReduceOps$3ReducingSink.accept(ReduceOps.java:169)
at java.util.ArrayList$ArrayListSpliterator.forEachRemaining(ArrayList.java:1374)
at java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:481)
at java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:471)
at java.util.stream.ReduceOps$ReduceOp.evaluateSequential(ReduceOps.java:708)
at java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
at java.util.stream.ReferencePipeline.collect(ReferencePipeline.java:499)
at com.javatutorialcorner.java8.ListToMap.main(ListToMap.java:26)
0 comments:
Post a Comment