Jan*_*nne 4 java collections transform guava
我有这种情况,我想从多个源对象中提取多个值到一个集合.我试图通过Guava的转换实现这一目标,但遇到了我收回一系列集合的问题,我必须手动"扁平化".有没有一种很好的方法可以直接在平面集合中获得结果?
private static final Function<Target, Collection<Integer>> EXTRACT_FUNCTION = new Function<SourceObject, Collection<Integer>>() {
@Override
public Collection<Integer> apply(SourceObject o) {
// extract and return a collection of integers from o
return Lists.newArrayList(..);
}
};
Collection<SourceObject> sourceObjects = ...
Collection<Collection<Integer>>> nestedResults = transform(sourceObjects, EXTRACT_FUNCTION);
// Now I have to manually flatten the results by looping and doing addAll over the nestedResults..
// Can this be avoided?
Collection<Integer> results = flattenNestedResults(nestedResults);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2390 次 |
| 最近记录: |