Java 8 Stream and Collector

Recently I need to generate a report based on a database query result, I ended up using Java 8 stream and collector and was surprised how easy it was.


To generate a report on each person’s home country based on his/her city stored in a CSV file (“D:\\My Data\\test.txt”) below:

John Smith, 12345, New York
Mary Tommy, 23456, Toronto


public class Test {

 public static void main(String[] args) throws IOException {

 String inputDataFileName = "D:\\My Data\\test.txt";

 Function<String, Person> parseLine = s -> {
 try {
 String[] tokens = s.split(",");
 return new Person(tokens[0].trim(), tokens[1].trim(), tokens[2].trim());
 } catch (Exception e) {
 return null;

 Function<Person, String> findCountry = p -> {
 switch (p.getCity()) {
 case "New York":
 return "USA";
 case "Toronto":
 return "Canada";
 return "Unknown";

 Map<String, String> collect =
 filter(s -> !s.contains("---"))
 .map(s -> parseLine.apply(s))
 .filter(p -> p != null)
 .collect(Collectors.toMap(Person::getName, p -> findCountry.apply(p)));
 for (String key : collect.keySet()) {
 System.out.println(key + " is from " + collect.get(key));


  1. Line 7 and 16 define two named functions for easier debug. If lambda is used, it is not easy to see what’s going on in IDE;
  2. Line 28 reads the text file into a stream
  3. Line 29 filters the stream
  4. Line 30 transforms the original string stream into a person object stream
  5. Line 31 filters the person object stream
  6. Line 32 uses the build-in Collectors.toMap to convert the stream into a map, where you can see the key of the map is simply the person’s name and the value is computed by the function findCountry.
This entry was posted in Java, Programming and tagged , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s