在 Java 中将 ArrayList 转换为 Set
ArrayList 是 List 接口的实现类,用于以线性顺序存储数据,而 set 是具有 HashSet 类来存储数据的接口。这两者之间的主要区别是 HashSet 不允许重复元素并存储唯一元素。
本篇文章将介绍将 ArrayList 转换为 HashSet 的不同方法。
在 Java 中使用 Naive 方法将 ArrayList 转换为 HashSet
这是将列表转换为集合的最基本方法。
在这种方法中,我们首先声明一个空集。然后我们遍历列表并使用 HashSet.add()
方法将其元素一一添加到集合中。当循环终止时,列表被转换为一个集合。
看看下面的代码。
import java.util.*;
public class SimpleTesting {
public static void main(String args[]) {
// initialise a list
List<String> list = Arrays.asList("Karan", "Rahul", "Jay", "Laxman", "Praful", "Kushagra", "Karan");
//intialise a set
Set<String> set = new HashSet<String>();
// apply for loop
for(String ele : list){
set.add(ele);
}
// display the set
for(String ele : set){
System.out.println(ele);
}
}
}
输出:
Rahul
Kushagra
Jay
Karan
Laxman
Praful
在上面的代码中,我们创建了一个存储一些名称的列表。我们还初始化了一个名为 set 的空 Set。
然后我们遍历列表并添加其元素以一一设置。最后,我们打印集合的元素。请注意如何消除列表中的重复元素。
使用 Java 中的构造函数将 ArrayList 转换为 HashSet
这种方法是所有方法中最简单的。在这种方法中,我们将需要转换的列表传递给 HashSet 类的构造函数。
这消除了之前方法中所需的 for
循环的需要。构造函数返回一个包含所有唯一元素的新集合。
看看下面的代码。
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
public class SimpleTesting {
public static void main(String args[])
{
// initialise a list
List<String> list = Arrays.asList("Karan", "Rahul", "Jay", "Laxman", "Praful", "Kushagra", "Karan");
//intialise a set
Set<String> set = new HashSet<String>(list);
// display the set
for(String ele : set){
System.out.println(ele);
}
}
}
输出:
Rahul
Kushagra
Jay
Karan
Laxman
Praful
在上面的代码中,我们创建了一个存储一些名称的列表。我们还初始化了一个名为 set 的空 Set。
我们将列表作为参数传递给 HashMap 的构造函数。最后,我们打印集合的元素。请注意如何消除列表中的重复元素。
使用 Java 8 Stream API 将 ArrayList 转换为 HashSet
在这种方法中,我们使用 Java 8 Stream API 将 ArrayList 转换为 HashSet。我们首先将列表转换为流。然后我们将 collect
函数应用于流。
为了转换成一个集合,我们在 collect
操作中使用 Collectors.toSet()
函数来获取一个集合。看看下面的代码。
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
public class SimpleTesting {
public static void main(String args[])
{
// initialise a list
List<String> list = Arrays.asList("Karan", "Rahul", "Jay", "Laxman", "Praful", "Kushagra", "Karan");
//intialise a set using stream
Set<String> set = list.stream().collect(Collectors.toSet());
// display the set
for(String ele : set){
System.out.println(ele);
}
}
}
输出:
Rahul
Kushagra
Jay
Karan
Laxman
Praful
我们使用上面代码中的 List.stream()
函数将列表转换为流。我们将 collect
操作应用于流并将 Collectors.toSet()
方法作为参数传递。
这导致将流转换为集合。然后我们将这个集合存储在变量集合中。
使用 Java 中的 Set.copyOf()
方法将 ArrayList 转换为 HashSet
Java 10 提供了一个 Set.copyof()
方法,该方法将 Collection 作为其参数并返回一个包含给定 Collection 元素的不可变集合。在这种方法中,我们使用 Set.copyOf()
方法将 ArrayList 转换为 HashSet。
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
public class SimpleTesting {
public static void main(String args[])
{
// initialise a list
List<String> list = Arrays.asList("Karan", "Rahul", "Jay", "Laxman", "Praful", "Kushagra", "Karan");
//intialise a set
Set<String> set = Set.copyOf(list);
// display the set
for(String ele : set){
System.out.println(ele);
}
}
}
输出:
Rahul
Kushagra
Jay
Karan
Laxman
Praful
在 Java 中使用 Apache Commons 集合库将 ArrayList 转换为 HashSet
如果你正在使用 Apache Commons Collection 库,请使用带有两个参数的 CollectionUtils.addAll()
方法:目标集合对象和源集合对象。
源集合对象的值被复制到目标集合对象中并创建一个集合。看看下面的代码。
此代码使用 Apache 公共库的依赖代码。首先,将此依赖项添加到你的 Java 项目中以成功执行此代码。
<!-- https://mvnrepository.com/artifact/commons-collections/commons-collections -->
<dependency>
<groupId>commons-collections</groupId>
<artifactId>commons-collections</artifactId>
<version>3.2.2</version>
</dependency>
import java.util.*;
public class SimpleTesting {
public static void main(String args[]) {
// initialise a list
List<String> sourcelist = Arrays.asList("Karan", "Rahul", "Jay", "Laxman", "Praful", "Kushagra", "Karan");
//intialise a set
Set<String> targetset = new HashSet<String>();
// Adding elements
CollectionUtils.addAll(targetset, sourcelist);
// display the set
for(String ele : targetset){
System.out.println(ele);
}
}
}
输出:
Rahul
Kushagra
Jay
Karan
Laxman
Praful
相关文章
Do you understand JavaScript closures?
发布时间:2025/02/21 浏览次数:108 分类:JavaScript
-
The function of a closure can be inferred from its name, suggesting that it is related to the concept of scope. A closure itself is a core concept in JavaScript, and being a core concept, it is naturally also a difficult one.
Do you know about the hidden traps in variables in JavaScript?
发布时间:2025/02/21 浏览次数:178 分类:JavaScript
-
Whether you're just starting to learn JavaScript or have been using it for a long time, I believe you'll encounter some traps related to JavaScript variable scope. The goal is to identify these traps before you fall into them, in order to av
How much do you know about the Prototype Chain?
发布时间:2025/02/21 浏览次数:150 分类:JavaScript
-
The prototype chain can be considered one of the core features of JavaScript, and certainly one of its more challenging aspects. If you've learned other object-oriented programming languages, you may find it somewhat confusing when you start
在 Python 中将 Pandas 系列的日期时间转换为字符串
发布时间:2024/04/24 浏览次数:894 分类:Python
-
了解如何在 Python 中将 Pandas 系列日期时间转换为字符串
在 Python Pandas 中使用 str.split 将字符串拆分为两个列表列
发布时间:2024/04/24 浏览次数:1124 分类:Python
-
本教程介绍如何使用 pandas str.split() 函数将字符串拆分为两个列表列。
在 Pandas 中执行 SQL 查询
发布时间:2024/04/24 浏览次数:1195 分类:Python
-
本教程演示了在 Python 中对 Pandas DataFrame 执行 SQL 查询。
在 Pandas 中使用 stack() 和 unstack() 函数重塑 DataFrame
发布时间:2024/04/24 浏览次数:1289 分类:Python
-
本文讨论了 Pandas 中 stack() 和 unstack() 函数的使用。
在 Pandas 中读取 Excel 多张工作表
发布时间:2024/04/24 浏览次数:1450 分类:Python
-
本教程演示如何在 Pandas Python 中从 Excel 工作簿中读取多个 Excel 工作表。