📜  将列表转换为Java中的集合

📅  最后修改于: 2022-05-13 01:55:20.524000             🧑  作者: Mango

将列表转换为Java中的集合

Java Set 是Java.util 包的一部分,扩展了Java.util.Collection 接口。它不允许使用重复元素,并且最多只能容纳一个空元素。 Java Set 接口的几个重要特性如下:

  • set 接口是对象的无序集合,其中不能存储重复值。
  • Java Set 不提供对元素插入或删除位置的控制。
  • 基本上,Set 是由 HashSet、LinkedHashSet 或 TreeSet(排序表示)实现的。
  • Set 有多种方法来添加、删除 clear、size 等,以增强该接口的使用。

在这篇文章中,讨论了在Java中将 List 接口转换为 Set 的不同方法:

  1. 天真的方法:天真的解决方案是创建一个空集并将指定列表的每个元素添加到新创建的集合中。

    以下是天真的方法的实现:

    // Java Program to convert
    // List to Set in Java 8
      
    import java.util.*;
    import java.util.stream.*;
    import java.util.function.Function;
      
    class GFG {
      
        // Generic function to convert list to set
        public static  Set convertListToSet(List list)
        {
            // create an empty set
            Set set = new HashSet<>();
      
            // Add each element of list into the set
            for (T t : list)
                set.add(t);
      
            // return the set
            return set;
        }
      
        public static void main(String args[])
        {
      
            // Create a stream of integers
            List list = Arrays.asList("GeeksForGeeks",
                                              "Geeks",
                                              "forGeeks",
                                              "A computer portal",
                                              "for",
                                              "Geeks");
      
            // Print the List
            System.out.println("List: " + list);
      
            // Convert List to stream
            Set set = convertListToSet(list);
      
            // Print the Set
            System.out.println("Set from List: " + set);
        }
    }
    

    输出:

    List: [GeeksForGeeks, Geeks, forGeeks, A computer portal, for, Geeks]
    Set from List: [Geeks, for, GeeksForGeeks, A computer portal, forGeeks]
    
  2. 使用构造函数: HashSet 构造函数可以采用另一个集合对象来构造一个包含指定列表元素的新集合。

    下面是上述方法的实现:

    // Java Program to convert
    // List to Set in Java 8
      
    import java.util.*;
    import java.util.stream.*;
      
    class GFG {
      
        // Generic function to convert list to set
        public static  Set convertListToSet(List list)
        {
            // create a set from the List
            return new HashSet<>(list);
        }
      
        public static void main(String args[])
        {
      
            // Create a stream of integers
            List list = Arrays.asList("GeeksForGeeks",
                                              "Geeks",
                                              "forGeeks",
                                              "A computer portal",
                                              "for",
                                              "Geeks");
      
            // Print the List
            System.out.println("List: " + list);
      
            // Convert List to stream
            Set set = convertListToSet(list);
      
            // Print the Set
            System.out.println("Set from List: " + set);
        }
    }
    

    输出:

    List: [GeeksForGeeks, Geeks, forGeeks, A computer portal, for, Geeks]
    Set from List: [Geeks, for, GeeksForGeeks, A computer portal, forGeeks]
    
  3. 使用Java 8 Stream API :HashSet 构造函数可以采用另一个集合对象来构造一个包含指定列表元素的新集合。

    下面是上述方法的实现:

    // Java Program to convert
    // List to Set in Java 8
      
    import java.util.*;
    import java.util.stream.*;
      
    class GFG {
      
        // Generic function to convert list to set
        public static  Set convertListToSet(List list)
        {
            // create a set from the List
            return list.stream().collect(Collectors.toSet());
        }
      
        public static void main(String args[])
        {
      
            // Create a stream of integers
            List list = Arrays.asList("GeeksForGeeks",
                                              "Geeks",
                                              "forGeeks",
                                              "A computer portal",
                                              "for",
                                              "Geeks");
      
            // Print the List
            System.out.println("List: " + list);
      
            // Convert List to stream
            Set set = convertListToSet(list);
      
            // Print the Set
            System.out.println("Set from List: " + set);
        }
    }
    

    输出:

    List: [GeeksForGeeks, Geeks, forGeeks, A computer portal, for, Geeks]
    Set from List: [Geeks, for, GeeksForGeeks, A computer portal, forGeeks]
    
  4. 使用 Guava Sets.newHashSet() : Sets.newHashSet() 创建一个可变的 HashSet 实例,其中包含指定列表的元素。

    下面是上述方法的实现:

    // Java Program to convert
    // List to Set in Java 8
      
    import java.util.*;
    import java.util.stream.*;
      
    class GFG {
      
        // Generic function to convert list to set
        public static  Set convertListToSet(List list)
        {
            // create a set from the List
            return return Sets.newHashSet(list);
        }
      
        public static void main(String args[])
        {
      
            // Create a stream of integers
            List list = Arrays.asList("GeeksForGeeks",
                                              "Geeks",
                                              "forGeeks",
                                              "A computer portal",
                                              "for",
                                              "Geeks");
      
            // Print the List
            System.out.println("List: " + list);
      
            // Convert List to stream
            Set set = convertListToSet(list);
      
            // Print the Set
            System.out.println("Set from List: " + set);
        }
    }
    

    输出:

    List: [GeeksForGeeks, Geeks, forGeeks, A computer portal, for, Geeks]
    Set from List: [Geeks, for, GeeksForGeeks, A computer portal, forGeeks]