1
你能组List<TypeEnum> types = new ArrayList(Arrays.asList(TypeEnum.A, TypeEnum.B, TypeEnum.A));
成Map<TypeEnum, Integer> countPerType;
,使用仿函数(例如Google's Guava,Apache's Commons Functor)预的Java 8?我试图让我的头绕函数式编程,但我不确定这种事情实际上是可能的(因为我不只是映射集合值,但试图聚合)?您可以使用函子/函数式编程在Java 7中对列表进行分组(并对每个组的元素进行计数)?
在命令式的风格,我会做这样的事情:
public Map<TypeEnum, Integer> countByGroup(List<TypeEnum> types) {
Map<TypeEnum, Integer> countedTypes = new HashMap<>();
for(TypeEnum type : types) {
if(countedTypes.containsKey(type)) {
countedTypes.put(type, countedTypes.get(type) + 1);
} else {
countedTypes.put(type, 1);
}
}
return countedTypes;
}
编辑:依托副作用似乎有点不合适 - 或者是它是如何做...?
Procedure<TypeEnum> count = new Procedure<TypeEnum>() {
public Map<TypeEnum, Integer> countPerType = null;
@Override
public void run(TypeEnum type) {
if(countPerType.containsKey(type)) {
countPerType.put(type, countPerType.get(type) + 1);
} else {
countPerType.put(type, 1);
}
}
public Procedure<TypeEnum> init(Map<TypeEnum, Integer> countPerType) {
this.countPerType = countPerType;
return this;
}
}.init(countPerType); // kudos http://stackoverflow.com/a/12206542/2018047
使用番石榴,你想要一个简单的['Multiset'](http://google.github.io/guava/releases/20.0/api/docs/com/google/common/collect/Multiset.html),更具体地说它的['' EnumMultiset'](http://google.github.io/guava/releases/20.0/api/docs/com/google/common/collect/EnumMultiset.html)实现。 'Multiset'是一个数据结构,用于跟踪计数的元素。 –
Java 8并不具有魔力,即Stream API由使用接口类型参数的普通Java代码组成。没有理由不在Java8之前的环境中工作。 – Holger
是的,我知道。正如我所说的,我试图通过使用函数式编程以Java8ish风格的方式解决问题 - 但不能够使用Java 8本身...... :) – Christian