Java 8 Comparing Two object Structure using Streams and lambda

Java 8 Compare Object



Parent
            -->  Child
                              -->  School

Try to compare this using Stream 8 Java and Lambda

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collector;
import java.util.stream.Collectors;

public class Test1 {

public static void main(String args[]) {

P p1 = new P();
p1.setFn("pfn1");
List<C> cobj1 = new ArrayList<C>();
C c1 = new C();
c1.setFn("cfn1");
List<S> sobj1 = new ArrayList<S>();
c1.setObj(sobj1);

S s1 = new S();
s1.setN("sn1");
sobj1.add(s1);
cobj1.add(c1);
p1.setObj(cobj1);

P p2 = new P();
p2.setFn("pfn2");
List<C> cobj2 = new ArrayList<C>();
C c2 = new C();
c2.setFn("cfn2");
List<S> sobj2 = new ArrayList<S>();
c2.setObj(sobj2);
S s2 = new S();
s2.setN("sn2");
sobj2.add(s2);
cobj2.add(c2);
p2.setObj(cobj2);

List<P> plist = new ArrayList<P>();
plist.add(p1);

List<P> plist2 = new ArrayList<P>();
plist2.add(p2);

List<String> l1 = plist.stream()
.map(x -> x.getFn() + " " + x.getObj().stream().map(y -> y.getFn()).findAny().get() + " "
+ x.getObj().stream().map(y -> y.getObj())
.map(z -> z.stream().map(a -> a.getN()).findAny().get()).findAny().get())
.collect(Collectors.toList());

List<String> l2 = plist2.stream()
.map(x -> x.getFn() + " " + x.getObj().stream().map(y -> y.getFn()).findAny().get() + " "
+ x.getObj().stream().map(y -> y.getObj())
.map(z -> z.stream().map(a -> a.getN()).findAny().get()).findAny().get())
.collect(Collectors.toList());

System.out.println(l1.containsAll(l2));

}

}




Comments

Popular posts from this blog

Apache Karaf + Pax CDI OSGI Service

OSGI + PAX CDI + Drools + Karaf

Apache CXF CDI in Karaf (Using CXF and CDI 1.1 (JSR-346))