Created
May 14, 2020 07:22
-
-
Save anbusampath/3b4132fdd74a5243e8350247e89686de to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Arrays; | |
import java.util.List; | |
import java.util.Map; | |
import java.util.stream.Collectors; | |
public class ListToMap { | |
public static void main(String[] args) { | |
Transaction t1 = new Transaction("Submission", 1233L, "Initiated", "Failed"); | |
Transaction t2 = new Transaction("Submission", 1233L, "Quoted", "Passed"); | |
Transaction t3 = new Transaction("Submission", 1233L, "Submitted", "Passed"); | |
Transaction t4 = new Transaction("Renewal", 1366L, "Initiated", "Passed"); | |
Transaction t5 = new Transaction("Renewal", 1366L, "Quoted", "Failed"); | |
List<Transaction> trainsactions = Arrays.asList(t1, t2, t3, t4, t5); | |
Map<Long,List<Event>> eventsbyTransaction = trainsactions.stream() | |
.collect(Collectors.groupingBy(Transaction::getNumber, | |
Collectors.mapping( t-> | |
new Event(t.getType(), | |
t.getEventName(), | |
t.getStatus()),Collectors.toList()))); | |
System.out.println("Result : " + eventsbyTransaction); | |
} | |
} | |
class Transaction { | |
String type; | |
Long number; | |
String eventName; | |
String status; | |
public Transaction(String type, Long number, String eventName, String status) { | |
super(); | |
this.type = type; | |
this.number = number; | |
this.eventName = eventName; | |
this.status = status; | |
} | |
public String getType() { | |
return type; | |
} | |
public void setType(String type) { | |
this.type = type; | |
} | |
public Long getNumber() { | |
return number; | |
} | |
public void setNumber(Long number) { | |
this.number = number; | |
} | |
public String getEventName() { | |
return eventName; | |
} | |
public void setEventName(String eventName) { | |
this.eventName = eventName; | |
} | |
public String getStatus() { | |
return status; | |
} | |
public void setStatus(String status) { | |
this.status = status; | |
} | |
} | |
class Event { | |
String type; | |
String eventName; | |
String status; | |
public Event(String type, String eventName, String status) { | |
super(); | |
this.type = type; | |
this.eventName = eventName; | |
this.status = status; | |
} | |
public String getType() { | |
return type; | |
} | |
public void setType(String type) { | |
this.type = type; | |
} | |
public String getEventName() { | |
return eventName; | |
} | |
public void setEventName(String eventName) { | |
this.eventName = eventName; | |
} | |
public String getStatus() { | |
return status; | |
} | |
public void setStatus(String status) { | |
this.status = status; | |
} | |
@Override | |
public String toString() { | |
return "Event [type=" + type + ", eventName=" + eventName + ", status=" + status + "]"; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment