-3

I have List<Map<String, String>> that have family member details. Depending on added family member the rest details will be processed. There can be other family member like uncle, aunt, niece... etc. These records can be single or multiple.

  | Add_Family_Member | Full_Name     | Date_Of_Birth | Gender |
  | Sibling           | Sibling name  | 12-12-1990    | Male   |
  | Sibling           | Sibling name2 | 12-12-1990    | Male   |
  | Sibling           | Sibling name2 | 12-12-1990    | Male   |
  | Child             | Child name    | 12-12-2010    | Male   |
  | Child             | Child name2   | 12-12-2000    | Female |
  | Spouse            | Spouse name   | 12-12-1990    | Male   |

So how can I count Add_Family_Member i.e. Sibling count 3, Child count 2 and spouse count 1.

DeepakVerma
  • 179
  • 1
  • 3
  • 14
  • So you basically need group by? – Alexey R. Oct 29 '22 at 12:06
  • how are you going to fit that record into a hashmap? – Tan Yu Hau Sean Oct 29 '22 at 12:10
  • Please, share your **attempt** and specify the problem you've encountered. Every question on StackOverflow is expected to demonstrate an effort. See [How do I ask a good question?](https://stackoverflow.com/help/how-to-ask) and [Why is "Can someone help me?" not an actual question?](https://meta.stackoverflow.com/questions/284236/why-is-can-someone-help-me-not-an-actual-question) – Alexander Ivanchenko Oct 29 '22 at 12:14
  • I dont know why you use List> when you could use a custom class/Record to represent each record instead. – Tan Yu Hau Sean Oct 29 '22 at 12:16
  • Try looking at [Count occurrences of value in a map](https://stackoverflow.com/questions/44500446/count-occurrences-of-value-in-a-map?adlt=strict&toWww=1&redig=C18AF74D8EC54E0FB8B6FAF4A71EDBDD). – Alias Cartellano Oct 29 '22 at 16:33

1 Answers1

0

Using Java 8 stream

You can try using stream groupingBy feature to get the count of each member type shown as below:

Logic:

Here, I have created a POJO class FamilyMember with some attributes as given in the problem statement and created a list of FamilyMember and then by using stream operation and using groupingBy, grouped the data by memberType and using Collectors.counting getting the count of each memberType.

Suggestions:

As mentioned in the problem statement, the given input is in the form of List<Map<String, String>> so instead of Map you can use a POJO class to hold the data for each member as shown in my implementation. And moreover, how you are holding this kind of structure in the form of Map<String,String>

Code:

    public class Test {

       public static void main(String[] args) {

        FamilyMember member1 = new FamilyMember("Sibling","Sibling name",
                LocalDate.of(1990,12,12),"Male");
        FamilyMember member2 = new FamilyMember("Sibling","Sibling name2",
                LocalDate.of(1990,12,12),"Male");
        FamilyMember member3 = new FamilyMember("Sibling","Sibling name3",
                LocalDate.of(1990,12,12),"Male");
        FamilyMember member4 = new FamilyMember("Child","Child name",
                LocalDate.of(2010,12,12),"Male");
        FamilyMember member5 = new FamilyMember("Child","Child name2",
                LocalDate.of(2000,12,12),"Female");
        FamilyMember member6 = new FamilyMember("Spouse","Spouse name",
                LocalDate.of(1990,12,12),"Male");

        List<FamilyMember> listOfFamilyMember = Arrays.asList(member1,member2,
                                              member3,member4,member5,member6);

        Map<String,Long> countMembers = listOfFamilyMember.stream()
                .collect(Collectors.groupingBy(FamilyMember::getMemberType,
               Collectors.counting()));

        System.out.println(countMembers);
    }
}

FamilyMember.java

public class FamilyMember {

    private String memberType;
    private String fullName;
    private LocalDate dateOfBirth;
    private String gender;

    public FamilyMember(String memberType, String fullName, 
                        LocalDate dateOfBirth, String gender) {
        this.memberType = memberType;
        this.fullName = fullName;
        this.dateOfBirth = dateOfBirth;
        this.gender = gender;
    }
    //getters and setters
}

Output:

{Spouse=1, Sibling=3, Child=2}
GD07
  • 1,117
  • 1
  • 7
  • 9