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

后端 未结 12 1267
天涯浪人
天涯浪人 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:20

    From the definition Set contain only unique elements.

    Set<String> distinct = new HashSet<String>(); 
     distinct.addAll(oldStringSet);
     distinct.addAll(newStringSet);
    

    To enhance your code you may create a generic method for that

    public static <T> Set<T> distinct(Collection<T>... lists) {
        Set<T> distinct = new HashSet<T>();
    
        for(Collection<T> list : lists) {
            distinct.addAll(list);
        }
        return distinct;
    }
    
    0 讨论(0)
  • 2020-12-05 02:21

    The same with Guava:

    Set<String> combinedSet = Sets.union(oldStringSet, newStringSet)
    
    0 讨论(0)
  • 2020-12-05 02:22
    Set.addAll()
    

    Adds all of the elements in the specified collection to this set if they're not already present (optional operation). If the specified collection is also a set, the addAll operation effectively modifies this set so that its value is the union of the two sets

    newStringSet.addAll(oldStringSet)
    
    0 讨论(0)
  • 2020-12-05 02:23
     newStringSet.addAll(oldStringSet);
    

    This will produce Union of s1 and s2

    0 讨论(0)
  • 2020-12-05 02:25

    http://docs.oracle.com/javase/7/docs/api/java/util/Set.html#addAll(java.util.Collection)

    Since sets can't have duplicates, just adding all the elements of one to the other generates the correct union of the two.

    0 讨论(0)
  • 2020-12-05 02:25

    If you care about performance, and if you don't need to keep your two sets and one of them can be huge, I would suggest to check which set is the largest and add the elements from the smallest.

    Set<String> newStringSet = getNewStringSet();
    Set<String> oldStringSet = getOldStringSet();
    
    Set<String> myResult;
    if(oldStringSet.size() > newStringSet.size()){
        oldStringSet.addAll(newStringSet);
        myResult = oldStringSet;
    } else{
        newStringSet.addAll(oldStringSet);
        myResult = newStringSet;
    }
    

    In this way, if your new set has 10 elements and your old set has 100 000, you only do 10 operations instead of 100 000.

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