通过 Stream API 迭代和减少 Java 映射的值

时间:2021-07-01 15:14:29

标签: java collections java-stream reduce

这里是 Java 8。我有一个字符串数组:

String[] animals = getsomehow(); // "dogs", "cats", "sheep", etc.

然后我有一个映射,其中键是字符串(具体来说,与上面数组中的一些动物具有相同的文字值),而这些值是一个计数(表示那些动物的数量)动物):

Map<String,Integer> animalCounts = new HashMap<String,Integer>();
animalCounts.put("sheep", 4);
animalCounts.put("dogs", 2);
animalCounts.put("cats", 0);
animalCounts.put("porcupines", null);
animalCounts.put("dolphins", 43);

我想弄清楚如何使用 Stream API 迭代我的 animals 数组,并得出动物总数。例如,如果我的 animals 数组中有“羊”和“海豚”,那么动物总数将为 4 + 43 = 47。

迄今为止我最好的尝试:

int totalAnimals = Arrays.stream(animals)
    .reduce(
        0,
        (subtotal, animal) -> subtotal + animalCounts.get(animal));

然而,这会为 0 的标识值产生编译器错误:

<块引用>

"所需类型:字符串"

谁能发现我哪里出错了?

2 个答案:

答案 0 :(得分:2)

<块引用>

谁能发现我哪里出错了?

您正在使用 reduce 的 2 参数版本:

T reduce(T identity,
         BinaryOperator<T> accumulator)

如您所见,标识值和输出必须与输入的类型相同,因此必须是 String

解决方案是使用 reduce 的 3 参数版本:

<U> U reduce(U identity,
             BiFunction<U,? super T,U> accumulator,
             BinaryOperator<U> combiner)

或者,您可以这样做:

int totalAnimals = Arrays.stream(animals)
        .map(animalCounts::get)
        .filter(Objects::nonNull)
        .mapToInt(Integer::intValue)
        .sum();

答案 1 :(得分:0)

Arrays.stream(animals) 返回的流是 Stream<String> 类型,而您正在减少标识为零的流,即 int

一个简单的方法是将每只动物映射到它的数量,并将结果 Stream<Integer> 减少到它的总和:

int totalAnimals = Arrays.stream(animals)
                         .map(animalCounts::get)
                         .filter(Objects::nonNull)
                         .collect(Collectors.summingInt(Integer::intValue));

请注意在映射中使用 getOrDefault(animal, 0),因为数组可能包含映射中不存在的元素。