Java,使用流对地图进行分组、排序和收集
我有两张商店的地图,我想数一数第一张地图上每个品牌有多少家商店,但是这些品牌只在第二张地图上有。然后,我想按每个品牌的店铺数量降序对结果进行排序。我的代码如下所示:Java,使用流对地图进行分组、排序和收集,java,java-8,java-stream,linkedhashmap,Java,Java 8,Java Stream,Linkedhashmap,我有两张商店的地图,我想数一数第一张地图上每个品牌有多少家商店,但是这些品牌只在第二张地图上有。然后,我想按每个品牌的店铺数量降序对结果进行排序。我的代码如下所示: Store store, store1, store2, store3, store4, store5, store6; store = new Store("1", null); store1 = new Store("2", null); store2 = new Store("3", null); Map<String,
Store store, store1, store2, store3, store4, store5, store6;
store = new Store("1", null);
store1 = new Store("2", null);
store2 = new Store("3", null);
Map<String, Store> dynamicShops = new HashMap<>();
dynamicShops.put(store.getId(), store);
dynamicShops.put(store1.getId(), store1);
dynamicShops.put(store2.getId(), store2);
store3 = new Store("1", "ABC");
store4 = new Store("2", "ABC");
store5 = new Store("3", "Abra Cadabra");
store6 = new Store("4", "Abra Cadabra");
Map<String, Store> staticShops = new HashMap<>();
staticShops.put(store3.getId(), store3);
staticShops.put(store4.getId(), store4);
staticShops.put(store5.getId(), store5);
staticShops.put(store6.getId(), store6);
Map<String, Long> stats = dynamicShops.values().stream()
.collect(groupingBy(s -> staticShops.get(s.getId()).getBrand(), counting()));
Map<String, Long> statsSorted = stats.entrySet().stream()
.sorted(Map.Entry.<String, Long>comparingByValue().reversed())
.collect(toMap(Map.Entry::getKey, Map.Entry::getValue, (e1, e2) -> e1, LinkedHashMap::new));
statsSorted.entrySet().stream().forEach(s -> System.out.println(s.getKey() + " " + s.getValue()));
现在我想知道是否有一种方法可以将其整合到一个流中?这里有一个解决方案:
dynamicShops.values()
.stream()
.map(s -> staticShops.get(s.getId()).getBrand())
.collect(Collectors.collectingAndThen(Collectors.groupingBy(Function.identity(), Collectors.counting()), m -> m.entrySet().stream()))
.sorted(Entry.<String, Long>comparingByValue().reversed())
.forEach(s -> System.out.println(s.getKey() + " " + s.getValue()));
dynamicShops.values()
.stream()
.map(s->staticShops.get(s.getId()).getBrand())
.collect(Collectors.collectingAndThen(Collectors.groupingBy(Function.identity(),Collectors.counting()),m->m.entrySet().stream())
.sorted(Entry.comparingByValue().reversed())
.forEach(s->System.out.println(s.getKey()+“”+s.getValue());
这里有一个解决方案:
dynamicShops.values()
.stream()
.map(s -> staticShops.get(s.getId()).getBrand())
.collect(Collectors.collectingAndThen(Collectors.groupingBy(Function.identity(), Collectors.counting()), m -> m.entrySet().stream()))
.sorted(Entry.<String, Long>comparingByValue().reversed())
.forEach(s -> System.out.println(s.getKey() + " " + s.getValue()));
dynamicShops.values()
.stream()
.map(s->staticShops.get(s.getId()).getBrand())
.collect(Collectors.collectingAndThen(Collectors.groupingBy(Function.identity(),Collectors.counting()),m->m.entrySet().stream())
.sorted(Entry.comparingByValue().reversed())
.forEach(s->System.out.println(s.getKey()+“”+s.getValue());
你的意思是Map statsorted=dynamicShops.values().stream().collect(groupingBy(s->staticShops.get(s.getId()).getBrand().counting()).entrySet().stream().sorted(Map.Entry.comparingByValue().reversed()).collect(toMap(Map.Entry::getKey,Map.Entry::getValue,(e1,e2)->e1,LinkedHashMap::new)代码>@YCF_L,从技术上讲,这是两条流请看一看,并指定您在这里使用单流时试图实现的目标?@Naman我假设只将这些值流一次而不是两次,这才是有效的。效率并不取决于您执行了多少流操作。效率取决于CPU必须执行的实际操作。不能按未知的计数进行排序。原则上,您可以实现一种数据结构,在计数发生变化时动态重新排序,但性能会比排序前的计数更差。您的意思是Map statsorted=dynamicShops.values().stream().collect(groupingBy(s->staticShops.get(s.getId()).getBrand(),counting()).entrySet().stream().sorted(Map.Entry.comparingByValue().reversed()).collect(toMap(Map.Entry::getKey,Map.Entry::getValue,(e1,e2)->e1,LinkedHashMap::new))代码>@YCF_L,从技术上讲,这是两条流请看一看,并指定您在这里使用单流时试图实现的目标?@Naman我假设只将这些值流一次而不是两次,这才是有效的。效率并不取决于您执行了多少流操作。效率取决于CPU必须执行的实际操作。不能按未知的计数进行排序。原则上,您可以实现一个数据结构,每当计数发生变化时,它会动态地重新排序,但性能会比排序前的计数更差。我想它仍然是两个流。我想知道有没有一种方法可以在第一次收集的时候用一些收集,然后或者类似的东西来做。我想它仍然是两条流。我想知道有没有一种方法可以在第一次收集时使用一些CollectionAnd或类似的东西来实现这一点。