package com;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
public class GroupByDemoInJava8
{
public static void main(String args[]) throws IOException
{
List<Person> people = new ArrayList<>(); // Date Format is MM/DD/YYYY
people.add(new Person("John", "London", 21 , "2/1/18"));
people.add(new Person("Swann", "London", 21 , "3/5/18" ));
people.add(new Person("Kevin", "London", 23 , "3/12/18"));
people.add(new Person("Monobo", "Tokyo", 23 , "4/18/18"));
people.add(new Person("Sam", "Paris", 23 , "7/12/18"));
people.add(new Person("Nadal", "Paris", 31, "4/2/19"));
Map<String,List<Person>> personByCity = new HashMap<>();
SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yy");
personByCity = people.stream() .collect(Collectors.groupingBy(sdf.parse(Person::getDateOfBirth)));
for (Map.Entry<String,List<Person>> entry : personByCity.entrySet()) {
System.out.println("Key = " + entry.getKey());
System.out.println("Value = " + entry.getValue());
}
}
}
class Person
{
private String name;
private String city; private int age; private String dateOfBirth;
public String getDateOfBirth() {
return dateOfBirth;
}
public void setDateOfBirth(String dateOfBirth) {
this.dateOfBirth = dateOfBirth;
}
public Person(String name, String city, int age , String dateOfBirth)
{
this.name = name; this.city = city; this.age = age;this.dateOfBirth=dateOfBirth;
}
public String getName()
{ return name; }
public void setName(String name)
{ this.name = name; }
public String getCity()
{ return city; }
public void setCity(String city)
{ this.city = city; } public int getAge() { return age; } public void setAge(int age) { this.age = age; }
@Override
public String toString() {
return "Person [name=" + name + ", city=" + city + ", age=" + age + "]";
}
}
Unfortunately, I get the following compilation error
- The method parse(String) in the type DateFormat is not applicable for the arguments (Person::getDateOfBirth)
- The target type of this expression must be a functional interface
Firstly you say the dates are in the format MM/DD/YYYY, but in fact they are not. Your data should be defined:
List<Person> people = new ArrayList<>(); // Date Format is MM/DD/YYYY
people.add(new Person("John", "London", 21, "02/01/2018"));
people.add(new Person("Swann", "London", 21, "03/05/2018"));
people.add(new Person("Kevin", "London", 23, "03/12/2018"));
people.add(new Person("Monobo", "Tokyo", 23, "04/18/2018"));
people.add(new Person("Sam", "Paris", 23, "07/12/2018"));
people.add(new Person("Nadal", "Paris", 31, "04/02/2019"));
Secondly do not use SimpleDateFormat
it is long outdated, use DateTimeFormatter
and LocalDate
from java 1.8 instead:
Map<LocalDate, List<Person>> personByCity = new HashMap<>();
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("MM/dd/yyyy");
personByCity = people.stream()
.collect(Collectors.groupingBy(p -> LocalDate.parse(p.getDateOfBirth(), dtf)));