簡體   English   中英

使用 Stream API 從 HashMap 創建列表,其中元素按特定順序排列

[英]Use the Stream API to create List from HashMap with elements arranged in specific order

我有一個HashMap<String, List<Appliance>> ,其中 object Appliance中的字段name::String用作鍵, HashMap中的每個值都是Appliance對象的list 每個列表根據Appliance object 的字段“price::BigDecimal”按升序排序。 I would like to create an ArrayList<Appliance> , using the Stream API , and prexisted HashMap by extracting, first the first elements of each list in the HashMap , then the second ones, etc. So if the HashMap has these contents:

["Fridge",     [<"Fridge", 100>, <"Fridge", 200>, <"Fridge", 300>],
 "Oven",       [<"Oven", 150>, <"Oven", 250>, <"Oven", 350>],
 "DishWasher", [<"DishWasher", 220>, <"DishWasher", 320>, <"DishWasher", 420>]]

我希望最終名單如下:

[<"Fridge",     100>,
 <"Oven",       150>,
 <"DishWasher", 220>,
 <"Fridge",     200>,
 <"Oven",       250>,
 <"DishWasher", 320>,
 <"Fridge",     300>,
 <"Oven",       350>,
 <"DishWasher", 420>]

是否可以使用 Java 的 8 Stream API 以功能方式做到這一點?

這是我的代碼。 我想以聲明的方式實現相同的結果。

while(!appliancesMap.isEmpty()) {
    for (Map.Entry<String, List<Appliance>> entry : 
        appliancesMap.entrySet()) {
        String key = entry.getKey();
        List<Appliance> value = entry.getValue();
        finalList.add(value.get(0));
        value.remove(0);
        if (value.size() == 0) {
            appliancesMap.entrySet()
                .removeIf(predicate -> predicate.getKey().equals(key));
        } else {
            appliancesMap.replace(key, value);
        }
    }
}

腳步:

  1. 找到 map 中最長列表的大小。 這可以作為
map.keySet().stream().mapToInt(k -> map.get(k).size()).max().getAsInt()
  1. 使用IntStream迭代從 0 到步驟#1 中獲得的最大大小的值
IntStream.range(0, map.keySet().stream().mapToInt(k -> map.get(k).size()).max().getAsInt())
  1. 使用IntStream的每個值(例如i )作為索引從列表中獲取元素,例如如果i = 0 ,則從 map 內的每個列表中獲取索引處的元素0並添加到result列表
List<Appliance> result = new ArrayList<>();

IntStream.range(0, map.keySet().stream().mapToInt(k -> map.get(k).size()).max().getAsInt())
    .forEach(i -> map
                .keySet()
                .stream()
                .filter(key -> i < map.get(key).size())
                .forEach(k -> result.add(map.get(k).get(i))));

演示

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.IntStream;

class Appliance {
    private String name;
    private double price;

    public Appliance(String name, double price) {
        this.name = name;
        this.price = price;
    }

    @Override
    public String toString() {
        return "Appliance [name=" + name + ", price=" + price + "]";
    }
}

public class Main {
    public static void main(String[] args) {
        Map<String, List<Appliance>> map = Map.of("Fridge",
                List.of(new Appliance("Fridge", 100), new Appliance("Fridge", 200), new Appliance("Fridge", 300)),
                "Oven", List.of(new Appliance("Oven", 150), new Appliance("Oven", 250), new Appliance("Oven", 350)),
                "DishWasher", List.of(new Appliance("DishWasher", 220), new Appliance("DishWasher", 320),
                        new Appliance("DishWasher", 420)));

        List<Appliance> result = new ArrayList<>();

        IntStream.range(0, map.keySet().stream().mapToInt(k -> map.get(k).size()).max().getAsInt())
        .forEach(i -> map
                .keySet()
                .stream()
                .filter(key -> i < map.get(key).size())
                .forEach(k -> result.add(map.get(k).get(i))));

        // Display
        result.forEach(System.out::println);
    }
}

Output:

Appliance [name=Fridge, price=100.0]
Appliance [name=Oven, price=150.0]
Appliance [name=DishWasher, price=220.0]
Appliance [name=Fridge, price=200.0]
Appliance [name=Oven, price=250.0]
Appliance [name=DishWasher, price=320.0]
Appliance [name=Fridge, price=300.0]
Appliance [name=Oven, price=350.0]
Appliance [name=DishWasher, price=420.0]

[更新]

下面給出了解決方案的慣用代碼(感謝Holger ):

List<Appliance> result = IntStream.range(0, map.values().stream().mapToInt(List::size).max().getAsInt())
                            .mapToObj(i -> map.values()
                                    .stream()
                                    .filter(list -> i < list.size())
                                    .map(list -> list.get(i)))
                            .flatMap(Function.identity()).collect(Collectors.toList());
map.keySet().stream().map(map::get).forEach(list::addAll);

.addAll()中的.stream()可以完成這項工作。

現在您已經擁有列表中的所有元素,您可以對其進行排序:

list.sort(Comparator.comparing(Object::toString)
    .thenComparingInt(s -> Integer.parseInt(
            s.toString()
             .substring(0, s.toString().length() - 1)
             .split(",")[1].trim())));

如果您不介意Fridge -> Oven -> DishWasher的順序,下面的代碼很有幫助:

map.values().stream().flatMap((Function<List<Appliance>, Stream<Appliance>>) Collection::stream)
    .collect(Collectors.groupingBy(appliance -> {
    List<Appliance> appliances = map.get(appliance.getName());
    for (int i = 0;i<appliances.size();i++) {
        if (appliance.getPrice() == appliances.get(i).getPrice()) {
            return i;
        }
    }
    return 0;
})).values().stream().flatMap((Function<List<Appliance>, Stream<Appliance>>) Collection::stream)
    .forEach(System.out::println);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM