AccountDetails.java

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

}

SortMapByKeyAndValue.java

package com.javatutsworld.stream;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class SortMapByKeyAndValue {

    public static void main(String[] argv) {

        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> map = acctDetailsList.stream().collect(
                Collectors.toMap(AccountDetails::getName,
                        AccountDetails::getAmount));

        System.out.println("output without sort: ");
        System.out.println(map);
        System.out.println("\t");

        // sorting logic by keys and return a new LinkedHashMap
        Map<String, Integer> resultKey = map.entrySet().stream()
                .sorted(Map.Entry.comparingByKey())
                .collect(Collectors.toMap(Map.Entry::getKey,Map.Entry::getValue,
                                (oldValue, newValue) -> oldValue,LinkedHashMap::new));

        System.out.println("output with sort by key: ");
        System.out.println(resultKey);

        // sorting logic by values and return a new LinkedHashMap
        Map<String, Integer> resultValue = map.entrySet().stream()
                .sorted(Map.Entry.comparingByValue(Comparator.naturalOrder()))
                .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue,
                        (oldValue, newValue) -> oldValue, LinkedHashMap::new));

        System.out.println("\t");
        System.out.println("output with sort by values: ");
        System.out.println(resultValue);
    }

}

Output:

output without sort: 
{Aariz=25000, Saaim=2000, Gaurav=35000, Sudhir=30000, Ram=4000}

output with sort by key: 
{Aariz=25000, Gaurav=35000, Ram=4000, Saaim=2000, Sudhir=30000}

output with sort by values: 
{Saaim=2000, Ram=4000, Aariz=25000, Sudhir=30000, Gaurav=35000}

References: Please see here for more details on mkyong.com