如何使用flatmap java8根据键的数量为值排序哈希映射? [英] How to sort hash map based on number of keys for a value using flatmap java8?
问题描述
这是如何获得使用lambda表达式的哈希映射中的值的键值计数。我有一个 HashMap
,我想要找到每个值的键数量
Map< Integer,List< Integer>> map = new HashMap< Integer,List< Integer>>(){{
put(0,Arrays.asList(1,2));
put(1,Arrays.asList(2,0,3));
put(2,Arrays.asList(4,0,1));
put(3,Arrays.asList(4,1,5));
put(4,Arrays.asList(5,2,3));
put(5,Arrays.asList(4,3));
}};
根据上面的帖子,我试过平面贴图:
Map< Object,Long> ex =
map.values()
.stream()
.flatMap(Collection :: stream)
.collect(Collectors.groupingBy(v - > v,数数()));
System.out.println(ex);
输出是
{0 = 2,1 = 3,2 = 3,3 = 3,4 = 3,5 = 2}。
这意味着0有两个键,1有三个键等等。现在我想根据键的数量以降序对键和值进行排序。我试过这样的事情:
Map< Object,Long> ex =
map.values()
.stream()
.flatMap(Collection :: stream)
.collect(Collectors.groupingBy(v - > v,数数()));
$ b $ .entrySet()
.stream()
.sorted(Map.Entry。< String,Long> comparisonByValue(reverseOrder())。thenComparing(Map.Entry。比较关键字()))
.collect(LinkedHashMap :: new,(m,e) - > m.put(e.getKey(),e.getValue()),Map :: putAll);
我想要以下输出:
<$ p $ 1,3,5],2 = [1,4,0],3 = [1,4,5],4 = [2,3,5],0 = = [1,2],5 = [3,4]
键和值应该排列根据这个密钥计数 {0 = 2,1 = 3,2 = 3,3,3 = 3,4 = 3,5 = 2}
:1,2 ,3,4有三个键,0和5有两个键。例如: 1 = [2,3,0] code>:1有三个键,所以它首先以
[2,3,0]
出现:2和3有三个键,0只有两个键。 / p>
您可以具有以下内容:
Map< Integer,List< Integer>> (e - > ex.get(e.getKey()),reverseOrder()))$ b。sort(
map.entrySet()
.stream() $ b .collect(toMap(
Map.Entry :: getKey,
e - > e.getValue()。stream()。sorted(comparison(ex :: get,reverseOrder()))。 collect(toList()),
(v1,v2) - > {throw new IllegalStateException();},
LinkedHashMap :: new
));
这将创建地图条目的一个Stream,根据键的数量以相反的顺序排序它们对于该条目的关键字,最后将其收集到一个映射中,在该映射中,对于每个整数的计数( ex :: get
),新值按相反顺序排序。收集地图是一个 LinkedHashMap
以保留遭遇订单。
输出:
$ b = {code $} {1 = [2,3,0],2 = [4,1,0],3 = [4,1,5],4 = [2 ,3 = 5],0 = [1,2],5 = [4,3]}
使用静态导入:
import static java.util.Comparator.comparing;
import static java.util.Comparator.reverseOrder;
import static java.util.stream.Collectors.toList;
import static java.util.stream.Collectors.toMap;
This is a follow-up of How to get the count of keys for values in a hash map using lambda. I have a HashMap
and I want to find the number of keys for each value
Map<Integer, List<Integer>> map = new HashMap<Integer, List<Integer>>() {{
put(0, Arrays.asList(1, 2));
put(1, Arrays.asList(2, 0, 3));
put(2, Arrays.asList(4,0,1));
put(3, Arrays.asList(4,1, 5));
put(4, Arrays.asList(5,2,3));
put(5, Arrays.asList(4,3));
}};
According to the above post, I tried flat mapping:
Map<Object, Long> ex =
map.values()
.stream()
.flatMap(Collection::stream)
.collect(Collectors.groupingBy(v -> v, Collectors.counting()));
System.out.println(ex);
The output is
{0=2, 1=3, 2=3, 3=3, 4=3, 5=2}.
This means 0 has two keys, 1 has three keys and so on. Now I want to sort the keys and values in descending order based on count of keys. I tried something like this:
Map<Object, Long> ex =
map.values()
.stream()
.flatMap(Collection::stream)
.collect(Collectors.groupingBy(v -> v, Collectors.counting()));
.entrySet()
.stream()
.sorted(Map.Entry.<String, Long>comparingByValue(reverseOrder()).thenComparing(Map.Entry.comparingByKey()))
.collect(LinkedHashMap::new, (m,e) -> m.put(e.getKey(), e.getValue()), Map::putAll);
I want the following output:
1=[2, 3, 0], 2=[1,4,0], 3=[1, 4, 5], 4=[2, 3, 5], 0=[1, 2], 5=[3, 4]
The keys and values should be arranged in descending according to this count of keys {0=2, 1=3, 2=3, 3=3, 4=3, 5=2}
: 1, 2, 3, 4 has three keys, 0 and 5 have two keys.
For example: 1=[2, 3, 0]
: 1 has three keys so it appears first with [2, 3, 0]
: 2 and 3 have three keys and 0 has only two keys.
You could have the following:
Map<Integer, List<Integer>> sorted =
map.entrySet()
.stream()
.sorted(comparing(e -> ex.get(e.getKey()), reverseOrder()))
.collect(toMap(
Map.Entry::getKey,
e -> e.getValue().stream().sorted(comparing(ex::get, reverseOrder())).collect(toList()),
(v1, v2) -> { throw new IllegalStateException(); },
LinkedHashMap::new
));
This creates a Stream of the entries of the map, sorts them in reverse order according the count of keys for that entry's key and finally collects that into a map where the new value is sorted in reverse order with regard to the count of each integer (ex::get
). The collecting map is a LinkedHashMap
to preserve encounter order.
Output:
{1=[2, 3, 0], 2=[4, 1, 0], 3=[4, 1, 5], 4=[2, 3, 5], 0=[1, 2], 5=[4, 3]}
Static imports used:
import static java.util.Comparator.comparing;
import static java.util.Comparator.reverseOrder;
import static java.util.stream.Collectors.toList;
import static java.util.stream.Collectors.toMap;
这篇关于如何使用flatmap java8根据键的数量为值排序哈希映射?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!