简体   繁体   English

从地图中获得最高n个数字的最简洁方法?

[英]Most concise way to get highest n numbers out of a map?

I have the following already: 我已经有以下内容:

public enum InvoiceCurrency {
    EUR(
            s -> (s.contains("€") || s.contains("EUR"))
    ),
    USD(
            s -> (s.contains("$") || s.contains("USD"))
    );

    private final Predicate<String> predicate;

    InvoiceCurrency(final Predicate<String> predicate) {
        this.predicate = predicate;
    }

    public boolean matchesString(final String value) {
        return predicate.test(value);
    }

    public static EnumMap<InvoiceCurrency, Integer> createMapping(final Stream<String> valuesStream) {
        EnumMap<InvoiceCurrency, Integer> mapping = new EnumMap<>(InvoiceCurrency.class);
        mapping.replaceAll((k, v) -> 0);
        Stream<InvoiceCurrency> enums = Arrays.stream(InvoiceCurrency.values());
        valuesStream.forEach(
            s -> enums.forEach(
                e -> {
                    if (e.matchesString(s)) {
                        mapping.compute(e, (k, v) -> v++);
                    }
                }
            )
        );
        return mapping;
    }
}

private InvoiceCurrency calculateCurrency() {
    EnumMap<InvoiceCurrency, Integer> map = InvoiceCurrency.createMapping(data.words.stream().map(w -> w.content));
    InvoiceCurrency maximum = map.entrySet().parallelStream().  //how to continue?
}

This results in a mapping from the enum to the 'number of occurences', so EUR can be mapped to 10 and USD to 1 . 这导致从枚举到“出现次数”的映射,因此EUR可以映射为10USD可以映射为1 Possibly, the count may be the same. 可能,计数可能相同。

Now have do I, as concise as possibly and with the ability to use java-8 , get the InvoiceCurrency that belongs to the highest number? 现在,我是否尽可能简洁并且能够使用java-8获取属于最高编号的InvoiceCurrency And is there a concise way to see that the top 2 of the sorted integer count actually has the same value? 有没有一种简洁的方法可以看到排序后的整数计数的前2个实际上具有相同的值?

I know I can program it with loops, etc., but I wish to rely on the java-8 spirit for the most maintainable code. 我知道我可以使用循环等对它进行编程,但是我希望依靠java-8来获得最可维护的代码。

Simple example with a Map<String, Integer> but the same will work with your example. 使用Map<String, Integer>简单示例,但同样适用于您的示例。 Prints the top 2 entries (b and c or d). 打印前2个条目(b和c或d)。

import static java.util.Collections.reverseOrder;
import static java.util.Comparator.comparingInt;
//...

Map<String, Integer> map = new HashMap<>();
map.put("a", 2);
map.put("b", 10);
map.put("c", 5);
map.put("d", 5);
map.put("e", 1);

map.entrySet().parallelStream()
        .sorted(reverseOrder(comparingInt(Map.Entry::getValue)))
        .limit(2)
        .forEach(System.out::println);

//or:   .forEachOrdered(System.out::println);
//to print in descending order

NOTE: from b129 on, you can also use sorted(comparingInt(Map.Entry::getValue).reversed()) instead of sorted(reverseOrder(comparingInt(Map.Entry::getValue))) . 注意:从b129开始,您还可以使用sorted(comparingInt(Map.Entry::getValue).reversed())而不是sorted(reverseOrder(comparingInt(Map.Entry::getValue)))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM