Using Group By in Java 8 with Property

user663724 :
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
Nicholas K :

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 LocalDatefrom 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)));

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Get Array of Property, of Property (nested property) using stream Java 8

From Dev

Getting the max of a property using Java 8

From Dev

Java 8 Steam Group By on two properties and get average of third property

From Dev

Can I group by property and map to new object in java 8

From Dev

Group 8x8 blocks using for loops in Java

From Dev

Modify property value of the objects in list using Java 8 streams

From Java

Java 8 Distinct by property

From Dev

How to group by range of values in Java 8 using streams

From Dev

Java 8 - How to group and sum on a list of maps using stream collectors

From Dev

How to group a set of objects into sorted lists using java 8?

From Dev

Java 8. Group a list of values into a list of ranges using Collectors

From Dev

How to group List of Lists using a certain criteria in Java 8

From Dev

Java 8 - How to group and sum on a list of maps using stream collectors

From Dev

Java 8 group by and BigDecimal

From Dev

Remove element with duplicate property based on another property from list using java 8

From Dev

Sorting by property in Java 8 stream

From Dev

Java 8 Group by and append to a set

From Dev

Linq - group by using the elements inside an array property

From Dev

LINQ group by query using reflected property name

From Dev

Group 'calc()' CSS property; using margin shorthand?

From Dev

Linq - group by using the elements inside an array property

From Dev

Java - Group and Sort Array of Objects by property

From Dev

How to find the sum based on some property of objects from List of objects in Java 8 using stream

From Dev

Using Java 8 lambda expressions, filter a List of objects by property of an inner list

From Java

using Java 8 How to filter by list and group by based on filter condition and convert to Map with their count

From Dev

How to Group Objects in a List into other Lists by Attribute using streams & Java 8?

From Dev

using Java 8 How to filter by list and group by based on filter condition and convert to Map with their count

From Dev

Using Java 8 with LibGDX

From Dev

Sort by comparable Bean property in Java 8

Related Related

  1. 1

    Get Array of Property, of Property (nested property) using stream Java 8

  2. 2

    Getting the max of a property using Java 8

  3. 3

    Java 8 Steam Group By on two properties and get average of third property

  4. 4

    Can I group by property and map to new object in java 8

  5. 5

    Group 8x8 blocks using for loops in Java

  6. 6

    Modify property value of the objects in list using Java 8 streams

  7. 7

    Java 8 Distinct by property

  8. 8

    How to group by range of values in Java 8 using streams

  9. 9

    Java 8 - How to group and sum on a list of maps using stream collectors

  10. 10

    How to group a set of objects into sorted lists using java 8?

  11. 11

    Java 8. Group a list of values into a list of ranges using Collectors

  12. 12

    How to group List of Lists using a certain criteria in Java 8

  13. 13

    Java 8 - How to group and sum on a list of maps using stream collectors

  14. 14

    Java 8 group by and BigDecimal

  15. 15

    Remove element with duplicate property based on another property from list using java 8

  16. 16

    Sorting by property in Java 8 stream

  17. 17

    Java 8 Group by and append to a set

  18. 18

    Linq - group by using the elements inside an array property

  19. 19

    LINQ group by query using reflected property name

  20. 20

    Group 'calc()' CSS property; using margin shorthand?

  21. 21

    Linq - group by using the elements inside an array property

  22. 22

    Java - Group and Sort Array of Objects by property

  23. 23

    How to find the sum based on some property of objects from List of objects in Java 8 using stream

  24. 24

    Using Java 8 lambda expressions, filter a List of objects by property of an inner list

  25. 25

    using Java 8 How to filter by list and group by based on filter condition and convert to Map with their count

  26. 26

    How to Group Objects in a List into other Lists by Attribute using streams & Java 8?

  27. 27

    using Java 8 How to filter by list and group by based on filter condition and convert to Map with their count

  28. 28

    Using Java 8 with LibGDX

  29. 29

    Sort by comparable Bean property in Java 8

HotTag

Archive