📌  相关文章
📜  Java中将字符串列表转换为整数列表的程序

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

Java中将字符串列表转换为整数列表的程序

Java.util.List 是 Collection 的子接口。它是可以存储重复值的对象的有序集合。由于 List 保留了插入顺序,它允许元素的位置访问和插入。 List 接口由 ArrayList、LinkedList、Vector 和 Stack 类实现。

  1. 使用Java 8 Stream API :Stream 是一系列支持各种方法的对象,这些方法可以流水线化以产生所需的结果。

    Java 8 Stream API 可用于转换List列出.

    算法

    1. 获取字符串列表。
    2. 将字符串列表转换为字符串流。这是使用 List.stream() 完成的。
    3. 将字符串流转换为整数流。这是使用 Stream.map() 并将 Integer.parseInt() 方法作为 lambda 表达式传递来完成的。
    4. 将整数流收集到整数列表中。这是使用 Collectors.toList() 完成的。
    5. 返回/打印字符串列表。

    程序:

    // Java Program to convert
    // List to List in Java 8
      
    import java.util.*;
    import java.util.stream.*;
    import java.util.function.*;
      
    class GFG {
      
        // Generic function to convert List of
        // String to List of Integer
        public static  List
        convertStringListToIntList(List listOfString,
                                   Function function)
        {
            return listOfString.stream()
                .map(function)
                .collect(Collectors.toList());
        }
      
        public static void main(String args[])
        {
      
            // Create a list of String
            List listOfString = Arrays.asList("1", "2", "3",
                                                           "4", "5");
      
            // Print the list of String
            System.out.println("List of String: " + listOfString);
      
            // Convert List of String to list of Integer
            List listOfInteger = convertStringListToIntList(
                listOfString,
                Integer::parseInt);
      
            // Print the list of Integer
            System.out.println("List of Integer: " + listOfInteger);
        }
    }
    
    输出:
    List of String: [1, 2, 3, 4, 5]
    List of Integer: [1, 2, 3, 4, 5]
    
  2. 使用番石榴的 List.transform()

    算法

    1. 获取字符串列表。
    2. 使用 Lists.transform() 将字符串列表转换为整数列表。这是使用传递 Integer.parseInt() 方法作为转换的 lambda 表达式来完成的。
    3. 返回/打印字符串列表。

    程序:

    // Java Program to convert
    // List to List in Java 8
      
    import com.google.common.base.Function;
    import com.google.common.collect.Lists;
    import java.util.*;
    import java.util.stream.*;
      
    class GFG {
      
        // Generic function to convert List of
        // String to List of Integer
        public static  List
        convertStringListToIntList(List listOfString,
                                   Function function)
        {
            return Lists.transform(listOfString, function);
        }
      
        public static void main(String args[])
        {
      
            // Create a list of String
            List listOfString = Arrays.asList("1", "2", "3",
                                                          "4", "5");
      
            // Print the list of String
            System.out.println("List of String: " + listOfString);
      
            // Convert List of String to list of Integer
            List
                listOfInteger = convertStringListToIntList(listOfString,
                                                     Integer::parseInt);
      
            // Print the list of Integer
            System.out.println("List of Integer: " + listOfInteger);
        }
    }
    
    输出:
    List of String: [1, 2, 3, 4, 5]
    List of Integer: [1, 2, 3, 4, 5]