Posts

Showing posts from 2018

Chain of Command

public interface Command<T>{     boolean execute(T context); } public class FirstCommand implements Command<Map<String, Object>>{     public boolean execute(Map<String, Object> context){         //doing something in here     } } public class SecondCommand implements Command<Map<String, Object>>{     public boolean execute(Map<String, Object> context){         //doing something in here     } } public class Chain {     public List<Command> commands;     public Chain(Command... commands){         this.commands = Arrays.asList(commands);     }     public void start(Object context){         for(Command command : commands){             boolean shouldStop = command.execute(context);             i...

Builder Pattern

public class Product {     private String id;     private Product(Builder builder) {         setId(builder.id);     }     public static Builder newProduct() {         return new Builder();     }     public String getId() {         return id;     }     public void setId(String id) {         this.id = id;     }     public static final class Builder {         private String id;         private Builder() {         }         public Builder id(String id) {             this.id = id;             return this;         }         public Product build() {             return new Produc...

Singleton in Enum

Singleton using Java 8 public enum Singleton {   INSTANCE;      private Singleton(){   }   public Test getObject(){     return new Test();   } } Usage: Singleton.INSTANCE.getObject();

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] + ...