當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


Java List轉Set用法及代碼示例


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 <T> Set<T> convertListToSet(List<T> list)
        {
            // create an empty set
            Set<T> 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<String> list = Arrays.asList("GeeksForGeeks",
                                              "Geeks",
                                              "forGeeks",
                                              "A computer portal",
                                              "for",
                                              "Geeks");
      
            // Print the List
            System.out.println("List:" + list);
      
            // Convert List to stream
            Set<String> 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 <T> Set<T> convertListToSet(List<T> list)
        {
            // create a set from the List
            return new HashSet<>(list);
        }
      
        public static void main(String args[])
        {
      
            // Create a stream of integers
            List<String> list = Arrays.asList("GeeksForGeeks",
                                              "Geeks",
                                              "forGeeks",
                                              "A computer portal",
                                              "for",
                                              "Geeks");
      
            // Print the List
            System.out.println("List:" + list);
      
            // Convert List to stream
            Set<String> 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 流 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 <T> Set<T> convertListToSet(List<T> 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<String> list = Arrays.asList("GeeksForGeeks",
                                              "Geeks",
                                              "forGeeks",
                                              "A computer portal",
                                              "for",
                                              "Geeks");
      
            // Print the List
            System.out.println("List:" + list);
      
            // Convert List to stream
            Set<String> 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 <T> Set<T> convertListToSet(List<T> list)
        {
            // create a set from the List
            return return Sets.newHashSet(list);
        }
      
        public static void main(String args[])
        {
      
            // Create a stream of integers
            List<String> list = Arrays.asList("GeeksForGeeks",
                                              "Geeks",
                                              "forGeeks",
                                              "A computer portal",
                                              "for",
                                              "Geeks");
      
            // Print the List
            System.out.println("List:" + list);
      
            // Convert List to stream
            Set<String> 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]
    




相關用法


注:本文由純淨天空篩選整理自RishabhPrabhu大神的英文原創作品 Convert List to Set in Java。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。