What is an efficient and elegant way to add a single element to an immutable set?

后端 未结 7 761
谎友^
谎友^ 2020-12-29 21:40

I have an immutable set (cast as a Set) that potentially contains many elements. I need a Collection that contains the elements from that set plu

相关标签:
7条回答
  • 2020-12-29 22:10

    I'm experiencing cognitive dissonance when I read "immutable" and "add to" in the same sentence. You can add a new element to the end of a mutable copy of the immutable values, but you can't modify the immutable set. I don't know of anything elegant.

    0 讨论(0)
  • 2020-12-29 22:12

    You might consider Sets.union(). Construction would be faster, but use slower.

    public static <T> Set<T> setWith(Set<T> old, T item) {
      return Sets.union(old, Collections.singleton(item);
    }
    

    (com.google.common.collect.Sets & java.util.Collections)

    0 讨论(0)
  • 2020-12-29 22:16

    You have three options.

    • Use a mutable set.
    • Check the element isn't already present, if not create a copy of the set and add an element.
    • Create a wrapper set which includes the previous set and the element.

    Sometimes a BitSet is a better choice than Set<Integer> depending on the distribution of your values.

    0 讨论(0)
  • 2020-12-29 22:24

    Using Java 8 you can also use streams for that effect

    Stream.concat(oldSet.stream(),
                  Stream.of(singleElement))
          .collect(toSet())
    
    0 讨论(0)
  • 2020-12-29 22:25

    Not sure about performance, but you can use Guava's ImmutableSet.Builder:

    import com.google.common.collect.ImmutableSet
    
    // ...
    Set<Integer> newSet = new ImmutableSet.Builder<Integer>()
                                    .addAll(oldSet)
                                    .add(3)
                                    .build();
    

    Of course you can also write yourself a helper method for that:

    public static <T> Set<T> setWith(Set<T> old, T item) {
      return new ImmutableSet.Builder<T>().addAll(old).add(item).build();
    }
    
    // ...
    Set<Integer> newSet = setWith(oldSet, 3);
    
    0 讨论(0)
  • 2020-12-29 22:25

    If the Set is immutable, I don't see any way to do it other than copy the Set, and then add your new element. Remember, copying a set is as easy as passing the base set to the constructor function when creating the new set.

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