Java8GroupingBy.java

package com.javatutsworld.stream;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

public class Java8GroupingBy {

    public static void main(String args[]) {

        List<AccountDetails> acctDetailsList = new ArrayList();
        acctDetailsList.add(new AccountDetails("Saaim", "00266767", 2000));
        acctDetailsList.add(new AccountDetails("Saaim", "00266767", 2000));
        acctDetailsList.add(new AccountDetails("Saaim", "00266767", 2000));
        acctDetailsList.add(new AccountDetails("Sudhir", "00266788", 30000));
        acctDetailsList.add(new AccountDetails("Gaurav", "00266798", 30000));
        acctDetailsList.add(new AccountDetails("Gaurav", "00266798", 90000));
        acctDetailsList.add(new AccountDetails("Ram", "00266799", 50000));
        acctDetailsList.add(new AccountDetails("Ram", "00266799", 50000));
        acctDetailsList.add(new AccountDetails("Ram", "00266799", 2000));
        List<String> listName = new ArrayList<>();
        List<Integer> listAmount = new ArrayList<>();

        acctDetailsList.forEach(act -> {
            listName.add(act.getName());
            listAmount.add(act.getAmount());
        });

        // groupingBy on name
        Map<String, Long> groupName = listName.stream().collect(
                Collectors.groupingBy(Function.identity(),
                        Collectors.counting()));

        System.out.println(groupName);

        // groupingBy on amount
        Map<Integer, Long> groupAmount = listAmount.stream().collect(
                Collectors.groupingBy(Function.identity(),
                        Collectors.counting()));

        System.out.println(groupAmount);


    }
}

AccountDetails.java

package com.javatutsworld.stream;

public class AccountDetails {
    private String name;
    private String accountNumber;
    private Integer amount;

    public AccountDetails(String name, String accountNumber, Integer amount) {
        super();
        this.name = name;
        this.accountNumber = accountNumber;
        this.amount = amount;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAccountNumber() {
        return accountNumber;
    }

    public void setAccountNumber(String accountNumber) {
        this.accountNumber = accountNumber;
    }

    public Integer getAmount() {
        return amount;
    }

    public void setAmount(Integer amount) {
        this.amount = amount;
    }


}

Output:

{Saaim=3, Gaurav=2, Sudhir=1, Ram=3}
{50000=2, 30000=2, 2000=4, 90000=1}