将List转换并转换为使用Guava进行设置

有没有一种简单的方法来转换转换列表与番石榴设置?

我想用方法:

Set result = Sets.transformToSet(myList, new Function() { public To apply(From item) { return convert(item); } }); 

这是我的代码,带有“tempCollection”

 Collection tempCollection = Collections2.transform(myList, new Function() { public To apply(From item) { return convert(item); } }); Set result = newHashSet(tempCollection ); 

 Set result = FluentIterable.from(myList) .transform(new Function() { @Override public To apply(From input) { return convert(input); } }) .toSet(); 

这将创建一个ImmutableSet,它不接受null。 因此,如果您希望Set包含null,则必须使用其他解决方案,例如您当前使用的解决方案。

请注意,如果它是创建困扰您的临时集合,您不应该被打扰。 没有复制。 该集合只是原始列表的视图。