sep*_*p2k 1028
Set<Foo> foo = new HashSet<Foo>(myList);
Run Code Online (Sandbox Code Playgroud)
Spi*_*ina 140
我同意sepp2k,但还有一些其他细节可能很重要:
new HashSet<Foo>(myList);
Run Code Online (Sandbox Code Playgroud)
会给你一个没有重复的未排序集.在这种情况下,使用对象上的.equals()方法识别复制.这与.hashCode()方法结合使用.(关于平等的更多信息,请看这里)
给出排序集的替代方法是:
new TreeSet<Foo>(myList);
Run Code Online (Sandbox Code Playgroud)
如果Foo实现Comparable,这可以工作.如果没有,那么你可能想要使用比较器:
Set<Foo> lSet = new TreeSet<Foo>(someComparator);
lSet.addAll(myList);
Run Code Online (Sandbox Code Playgroud)
这取决于compareTo()(来自可比较的接口)或compare()(来自比较器)以确保唯一性.因此,如果您只关心唯一性,请使用HashSet.如果您在排序后,请考虑TreeSet.(请记住:稍后进行优化!)如果时间效率很重要,如果空间效率很重要,请使用HashSet,请查看TreeSet.请注意,通过Trove(和其他位置)可以更有效地实现Set和Map.
Vit*_*nko 68
如果您使用Guava库:
Set<Foo> set = Sets.newHashSet(list);
Run Code Online (Sandbox Code Playgroud)
或更好:
Set<Foo> set = ImmutableSet.copyOf(list);
Run Code Online (Sandbox Code Playgroud)
Jim*_*imB 27
使用java 8你可以使用stream:
List<Integer> mylist = Arrays.asList(100, 101, 102);
Set<Integer> myset = mylist.stream().collect(Collectors.toSet()));
Run Code Online (Sandbox Code Playgroud)
San*_*waj 17
Set<E> alphaSet = new HashSet<E>(<your List>);
Run Code Online (Sandbox Code Playgroud)
或完整的例子
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
public class ListToSet
{
public static void main(String[] args)
{
List<String> alphaList = new ArrayList<String>();
alphaList.add("A");
alphaList.add("B");
alphaList.add("C");
alphaList.add("A");
alphaList.add("B");
System.out.println("List values .....");
for (String alpha : alphaList)
{
System.out.println(alpha);
}
Set<String> alphaSet = new HashSet<String>(alphaList);
System.out.println("\nSet values .....");
for (String alpha : alphaSet)
{
System.out.println(alpha);
}
}
}
Run Code Online (Sandbox Code Playgroud)
Abd*_*kir 11
请记住,从 List 转换为 Set 将从集合中删除重复项,因为 List 支持重复项,但 Set 不支持 Java 中的重复项。
直接转换:将列表转换为集合的最常见和最简单的方法
// Creating a list of strings
List<String> list = Arrays.asList("One", "Two", "Three", "Four");
// Converting a list to set
Set<String> set = new HashSet<>(list);
Run Code Online (Sandbox Code Playgroud)
Apache Commons Collections :您还可以使用 Commons Collections API 将列表转换为集合:-
// Creating a list of strings
List<String> list = Arrays.asList("One", "Two", "Three", "Four");
// Creating a set with the same number of members in the list
Set<String> set = new HashSet<>(4);
// Adds all of the elements in the list to the target set
CollectionUtils.addAll(set, list);
Run Code Online (Sandbox Code Playgroud)
使用流:另一种方法是将给定列表转换为流,然后流到设置:-
// Creating a list of strings
List<String> list = Arrays.asList("One", "Two", "Three", "Four");
// Converting to set using stream
Set<String> set = list.stream().collect(Collectors.toSet());
Run Code Online (Sandbox Code Playgroud)
我会在转换为set之前执行Null检查.
if(myList != null){
Set<Foo> foo = new HashSet<Foo>(myList);
}
Run Code Online (Sandbox Code Playgroud)
使用 Java 10,您现在可以Set#copyOf
轻松地将 a 转换List<E>
为不可修改的Set<E>
:
例子:
var set = Set.copyOf(list);
Run Code Online (Sandbox Code Playgroud)
请记住,这是一个无序操作,不允许null
使用元素,因为它会抛出.NullPointerException
如果您希望它是可修改的,那么只需将其传递到构造函数中即可Set
实现。
你可以转换List<>
为Set<>
Set<T> set=new HashSet<T>();
//Added dependency -> If list is null then it will throw NullPointerExcetion.
Set<T> set;
if(list != null){
set = new HashSet<T>(list);
}
Run Code Online (Sandbox Code Playgroud)
对于Java 8,它非常简单:
List < UserEntity > vList= new ArrayList<>();
vList= service(...);
Set<UserEntity> vSet= vList.stream().collect(Collectors.toSet());
Run Code Online (Sandbox Code Playgroud)
让我们不要忘记我们相对较新的朋友java-8流API.如果您需要在将列表转换为集合之前预先处理列表,那么最好具有以下内容:
list.stream().<here goes some preprocessing>.collect(Collectors.toSet());
Run Code Online (Sandbox Code Playgroud)
小智 5
使用构造函数的最佳方式
Set s= new HashSet(list);
Run Code Online (Sandbox Code Playgroud)
在 java 8 中,您还可以使用流 api::
Set s= list.stream().collect(Collectors.toSet());
Run Code Online (Sandbox Code Playgroud)
Java - addAll
set.addAll(aList);
Run Code Online (Sandbox Code Playgroud)
Java的 - 新对象
new HashSet(list)
Run Code Online (Sandbox Code Playgroud)
Java-8
list.stream().collect(Collectors.toSet());
Run Code Online (Sandbox Code Playgroud)
使用Guva
Sets.newHashSet(list)
Run Code Online (Sandbox Code Playgroud)
Apache Commons
CollectionUtils.addAll(targetSet, sourceList);
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
504032 次 |
最近记录: |