Sum of values from hashmap using streams

偶尔善良 提交于 2021-01-01 04:51:25

问题


I have an hashmap of huge size (around 10^60). I am putting values in each entry one by one. Problem is to get the sum of values from hashmap for given range of keys. eg: Putting it in simple Hashmap has entries from 0 to 1000 as a key and every key has an value(BigInteger). Now problem is to get sum of values from range (say) 37 to 95.

I have tried with iterator but when we go for huge map of size 10^60, it is time taking operation for large range of indices.

I am trying it with streams but as am new to the streams/parallelStreams, I am not getting actual idea about it.

BigInteger index1 = new BigInteger(array[1]); // array[1] min value
BigInteger index2 = new BigInteger(array[2]); // array[2] max value
BigInteger max = index1.max(index2); // getting max and min range from index1 and index2
BigInteger min = index1.min(index2);
AtomicReference<Long> atomicSum = new AtomicReference<Long>(0l);
hashMap.entrySet().parallelStream().
    forEach(e -> {
        if (e.getKey().compareTo(min) == 1 && e.getKey().compareTo(max) == -1) {
            atomicSum.accumulateAndGet(e.getValue().longValue(), (x,y) -> x+y);
        }
    });

I searched over SO and few are related to list or without Streams. Please also suggest if any improvement is possible like using some other data structure instead of HashMap.


回答1:


You seem to be looking for something like :

BigInteger sumOfValues = hashMap.entrySet().stream()
        .filter(e -> e.getKey().compareTo(min) > 0 && e.getKey().compareTo(max) < 0)
        .map((Map.Entry::getValue))
        .reduce(BigInteger.ZERO, BigInteger::add);

or stated as in your code

Long sumOfValues = hashMap.entrySet().stream()
        .filter(e -> e.getKey().compareTo(min) > 0 && e.getKey().compareTo(max) < 0)
        .map((Map.Entry::getValue))
        .reduce(BigInteger.ZERO, BigInteger::add).longValue();


来源:https://stackoverflow.com/questions/55888405/sum-of-values-from-hashmap-using-streams

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!