java 8 中一组数据的并行计算?

Parallel computation on a Set of data in java 8?

我有一组这样的数据:

Set<CustomObject> testSet = [{id: a1, qty: 3}, 
                             {id: a2, qty: 9},
                             {id: a3, qty: 5},
                             {id: a4, qty: 8},
                             {id: a5, qty: 12},
                             ...
                             {id: a200, qty: 7}];

IDs分为3组,可以使用以下方法找到:

//The getGroup method is implemented in the class CustomObject.
//I am using hazelcast map to store few id's that are inclusive, and
//one of the id that is in the request of the api is the current id.
public String getGroup(String id){
     HazelcastInstance hazelcastInstance = Hazelcast.newHazelcastInstance();
     if(id.equals(this.id)){
       return "currentId";
     }else if(id.equals(hazelcastInstance.getMap("idMap").get(id))){
       return "inclusive";
     } else {
       return "exclusive";
     }
}

上面的testSet数据量很大,我想用Java.

按照上面的分组方法对Set中的每个对象进行数量求和

我尝试使用流,但这不允许我在 Java 8 条流的 groupingBy 方法中使用 getGroup 方法。

请指导我如何有效地基于并行处理的组对数量值求和。

这里的代码将给出分组的包含和排除数量的总和。

Map < Object, Integer > resultMap =
    testSet.parallelStream()
    .collect(Collectors.groupingBy(item - > {
            if (item.getId().equals(hazelcastInstance.getMap("idMap").get(id)) 
                    return "inclusive";
                else
                    return "exclusive";
            },
            Collectors.summingInt(CustomObject::getQty)));

另外在使用parallelStream()时,可以考虑使用ArrayList代替HashSet以获得更好的性能,请不要忘记测量它。