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] + "," + x[5])));

}

br.close();

}

// Method to filter Distinct Key Value.
public static <T> Predicate<T> distinctByKey(Function<? super T, Object> keyExtractor) {
Map<Object, Boolean> map = new ConcurrentHashMap<>();
return t -> map.putIfAbsent(keyExtractor.apply(t), Boolean.TRUE) == null;
}

}

Comments

Popular posts from this blog

Apache Karaf + Pax CDI OSGI Service

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

OSGI + PAX CDI + Drools + Karaf