简体   繁体   English

相当于Guavas Maps.uniqueIndex(…)的Set

[英]Equivalent of Guavas Maps.uniqueIndex(…) for Set

Guava has a static method Maps.uniqueIndex(Iterable<V> values, Function<? super V,K> keyFunction) which maps each entry in the entered collection by the result of the entered function applied on respective elements. 番石榴有一个静态方法Maps.uniqueIndex(Iterable<V> values, Function<? super V,K> keyFunction) ,该方法通过将输入函数应用于各个元素的结果来映射输入集合中的每个条目。

Is there an equivalent of this function for Sets (where the return value is not a map but a set)? 对于集合(返回值不是映射而是集合),是否具有此函数的等效项?

A few alternatives: 一些替代方案:

Maps.uniqueIndex(values, keyFunction).keySet();
ImmutableSet.copyOf(Collections2.transform(values, function));

我实际上找到了一个简单的解决方案:

FluentIterable.from(values).transform(keyFunction).toSet();

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

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