Is there any other way to refactor below code in single line in java 8. To get final output map I am performing two phase operation.
class Pair<X, Y> {
public X first;
public Y second;
public Pair(X first, Y second) {
this.first = first;
this.second = second;
}
//.... other stuff
}
class InvHistory{
private Integer sysId;
private Integer sourceId;
public InvHistory(Integer sysId, Integer sourceId) {
this.sysId = sysId;
this.sourceId = sourceId;
}
//... other stuff
}
public static void main(String[] args) {
List<InvHistory> invHisLst=Arrays.asList(new InvHistory(1,101),new InvHistory(2,102),new InvHistory(3,102));
Map<Integer, List<InvHistory>> grpMap=invHisLst.stream().collect(Collectors.groupingBy(InvHistory::getSourceId));
System.out.println("Group Map:"+grpMap);
Map<Integer,Pair<Boolean,List<InvHistory>>> newMap=new HashMap<>();
grpMap.forEach((key,value)->{
Pair<Boolean,List<InvHistory>> accociatedValue=new Pair<>(Boolean.TRUE,value);
newMap.put(key, accociatedValue);
});
System.out.println(newMap);
}
thanks in advance