java 用于将对象包装在集合中的实用方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10105799/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 23:38:47  来源:igfitidea点击:

Utility method for wrapping an object in a collection

javacollections

提问by Tom Hawtin - tackline

I'm looking for a static method in the Java core libraries or some other commonly used dependency — preferably one of Apache — that does the following:

我正在 Java 核心库或其他一些常用依赖项(最好是 Apache 之一)中寻找静态方法,它执行以下操作:

public static <T> Collection<T> wrap(final T object){
    final Collection<T> collection = new ArrayList<T>();
    collection.add(object);
    return collection;
}

Do you know where such a method already exists? Since I guess the problem is common, I don't want to duplicate it's solution.

你知道哪里已经有这样的方法了吗?因为我猜这个问题很常见,所以我不想复制它的解决方案。

回答by Tom Hawtin - tackline

java.util.Collections.singleton(object)will give you an immutable Set. singletonListis also available.

java.util.Collections.singleton(object)会给你一个不变的SetsingletonList也可用。

Less efficiently java.util.Arrays.asList(object)will give you a mutable (can use list.set(0, x);), but non-structurally changeable (can't add or remove) List. It is a bit more expensive as there is an extra array that is created client-side.

效率较低java.util.Arrays.asList(object)会给你一个可变的(可以使用list.set(0, x);),但非结构可变的(不能添加或删除)List。它有点贵,因为有一个额外的数组是在客户端创建的。

回答by Sheepy

Here are some efficient ways to wrap Java object(s) in List, as of Java 8.

从 Java 8 开始,这里有一些在 List 中包装 Java 对象的有效方法。

Collections.singletonList: Single item, immutable, since 1.3.
Collections.singletonList( object )
High performance backed by internal class.

收藏。singletonList:单个项目,不可变,自 1.3 起。
Collections.singletonList( object )
内部类支持的高性能。

Collections.nCopies: One object, zeroto many items, immutable, since 1.2.
Collections.nCopies( number_of_copy, object )
High performance backed by internal class. All items point to same object.

收藏。nCopies:一个对象,到多个项目,不可变,自 1.2 起。
Collections.nCopies( number_of_copy, object )
内部类支持的高性能。所有项目都指向同一个对象。

Array.asList: Any number of objects, sizeimmutable (individual elements mutable), since 1.2.
Arrays.asList( object1, object2, object3 )
Backed by internal class. Items are converted to array at compile timeand this array directly backs the List.

大批。asList:任意数量的对象,大小不可变(单个元素可变),自 1.2 起。
Arrays.asList( object1, object2, object3 )
由内部类支持。项目在编译时转换为数组该数组直接支持列表。

new ArrayList(Collection): Any number of objects, mutable, since 1.2
new ArrayList<>( Arrays.asList( object1, object2, object3 ) )
The ArrayList is created with an array cloneand an array copy, and so does not use any loops.

new ArrayList(Collection):任意数量的对象,可变的,自 1.2 起
new ArrayList<>( Arrays.asList( object1, object2, object3 ) )
ArrayList 是使用数组 clone数组 copy 创建的,因此不使用任何循环。

回答by NimChimpsky

Immutable list in guava

番石榴中的不可变列表

public static <E> ImmutableList<E> of(E element)

Returns an immutable list containing a single element. This list behaves and performs comparably to Collections.singleton(T), but will not accept a null element. It is preferable mainly for consistency and maintainability of your code.

返回包含单个元素的不可变列表。此列表的行为和性能与 Collections.singleton(T) 相当,但不接受空元素。主要是为了代码的一致性和可维护性。

回答by Martijn Courteaux

Don't be afraid of writing something yourself. As far as I know it doesn't exist. I think a reason for this is that the utility method decides which implementation of Collection it uses. In your case you chose for ArrayList, but there are a whole bunch of other collections.

不要害怕自己写一些东西。据我所知它不存在。我认为这样做的一个原因是实用程序方法决定了它使用的 Collection 实现。在您的情况下,您选择了 ArrayList,但还有一大堆其他集合。

回答by sharakan

java.util.Collections.singletonList() or singleton(). Note though that the result is immutable.

java.util.Collections.singletonList() 或 singleton()。请注意,结果是不可变的。