java 8 map 排序_java8新特性

本文介绍了使用Java 8 Stream API对Map进行排序的三种方法:按Key升序排序、按Value降序排序以及创建一个通用方法实现Map的排序。通过示例代码详细展示了排序过程。
摘要由CSDN通过智能技术生成

4685490b32f724a7405a849f29f56db4.png

4685490b32f724a7405a849f29f56db4.png

Few Java 8 Stream examples to sort a Map

1. Sort by Key

SortByKeyExample.java

package com.mkyong.test;

import java.util.HashMap;

import java.util.LinkedHashMap;

import java.util.Map;

public class SortByKeyExample {

public static void main(String[] argv) {

Map 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("j", 50);

unsortMap.put("m", 2);

unsortMap.put("f", 9);

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

System.out.println(unsortMap);

Map result = new LinkedHashMap<>();

//sort by key, a,b,c..., and put it into the "result" map

unsortMap.entrySet().stream()

.sorted(Map.Entry.comparingByKey())

.forEachOrdered(x -> result.put(x.getKey(), x.getValue()));

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

System.out.println(result);

}

}

Output

Original...

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

Sorted...

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

2. Sort by Value

SortByValueExample.java

package com.mkyong.test;

import java.util.HashMap;

import java.util.LinkedHashMap;

import java.util.Map;

public class SortByValueExample {

public static void main(String[] argv) {

Map 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("j", 50);

unsortMap.put("m", 2);

unsortMap.put("f", 9);

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

System.out.println(unsortMap);

Map result = new LinkedHashMap<>();

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

unsortMap.entrySet().stream()

.sorted(Map.Entry.comparingByValue().reversed())

.forEachOrdered(x -> result.put(x.getKey(), x.getValue()));

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

System.out.println(result);

}

}

Output

Original...

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

Sorted...

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

3. Generic Example

Create a generic method to sort a Map.

SortByGenericExample.java

package com.mkyong.test;

import java.util.HashMap;

import java.util.LinkedHashMap;

import java.util.Map;

import java.util.stream.Stream;

public class SortByGenericExample {

public static void main(String[] argv) {

Map 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("j", 50);

unsortMap.put("m", 2);

unsortMap.put("f", 9);

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

System.out.println(unsortMap);

System.out.println("Sort By Key...");

Map resultKey = compareByKey(unsortMap);

System.out.println(resultKey);

System.out.println("Sort By Value...");

Map resultValue = compareByValue(unsortMap);

System.out.println(resultValue);

}

//Reference from java.util.Map source code, try dig inside, many generic examples.

public static > Map compareByValue(Map map) {

Map result = new LinkedHashMap<>();

Stream> mapInStream = map.entrySet().stream();

mapInStream.sorted(Map.Entry.comparingByValue())

.forEachOrdered(x -> result.put(x.getKey(), x.getValue()));

return result;

}

public static , V> Map compareByKey(Map map) {

Map result = new LinkedHashMap<>();

Stream> mapInStream = map.entrySet().stream();

mapInStream.sorted(Map.Entry.comparingByKey())

.forEachOrdered(x -> result.put(x.getKey(), x.getValue()));

return result;

}

}

Output

Original...

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

Sort By Key...

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

Sort By Value...

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

Note

Not using Java 8? Try this classic way to sort a Map in Java.

References

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值