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

后端 未结 7 823
谎友^
谎友^ 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:24

    Using Java 8 you can also use streams for that effect

    Stream.concat(oldSet.stream(),
                  Stream.of(singleElement))
          .collect(toSet())
    

提交回复
热议问题