Transform and convert a List to Set with Guava

前端 未结 1 1475
别跟我提以往
别跟我提以往 2021-02-09 05:10

Is there a simple way to convert and transform a List to Set with Guava?

I\'d like to use method:



        
相关标签:
1条回答
  • 2021-02-09 05:19
    Set<To> result = FluentIterable.from(myList)
                                   .transform(new Function<From, To>() {
                                       @Override
                                       public To apply(From input) {
                                           return convert(input);
                                       }
                                   })
                                   .toSet();
    

    This creates an ImmutableSet, which does not accept null. So if you want your Set to contain null, you'll have to use another solution, like the one you're currently using.

    Note that, if it's the creation of the temporary collection that bothers you, you shouldn't be bothered. No copy is made. The collection is simply a view over the original list.

    0 讨论(0)
提交回复
热议问题