AccountDetails

package com.javatutsworld.stream;

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

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

    public int getIfscCode() {
        return ifscCode;
    }

    public void setIfscCode(int ifscCode) {
        this.ifscCode = ifscCode;
    }

    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;
    }

}

Java8MapofList.java

package com.javatutsworld.stream;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class Java8MapofList  {

    public static void main(String args[]) {

        List<AccountDetails> acctDetailsList = new ArrayList();
        acctDetailsList.add(new AccountDetails(000,"Saaim", "00266767", 2000));
        acctDetailsList.add(new AccountDetails(001,"Sudhir", "00266788", 30000));
        acctDetailsList.add(new AccountDetails(002,"Gaurav", "00266798", 35000));
        acctDetailsList.add(new AccountDetails(003,"Ram", "00266799", 4000));
        acctDetailsList.add(new AccountDetails(004,"Aariz", "00266730", 25000));

        String[] str = {"PNB","Citi","SBI","BOA", "ICICI"};
        List<String> bankName=Arrays.asList(str);

        Map<String, Object> result = acctDetailsList.stream()
                .collect(Collectors.toMap(AccountDetails::getName, 
                        (colMap) -> bankName.get(colMap.getIfscCode())));
        System.out.println(result);

    }
}

Here is the acctDetailsList is the first object list of AccountDetails and bankName is the second list of String. We have mapped ifscCode from first list with second list. Here is the output result of two mapping list.

Output:

{Aariz=ICICI, Saaim=PNB, Gaurav=SBI, Sudhir=Citi, Ram=BOA}

How to update inner and outer maps in Java 8

package com.javatutsworld.stream;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class Java8MapofList  {

    public static void main(String args[]) {

        List<AccountDetails> acctDetailsList = new ArrayList();
        acctDetailsList.add(new AccountDetails(000,"Saaim", "00266767", 2000));
        acctDetailsList.add(new AccountDetails(001,"Sudhir", "00266788", 30000));
        acctDetailsList.add(new AccountDetails(002,"Gaurav", "00266798", 35000));
        acctDetailsList.add(new AccountDetails(003,"Ram", "00266799", 4000));
        acctDetailsList.add(new AccountDetails(004,"Aariz", "00266730", 25000));


        Map<String, Integer> innerMap = acctDetailsList.stream()
                .collect(Collectors.toMap(AccountDetails::getName, AccountDetails::getAmount));

        Map<String, Map<String, Integer>> outerMap = new HashMap<>();
        outerMap.put("Mukarram",innerMap);

        System.out.println("inner hashmap: "+innerMap);
        System.out.println("outer hashmap: "+outerMap);
        System.out.println("\t");

        //add one or more value in the inner hashmap
        outerMap.computeIfAbsent("Mukarram", k -> new HashMap<>()).put("Piyus", 100);
        outerMap.computeIfAbsent("Mukarram",k -> new HashMap<>()).putAll(new HashMap<String, Integer>(){{
                        put("javatpoint", 1000);
                        put("stackoverflow", 50);
                        put("tutorialspoint", 999);
                    }});

        System.out.println("inner hashmap: "+innerMap);
        System.out.println("outer hashmap: "+outerMap);
    }
}

Output:

inner hashmap: {Aariz=25000, Saaim=2000, Gaurav=35000, Sudhir=30000, Ram=4000}
outer hashmap: {Mukarram={Aariz=25000, Saaim=2000, Gaurav=35000, Sudhir=30000, Ram=4000}}

inner hashmap: {javatpoint=1000, Aariz=25000, tutorialspoint=999, Piyus=100, Saaim=2000, Gaurav=35000, Sudhir=30000, Ram=4000, stackoverflow=50}
outer hashmap: {Mukarram={javatpoint=1000, Aariz=25000, tutorialspoint=999, Piyus=100, Saaim=2000, Gaurav=35000, Sudhir=30000, Ram=4000, stackoverflow=50}}

How to filter a map with stream api in Java 8

package com.javatutsworld.stream;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

public class Java8MapofList  {

    public static void main(String args[]) {

        List<AccountDetails> acctDetailsList = new ArrayList();
        acctDetailsList.add(new AccountDetails(000,"Saaim", "00266767", 2000));
        acctDetailsList.add(new AccountDetails(001,"Sudhir", "00266788", 30000));
        acctDetailsList.add(new AccountDetails(002,"Gaurav", "00266798", 35000));
        acctDetailsList.add(new AccountDetails(003,"Ram", "00266799", 4000));
        acctDetailsList.add(new AccountDetails(004,"Aariz", "00266730", 25000));


        Map<String, String> map = acctDetailsList.stream()
                .collect(Collectors.toMap(AccountDetails::getName, AccountDetails::getAccountNumber));

        List<String> list = new ArrayList<>();
        list.add("Sudhir");
        list.add("Gaurav");
        list.add("Gaurav1");

        List<String> listMatchValues = list.stream().map(map::get)
                .filter(Objects::nonNull)
                .collect(Collectors.toList());

        System.out.println("list Match Values: "+listMatchValues);

    }
}

Output:

list Match Values: [00266788, 00266798]

Reference: Please see here for more details on stackoverflow