Posts

Showing posts from April, 2019

Java 8 streams, grouping by transformed stream object

Following example demonstrates, how to use Java streams to group an object based on its property and transforming the object to some other Class before they are collected in a list. In the following example, we create a list of objects of Class A and stream the list to produce a map based on a property of Class A but containing a list of objects of Class B. public class A { public A(String a) { super(); this.a = a; } private String a; public String getA() { return a; } public void setA(String a) { this.a = a; } } public class B { public B(String a) { super(); this.a = a; } private String a; public String getA() { return a; } public void setA(String a) { this.a = a; } } public class Main { public static void main(String a[]) { A a1 =