博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Java 8 – How to sort a Map
阅读量:5971 次
发布时间:2019-06-19

本文共 4119 字,大约阅读时间需要 13 分钟。

Java 8 – How to sort a Map

1. Quick Explanation

Map result = map.entrySet().stream()
.sorted(Map.Entry.comparingByKey())
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue,
(oldValue, newValue) -> oldValue, LinkedHashMap::new));

2. Sort by KEYS

package com.mkyong.test;

import java.util.HashMap;

import java.util.LinkedHashMap;
import java.util.Map;
import java.util.stream.Collectors;

public class SortByKeyExample {

public static void main(String[] argv) {

Map<String, Integer> unsortMap = new HashMap<>();

unsortMap.put("z", 10);
unsortMap.put("b", 5);
unsortMap.put("a", 6);
unsortMap.put("c", 20);
unsortMap.put("d", 1);
unsortMap.put("e", 7);
unsortMap.put("y", 8);
unsortMap.put("n", 99);
unsortMap.put("g", 50);
unsortMap.put("m", 2);
unsortMap.put("f", 9);

System.out.println("Original...");

System.out.println(unsortMap);

// sort by keys, a,b,c..., and return a new LinkedHashMap

// toMap() will returns HashMap by default, we need LinkedHashMap to keep the order.
Map<String, Integer> result = unsortMap.entrySet().stream()
.sorted(Map.Entry.comparingByKey())
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue,
(oldValue, newValue) -> oldValue, LinkedHashMap::new));

// Not Recommend, but it works.
//Alternative way to sort a Map by keys, and put it into the "result" map
Map<String, Integer> result2 = new LinkedHashMap<>();
unsortMap.entrySet().stream()
.sorted(Map.Entry.comparingByKey())
.forEachOrdered(x -> result2.put(x.getKey(), x.getValue()));

System.out.println("Sorted...");

System.out.println(result);
System.out.println(result2);

}

}

Output

Original

{a=6, b=5, c=20, d=1, e=7, f=9, g=50, y=8, z=10, m=2, n=99}

Sorted...

{a=6, b=5, c=20, d=1, e=7, f=9, g=50, m=2, n=99, y=8, z=10}
{a=6, b=5, c=20, d=1, e=7, f=9, g=50, m=2, n=99, y=8, z=10}

3. Sort by Values

package com.mkyong.test;

package com.mkyong;

import java.util.Comparator;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.stream.Collectors;

public class SortByValueExample {

public static void main(String[] argv) {

Map<String, Integer> unsortMap = new HashMap<>();

unsortMap.put("z", 10);
unsortMap.put("b", 5);
unsortMap.put("a", 6);
unsortMap.put("c", 20);
unsortMap.put("d", 1);
unsortMap.put("e", 7);
unsortMap.put("y", 8);
unsortMap.put("n", 99);
unsortMap.put("g", 50);
unsortMap.put("m", 2);
unsortMap.put("f", 9);

System.out.println("Original...");

System.out.println(unsortMap);

//sort by values, and reserve it, 10,9,8,7,6...

Map<String, Integer> result = unsortMap.entrySet().stream()
.sorted(Map.Entry.comparingByValue(Comparator.reverseOrder()))
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue,
(oldValue, newValue) -> oldValue, LinkedHashMap::new));

//Alternative way
Map<String, Integer> result2 = new LinkedHashMap<>();
unsortMap.entrySet().stream()
.sorted(Map.Entry.<String, Integer>comparingByValue().reversed())
.forEachOrdered(x -> result2.put(x.getKey(), x.getValue()));

System.out.println("Sorted...");

System.out.println(result);
System.out.println(result2);

}

}
Output

Original...

{a=6, b=5, c=20, d=1, e=7, f=9, g=50, y=8, z=10, m=2, n=99}

Sorted...

{n=99, g=50, c=20, z=10, f=9, y=8, e=7, a=6, b=5, m=2, d=1}
{n=99, g=50, c=20, z=10, f=9, y=8, e=7, a=6, b=5, m=2, d=1}

4. Map<Object,Object>

package com.mkyong;

import java.util.LinkedHashMap;

import java.util.Map;
import java.util.Properties;
import java.util.Set;
import java.util.stream.Collectors;

public class DisplayApp {

public static void main(String[] args) {

Properties properties = System.getProperties();

// not easy to sort this
Set<Map.Entry<Object, Object>> entries = properties.entrySet();

LinkedHashMap<String, String> collect = entries.stream()

//Map<String, String>
.collect(Collectors.toMap(k -> (String) k.getKey(), e -> (String) e.getValue()))
.entrySet()
.stream()
.sorted(Map.Entry.comparingByKey())
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue,
(oldValue, newValue) -> oldValue, LinkedHashMap::new));

collect.forEach((k, v) -> System.out.println(k + ":" + v));

}

}

http://www.mkyong.com/java8/java-8-how-to-sort-a-map/

转载地址:http://sawox.baihongyu.com/

你可能感兴趣的文章
mysqlimport
查看>>
基于Xcode安装CocoaPods报错
查看>>
PHP判断变量内容是什么编码
查看>>
php扩展xdebug安装
查看>>
Linux mail 命令
查看>>
Ansi与Unicode及慎用W2A等
查看>>
IDEA 连接 GIT OSCHINA 提交本地已存在的项目
查看>>
EFI分区如何删除
查看>>
【Java集合源码剖析】TreeMap源码剖析
查看>>
二叉树
查看>>
Maven 学习之旅
查看>>
C Primer Plus 第5章 运算符、表达式和语句 编程练习及答案
查看>>
Python点滴
查看>>
WINDOWS 几种坐标系
查看>>
大豆和黄豆芽还能吃吗?
查看>>
Yii2中如何将Jquery放在head中的方法
查看>>
解析solidity的event log
查看>>
[转发] 【GRT安智网】HTC安致手机ROM国内首个中文定制教程goapk首发[最新厨房V0......
查看>>
try catch 之后是否会继续执行
查看>>
vim 配置svn
查看>>