Java 将 Set<T> 转换为 List<T> 的最简洁方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2319538/
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
Most concise way to convert a Set<T> to a List<T>
提问by Jacques René Mesrine
For example, I am currently doing this:
例如,我目前正在这样做:
Set<String> setOfTopicAuthors = ....
List<String> list = Arrays.asList(
setOfTopicAuthors.toArray( new String[0] ) );
Can you beat this ?
你能打败这个吗?
采纳答案by Schildmeijer
List<String> list = new ArrayList<String>(listOfTopicAuthors);
回答by Adamski
List<String> l = new ArrayList<String>(listOfTopicAuthors);
回答by Prabhakar Manthena
Try this for Set:
试试这个Set:
Set<String> listOfTopicAuthors = .....
List<String> setList = new ArrayList<String>(listOfTopicAuthors);
Try this for Map:
试试这个Map:
Map<String, String> listOfTopicAuthors = .....
// List of values:
List<String> mapValueList = new ArrayList<String>(listOfTopicAuthors.values());
// List of keys:
List<String> mapKeyList = new ArrayList<String>(listOfTopicAuthors.KeySet());
回答by Andrejs
回答by jp093121
not really sure what you're doing exactly via the context of your code but...
通过代码的上下文不太确定你在做什么,但是......
why make the listOfTopicAuthors
variable at all?
为什么要创建listOfTopicAuthors
变量?
List<String> list = Arrays.asList((....).toArray( new String[0] ) );
the "...." represents however your set came into play, whether it's new or came from another location.
“....” 代表您的系列开始使用时,无论它是新的还是来自其他位置。
回答by akhil_mittal
Considering that we have Set<String> stringSet
we can use following:
考虑到我们有Set<String> stringSet
我们可以使用以下内容:
Java 10 (Unmodifiable list)
Java 10(不可修改列表)
List<String> strList = stringSet.stream().collect(Collectors.toUnmodifiableList());
Java 8 (Modifiable Lists)
Java 8(可修改列表)
import static java.util.stream.Collectors.*;
List<String> stringList1 = stringSet.stream().collect(toList());
As per the docfor the method toList()
根据该方法的文档toList()
There are no guarantees on the type, mutability, serializability, or thread-safety of the List returned; if more control over the returned List is required, use toCollection(Supplier).
对返回的 List 的类型、可变性、可序列化性或线程安全性没有任何保证;如果需要对返回的 List 进行更多控制,请使用 toCollection(Supplier)。
So if we need a specific implementation e.g. ArrayList
we can get it this way:
所以如果我们需要一个特定的实现,例如ArrayList
我们可以通过这种方式得到它:
List<String> stringList2 = stringSet.stream().
collect(toCollection(ArrayList::new));
Java 8 (Unmodifiable Lists)
Java 8(不可修改列表)
We can make use of Collections::unmodifiableList
method and wrap the list returned in previous examples. We can also write our own custom method as:
我们可以使用Collections::unmodifiableList
method 并包装前面示例中返回的列表。我们还可以将自己的自定义方法编写为:
class ImmutableCollector {
public static <T> Collector<T, List<T>, List<T>> toImmutableList(Supplier<List<T>> supplier) {
return Collector.of( supplier, List::add, (left, right) -> {
left.addAll(right);
return left;
}, Collections::unmodifiableList);
}
}
And then use it as:
然后将其用作:
List<String> stringList3 = stringSet.stream()
.collect(ImmutableCollector.toImmutableList(ArrayList::new));
Another possibility is to make use of collectingAndThen
method which allows some final transformation to be done before returning result:
另一种可能性是使用collectingAndThen
允许在返回结果之前完成一些最终转换的方法:
List<String> stringList4 = stringSet.stream().collect(collectingAndThen(
toCollection(ArrayList::new),Collections::unmodifiableList));
One point to noteis that the method Collections::unmodifiableList
returns an unmodifiable view of the specified list, as per doc. An unmodifiable view collection is a collection that is unmodifiable and is also a view onto a backing collection. Note that changes to the backing collection might still be possible, and if they occur, they are visible through the unmodifiable view. But the collector method Collectors.unmodifiableList
returns truly immutable list in Java 10.
一点要注意的是,该方法Collections::unmodifiableList
返回指定列表的不可修改视图,按照文档。不可修改的视图集合是不可修改的集合,也是支持集合的视图。请注意,对后备集合的更改可能仍然是可能的,如果发生更改,它们将通过不可修改的视图可见。但是收集器方法Collectors.unmodifiableList
在Java 10 中返回真正不可变的列表。