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

Iterating over and reducing values of a Java map via Stream API

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

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

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

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:

的标识值,这会产生编译器错误

"Required type: String"

谁能看出我哪里出错了?

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) 的使用,因为数组可能包含映射中不存在的元素。

Can anyone spot where I'm going awry?

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

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