Java Collection to List 的意思是 Collection 到 List 的转换。将 Java 集合从一种类型转换为另一种类型是编程中的一项常见任务。集合是一种包含并处理一组数据的数据结构。集合框架由Set、Queue、Dequeue、List等接口和ArrayList、Vector、Linked List、Priority Queue、Tree Set、Hash Set、Linked Hash Set等类组成。存储在 Collection 中的数据是封装的,只能通过一些预定义的方法来访问这些数据。在本教程中,我们将看到 Collection 到 Array List 的转换。
开始您的免费软件开发课程
网络开发、编程语言、软件测试及其他
语法:
这是用于将 Java Collection 转换为 List 的语法。
List<integer> intVal = values.stream().collect(Collectors.toList());</integer>
Java Collection 必须使用类型声明进行参数化。它使 Java 编译器能够检查用户是否尝试使用具有正确类型的对象的集合。
要列出的 Java 集合示例
让我们看几个例子,可以深入了解集合的转换。
示例#1
代码:
import java.util.*; public class CollectionToArrayList{ public static void main(String[] args){ List<string> list = new ArrayList<string>(); list.add("eduCBA "); list.add("is "); list.add("best "); list.add("platform "); list.add("for "); list.add("Web "); list.add("Development "); list.add("course. "); String[] s = list.toArray(new String[0]); for(int i = 0; i <p><strong>输出:</strong></p> <p><img src="/static/imghwm/default1.png" data-src="https://img.php.cn/upload/article/000/000/000/172500413844531.png?x-oss-process=image/resize,p_40" class="lazy" alt="要列出的 Java 集合" ></p> <p>所以这里我们使用集合框架之一将数据转换为列表。</p> <p>由于 Collection 框架包括 List、Queue、Set 等,我们将数组转换为列表,类似于列表转换为数组的方式。</p> <h4 id="示例-使用-asList">示例 #2:使用 asList</h4> <p><strong>代码:</strong></p> <pre class="brush:php;toolbar:false">import java.util.*; import java.util.stream.*; class Main { public static void main(String args[]) { Integer[] evenArray = { 2, 4, 6, 8, 10, 12, 14 }; List<integer> evemList = Arrays.asList(evenArray); System.out.println("List from array: " + evemList); } }</integer>
输出:
基本上,我们有将数组集合转换为列表的传统方法。但这里我们使用另一种转换方法,即使用 Array 类的 asList 方法。
这里我们使用偶数数组,使用 Array 类的 asList 方法创建整数列表并将其分配给输出。
示例 #3:使用 Collections.addAll() 方法
代码:
import java.util.*; import java.util.stream.*; class Main { public static void main(String args[]) { String stringArr[] = { "Web", "Development", "is", "course", "No.", "1" }; System.out.println("Array Before conversion: " + Arrays.toString(stringArr)); List<string> strList = new ArrayList(); Collections.addAll(strList, stringArr); System.out.println("List after converting: " + strList); } }</string>
输出:
所以,这里我们使用 Collection 类的 addAll() 方法,因为数组和列表都是集合框架的一部分。我们初始化了一个空数组并创建了一个空列表。 Collections.addAll() 方法用于传递列表和数组作为参数。 与 Array、Collections 之一转换为 List 的方式类似,我们将看到 Set、Collection 如何转换为 List。
示例 #4:使用纯 Java
代码:
import java.util.*; class Main { public static void main(String[] args) { Set<string> HashSet = new HashSet<string>(); HashSet.add("Mango"); HashSet.add("Apple"); HashSet.add("Orange"); HashSet.add("Jamun"); HashSet.add("Pine"); HashSet.add("Kiwi"); System.out.println("Set elements are: "); for (String i : HashSet) System.out.print(i + " "); List<string> stringList = new ArrayList<string>(HashSet.size()); for (String i : HashSet) stringList.add(i); System.out.println("\nArrayList:" + stringList); } }</string></string></string></string>
输出:
这里我们声明并初始化一个集合,然后创建列表并将集合元素添加到列表中。
示例#5:使用构造函数
代码:
import java.util.*; class Main { public static void main(String[] args) { Set<string> HashSet = new HashSet<string>(); HashSet.add("Mango"); HashSet.add("Apple"); HashSet.add("Orange"); HashSet.add("Jamun"); HashSet.add("Pine"); HashSet.add("Kiwi"); System.out.println("Hash set :"); for(String string: HashSet) System.out.print(string + " "); List<string> lList = new LinkedList<string>(HashSet); System.out.println ("\nLinked List from set: " + lList); } }</string></string></string></string>
输出:
所以上面的例子是使用构造函数将哈希集、集合转换为列表的另一种方式。我们也使用了上面相同的哈希集,并使用列表构造函数和集合对象作为参数。它将所有集合元素复制到列表对象。
示例 #6:使用 Java 8 流
代码:
import java.util.*; import java.util.stream.*; class Main { public static void main(String[] args) { Set<string> HashSet = new HashSet<string>(); HashSet.add("Mango"); HashSet.add("Apple"); HashSet.add("Orange"); HashSet.add("Jamun"); HashSet.add("Pine"); HashSet.add("Kiwi"); System.out.println("The Hash set:"); for(String string : HashSet) System.out.print(string + " "); List<string> stringList = HashSet.stream().collect(Collectors.toList()); System.out.println("\nList converted: " + stringList); } }</string></string></string>
输出:
这里,我们使用 Java 8 的流和收集方法将 Hash Set 转换为列表。
至此,我们就结束了“Java Collection to List”主题。我们已经看到了将集合转换为列表的通用语法。我们已经看到了集合的转换,包括Array、Set等如何转换为List。通过各种方法实现了数组和集合转换为列表的几个示例,例如使用 addAll() 方法、Java 8 流、通用 Java 类、asList() 方法。 Java 中还有许多其他 Collection 可以转换为 List。谢谢!快乐学习!!
以上是要列出的 Java 集合的详细内容。更多信息请关注PHP中文网其他相关文章!

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于结构化数据处理开源库SPL的相关问题,下面就一起来看一下java下理想的结构化数据处理类库,希望对大家有帮助。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于PriorityQueue优先级队列的相关知识,Java集合框架中提供了PriorityQueue和PriorityBlockingQueue两种类型的优先级队列,PriorityQueue是线程不安全的,PriorityBlockingQueue是线程安全的,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于java锁的相关问题,包括了独占锁、悲观锁、乐观锁、共享锁等等内容,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于多线程的相关问题,包括了线程安装、线程加锁与线程不安全的原因、线程安全的标准类等等内容,希望对大家有帮助。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于枚举的相关问题,包括了枚举的基本操作、集合类对枚举的支持等等内容,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于Java的相关知识,其中主要介绍了关于关键字中this和super的相关问题,以及他们的一些区别,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于平衡二叉树(AVL树)的相关知识,AVL树本质上是带了平衡功能的二叉查找树,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于Java的相关知识,其中主要整理了Stream流的概念和使用的相关问题,包括了Stream流的概念、Stream流的获取、Stream流的常用方法等等内容,下面一起来看一下,希望对大家有帮助。


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章

热工具

Dreamweaver Mac版
视觉化网页开发工具

DVWA
Damn Vulnerable Web App (DVWA) 是一个PHP/MySQL的Web应用程序,非常容易受到攻击。它的主要目标是成为安全专业人员在合法环境中测试自己的技能和工具的辅助工具,帮助Web开发人员更好地理解保护Web应用程序的过程,并帮助教师/学生在课堂环境中教授/学习Web应用程序安全。DVWA的目标是通过简单直接的界面练习一些最常见的Web漏洞,难度各不相同。请注意,该软件中

Dreamweaver CS6
视觉化网页开发工具

EditPlus 中文破解版
体积小,语法高亮,不支持代码提示功能

SublimeText3 Linux新版
SublimeText3 Linux最新版