如何合并多个(任意数量的)集合<;字符串>;在Java中转换成一个?

如何合并多个(任意数量的)集合<;字符串>;在Java中转换成一个?,java,collections,guava,Java,Collections,Guava,我有多个集合,需要合并成一个集合。如何在Java中执行此操作?请注意,我正在尽可能地使用guavaAPI来帮助您。例如,我有3个类,如下所示 public class One { public static Set<String> SET = Sets.newHashSet("a","b","c"); } public class Two { public static Set<String> SET = Sets.newHashSet("a","d","e","f"

我有多个
集合
,需要合并成一个
集合
。如何在Java中执行此操作?请注意,我正在尽可能地使用
guava
API来帮助您。例如,我有3个类,如下所示

public class One {
 public static Set<String> SET = Sets.newHashSet("a","b","c");
}
public class Two {
 public static Set<String> SET = Sets.newHashSet("a","d","e","f");
}
public class Three {
 public static Set<String> SET = Sets.newHashSet("w","x","y","f");
}
这是电话号码;它不工作(不编译)

调用代码如下所示,我得到“类型安全性:为varargs参数创建一个集合的通用数组”

Set Set=Utils.immutableSetOf(一个.Set,两个.Set);
更新:对于被接受的答案,我做了以下几点

Set<String> set = Utils.immutableSetOf(
 Lists.newArrayList(
  One.SET, Two.SET));
public static Set<String> immutableSetOf(Set<String>... sets)
@SuppressWarnings("unchecked")
Set<String> set = Utils.immutableSetOf(Set[] { One.SET, Two.SET });
@SuppressWarnings(“未选中”)
Set Set=Utils.immutableSetOf(Set[]{One.Set,Two.Set});

在上一次尝试中,您编写了

Set<String> set = Utils.immutableSetOf(new Set<String>[] { One.SET, Two.SET });
Set Set=Utils.immutableSetOf(新集合[]{One.Set,Two.Set});
这不起作用,因为java中不允许创建泛型数组

试试这个:

@SuppressWarnings("unchecked")
Set<String>[] mySet = new Set[] { One.SET, Two.SET };
Set<String> set = Utils.immutableSetOf(mySet);
@SuppressWarnings(“未选中”)
Set[]mySet=newset[]{One.Set,Two.Set};
Set=Utils.immutableSetOf(mySet);
这样做的原因是,我们创建了一个新的集合[],但没有指定泛型类型,并将其分配给引用集合[]mySet(因为这是一个未选中的操作,我们必须将@SuppressWarnings(“未选中”)添加到它)

您可以使用:

Set<String> set = immutableSetOf(Arrays.asList(One.SET, Two.SET));
Set Set=immutableSetOf(Arrays.asList(One.Set,Two.Set));

使用
immutableTof(列表集)
的定义。没有警告或禁止的警告。

我认为
Fluentitable
(在18.0及以上版本中)在这方面会有所帮助,特别是

因此,我们需要定义一个方便的helper方法——是的,我们将使用varargs实现这一点

public <T extends Comparable<T>> Set<T> mergeSets(Set<T> initial,
                                                 Set<T>... rest) {
    FluentIterable<T> result =  FluentIterable.from(initial);
    for(Set<T> set : rest) {
        result = result.append(set);
    }
    return new TreeSet<>(result.toSet());
}
公共集合合并集合(集合初始,
设置…休息){
Fluentitable结果=Fluentitable.from(初始);
用于(设置:休息){
result=result.append(set);
}
返回新树集(result.toSet());
}

这里的结果集是按自然顺序排列的。如果你不想要一个
TreeSet
,也不想在以后改变你的集合,那么就省略
新的TreeSet
部分,然后松开
t

推荐
com.google.common.collect.Set#union(set1,set2)
以获得合并,而不是
Set.addAll
,因为番石榴已经在你的依赖中了

原因:它是视图,对记忆有效,而且不可修改


另外:对不起,我应该把它作为评论发布。

创建一个输入集集合,然后使用
flatMap

Set<String> allElements = ImmutableSet.of(
        One.SET,
        Two.SET,
        Three.SET
).stream().flatMap(Collection::stream).collect(Collectors.toSet());
Set等位基因=ImmutableSet.of(
一套,
二,一套,,
三套
).stream().flatMap(Collection::stream).collect(Collectors.toSet());

这确实有效,但在指定泛型类型时有没有办法?据我所知,java不允许这样做。此外,也没有必要这样做,因为在一天结束时,我们需要有一个指定类型的引用,这是可以完成的。推荐com.google.common.collect.Sets#union(set1,set2)来获得合并,而不是Set.addAll,因为Guava已经在您的依赖项中。原因:这是一个内存有效且不可修改的视图。
警告:[未选中]参数化vararg类型集可能会造成堆污染
我会做
Stream.of(One.Set,Two.Set,Three.Set)
而不是
ImmutableSet.of().Stream()
,以避免不必要地创建临时
ImmutableSet
@SuppressWarnings("unchecked")
Set<String>[] mySet = new Set[] { One.SET, Two.SET };
Set<String> set = Utils.immutableSetOf(mySet);
Set<String> set = immutableSetOf(Arrays.asList(One.SET, Two.SET));
public <T extends Comparable<T>> Set<T> mergeSets(Set<T> initial,
                                                 Set<T>... rest) {
    FluentIterable<T> result =  FluentIterable.from(initial);
    for(Set<T> set : rest) {
        result = result.append(set);
    }
    return new TreeSet<>(result.toSet());
}
Set<String> allElements = ImmutableSet.of(
        One.SET,
        Two.SET,
        Three.SET
).stream().flatMap(Collection::stream).collect(Collectors.toSet());