Java List转HashSet用法及代码示例

您所在的位置:网站首页 hashset转为list Java List转HashSet用法及代码示例

Java List转HashSet用法及代码示例

2024-01-08 05:38| 来源: 网络整理| 查看: 265

List 接口提供了一种存储有序集合的方法。它是可以存储重复值的对象的有序集合。由于 List 保留了插入顺序,它允许元素的位置访问和插入。

HashSet 类允许空元素。该类还为添加、删除、包含和大小等基本操作提供恒定的时间性能,假设哈希函数将元素正确地分散在桶中,我们将在本文中进一步了解。

Note:The set doesn’t allow to store duplicate values. Therefore, any duplicate value in list will be ignored. 

List转HashSet的方法:

将列表对象作为 HashSet 中的参数传递。使用循环将 List 的每个元素添加到 HashSet 中。使用 Set 类的 addAll() 方法。在 Java 中使用流

方法一:传递列表对象作为参数 在哈希集中

我们使用 HashSet 构造函数将其转换为 List。

Java // Java program to demonstrate conversion of // list to set using constructor import java.util.*; class GFG {     public static void main(String[] args)     {         // Create a List         List L = new ArrayList();                 // Add values to the List         L.add("Aragorn");         L.add("Gandalf");         L.add("Legolas");         L.add("Frodo");                 // Create a Set and pass List object as parameter         HashSet S = new HashSet(L);                 // Print values of Set         System.out.println("HashSet Elements are:");                 // since the set is of string type, create a String         // object to iterate through set         for (String ob:S)         {             System.out.println(ob);         }     } }输出HashSet Elements are: Aragorn Frodo Gandalf Legolas

方法2:使用循环将List的每个元素添加到HashSet中

我们只需创建一个列表。我们遍历给定的 List 并将元素一一添加到 Set 中。

Java // Java program to demonstrate conversion of // list to set using simple traversal import java.util.*; class GFG {     public static void main(String[] args)     {         // Create a List         List L = new ArrayList();                 // Add values to the List         L.add(1);         L.add(4);         L.add(30);         L.add(100);         L.add(15);         L.add(30);                 // Create a Set and pass List object as parameter         HashSet S = new HashSet();                 // add each element of list into set         for (Integer ob:L)         {             S.add(ob);         }                 // Print values of Set         System.out.println("HashSet Elements are:");                 // Create an Object ob that will automatically         // identify the type of object of HashSet to iterate         // through set         for (Object ob:S)         {             System.out.println(ob);         }     } }输出HashSet Elements are: 1 4 100 30 15

方法3:使用addAll() 方法集合类

java.util.Set.addAll(Collection C) 方法用于将提到的集合中的所有元素附加到现有集合中。元素是随机添加的,不遵循任何特定顺序。

用法:

boolean addAll(Collection C)

参数:参数 C 是要添加到集合中的任何类型的集合。

返回值:如果成功地将集合 C 的元素附加到此 Set,则该方法返回 true,否则返回 False。

Java // Java program to demonstrate conversion of // Set to array using addAll() method. import java.util.*; class GFG {     public static void main(String[] args)     {         // Create a List         List L = new ArrayList();                 // Add values to the List         L.add(1);         L.add(4);         L.add(30);         L.add(100);         L.add(15);         L.add(30);                 Set S = new HashSet();                 // Use addAll() method         S.addAll(L);                 // Print values of Set         System.out.println("HashSet Elements are:");                 // Create an Object ob that will automatically         // identify the type of object of HashSet to iterate         // through set         for (Object ob:S)         {             System.out.println(ob);         }     } }输出HashSet Elements are: 1 4 100 30 15

方法四:使用流我n Java

注意:Stream 仅适用于 Java8 或更高版本。

我们在java中使用流将给定的列表转换为流,然后流到集合。这仅适用于 Java 8 或之后的版本。

Java // Java program to demonstrate conversion of // Set to list using stream import java.util.*; class GFG {     public static void main(String[] args)     {         // Create a List         List L = new ArrayList();                 // Add values to the List         L.add("Rohan");         L.add("Ritik");         L.add("Yogesh");         L.add("Sangeeta");         L.add("Palak");         L.add("Laxmi");                 // create a stream from List and convert it into a         // Set         Set S = L.stream().collect(Collectors.toSet());                 // Print values of Set         System.out.println("HashSet Elements are:");                 // Create an Object ob that will automatically         // identify the type of object of HashSet to iterate         // through set         for (String ob:S)         {             System.out.println(ob);         }     } }

输出:

HashSet Elements are: 1 4 100 30 15



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3