Is there a better way to combine two string sets in java?

后端 未结 12 1319
天涯浪人
天涯浪人 2020-12-05 01:41

I need to combine two string sets while filtering out redundant information, this is the solution I came up with, is there a better way that anyone can suggest? Perhaps som

12条回答
  •  醉话见心
    2020-12-05 02:08

    Since a Set does not contain duplicate entries, you can therefore combine the two by:

    newStringSet.addAll(oldStringSet);
    

    It does not matter if you add things twice, the set will only contain the element once... e.g it's no need to check using contains method.

提交回复
热议问题