Java, группировка, сортировка и сбор карт с использованием потоков - PullRequest
0 голосов
/ 11 марта 2020

У меня есть две Карты магазинов, я хотел бы посчитать, сколько магазинов каждой марки есть на этой первой карте, но бренды доступны только на второй карте. Затем я хочу отсортировать результаты по убыванию количества магазинов каждой марки. Мой код выглядит так:

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()));

И выдает ожидаемый результат:

ABC 2
Abra Cadabra 1

Теперь мне интересно, есть ли способ сделать это sh в одном потоке

1 Ответ

1 голос
/ 12 марта 2020

Вот одно из решений:

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()));
...