Posts

Showing posts with the label java8

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.set...

CSV Parsing in Java8 Streams

Java 8 Stream File Processing import java.io.BufferedReader; import java.io.IOException; import java.io.PrintWriter; import java.nio.file.Files; import java.nio.file.Paths; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; import java.util.function.Function; import java.util.function.Predicate; public class Test { public static void main(String[] args) throws IOException { BufferedReader br = Files.newBufferedReader(Paths.get("C:\\Users\\anharayanan\\Desktop\\test.csv")); try (PrintWriter pw = new PrintWriter( Files.newBufferedWriter(Paths.get("C:\\Users\\anharayanan\\Desktop\\test2.csv")))) { br.lines().skip(1).parallel().map(x -> x.split("\\s+", 2)).map(x -> x[0].split("\\$")).filter(x->x.length>=6).filter(distinctByKey(x -> x[0]+x[1]+x[2])) .forEach(x -> pw.println(new String(x[0] + "," + x[1] + "," + x[2] + "," + x[3] + "," + x[4] + ...