Java .asSet(...) 是否存在于任何 API 中?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16358796/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Does .asSet(...) exist in any API?
提问by cahen
I'm looking for a very simple way to create a Set.
我正在寻找一种非常简单的方法来创建一个集合。
Arrays.asList("a", "b" ...)
creates a List<String>
Arrays.asList("a", "b" ...)
创建一个 List<String>
Is there anything similar for Set
?
有什么类似的Set
吗?
采纳答案by Mr. Anderson
Now with Java 8 you can do this without need of third-party framework:
现在使用 Java 8,您无需第三方框架即可执行此操作:
Set<String> set = Stream.of("a","b","c").collect(Collectors.toSet());
See Collectors.
见收藏家。
Enjoy!
享受!
回答by Dev Blanked
You could use
你可以用
new HashSet<String>(Arrays.asList("a","b"));
回答by Evgeniy Dorofeev
No but you can do it like this
不,但你可以这样做
new HashSet<String>(Arrays.asList("a", "b", ...));
回答by Michael Schmei?er
Using Guava, it is as simple as that:
使用Guava,就这么简单:
Set<String> mySet = ImmutableSet.<String> of("a", "b");
Or for a mutable set:
或者对于可变集:
Set<String> mySet = Sets.newHashSet("a", "b")
For more data types see the Guava user guide
有关更多数据类型,请参阅Guava 用户指南
回答by OscarRyz
回答by durron597
As others have said, use:
正如其他人所说,使用:
new HashSet<String>(Arrays.asList("a","b"));
The reason this does notexist in Java is that Arrays.asList
returns a fixed sized list, in other words:
这在 Java中不存在的原因是Arrays.asList
返回一个固定大小的列表,换句话说:
public static void main(String a[])
{
List<String> myList = Arrays.asList("a", "b");
myList.add("c");
}
Returns:
返回:
Exception in thread "main" java.lang.UnsupportedOperationException
at java.util.AbstractList.add(Unknown Source)
at java.util.AbstractList.add(Unknown Source)
There is no JDK implementation of a "fixed-size" Set
inside the Arrays
class. Why do you want this? A Set
guarantees that there are no duplicates, but if you are typing them out by hand, you shouldn't need that functionality... and List
has more methods. Both interfaces extend Collection
and Iterable
.
没有JDK实现一个“固定大小”的Set
内部Arrays
类。你为什么要这个?ASet
保证没有重复项,但是如果您手动输入它们,则不需要该功能......并且List
有更多方法。两个接口都扩展Collection
和Iterable
.
As others have said, use guavaIf you really want this functionality - since it's not in the JDK. Look at their answers (in particular Michael Schmei?er's answer) for information on that.
正如其他人所说,如果您真的想要此功能,请使用番石榴- 因为它不在 JDK 中。查看他们的答案(特别是 Michael Schmei?er 的答案)以获取有关信息。
回答by Samuel Edwin Ward
For the special cases of sets with zero or one members, you can use:
对于具有零个或一个成员的集合的特殊情况,您可以使用:
java.util.Collections.EMPTY_SET
and:
和:
java.util.Collections.singleton("A")
回答by Stuart Clark
Here's a little method you can use
这里有一个你可以使用的小方法
/**
* Utility method analogous to {@link java.util.Arrays#asList(Object[])}
*
* @param ts
* @param <T>
* @return the set of all the parameters given.
*/
@SafeVarargs
@SuppressWarnings("varargs")
public static <T> Set<T> asSet(T... ts) {
return new HashSet<>(Arrays.asList(ts));
}
回答by Holly Cummins
In Java 9, similar function has been added via factory methods:
在 Java 9 中,通过工厂方法添加了类似的功能:
Set<String> oneLinerSet = Set.of("a", "b", ...);
(There are equivalents for List
as well.)
(也有等价物List
。)
回答by Philippe Gioseffi
Another way to do it using Java 8 and enums would be:
使用 Java 8 和枚举的另一种方法是:
Set<String> set = EnumSet.of(StandardOpenOption.CREATE, StandardOpenOption.READ);
See EnumSet.
请参阅EnumSet。
I would recommend a performance analysis between this approach and
我建议在这种方法和
Set<String> set = Stream.of(StandardOpenOption.CREATE, StandardOpenOption.READ).collect(Collectors.toSet());
because if you have more than five elements the javadoc of the method states that may be performance issues as you can see in the javadoc of Set.Of(E, E...).
因为如果您有五个以上的元素,则该方法的 javadoc 声明可能是性能问题,正如您在Set.Of(E, E...)的 javadoc 中看到的那样。