Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Program to convert List of String to List of Integer in Java

  • Difficulty Level : Medium
  • Last Updated : 11 Dec, 2018

The Java.util.List is a child interface of Collection. It is an ordered collection of objects in which duplicate values can be stored. Since List preserves the insertion order, it allows positional access and insertion of elements. List Interface is implemented by ArrayList, LinkedList, Vector and Stack classes.

  1. Using Java 8 Stream API: A Stream is a sequence of objects that supports various methods which can be pipelined to produce the desired result.

    Java 8 Stream API can be used to convert List to List.

    Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

    Algorithm:

    1. Get the list of String.
    2. Convert List of String to Stream of String. This is done using List.stream().
    3. Convert Stream of String to Stream of Integer. This is done using Stream.map() and passing Integer.parseInt() method as lambda expression.
    4. Collect Stream of Integer into List of Integer. This is done using Collectors.toList().
    5. Return/Print the list of String.

    Program:






    // Java Program to convert
    // List<String> to List<Integer> 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 <T, U> List<U>
        convertStringListToIntList(List<T> listOfString,
                                   Function<T, U> function)
        {
            return listOfString.stream()
                .map(function)
                .collect(Collectors.toList());
        }
      
        public static void main(String args[])
        {
      
            // Create a list of String
            List<String> 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<Integer> listOfInteger = convertStringListToIntList(
                listOfString,
                Integer::parseInt);
      
            // Print the list of Integer
            System.out.println("List of Integer: " + listOfInteger);
        }
    }
    Output:
    List of String: [1, 2, 3, 4, 5]
    List of Integer: [1, 2, 3, 4, 5]
    
  2. Using Guava’s List.transform():

    Algorithm:

    1. Get the list of String.
    2. Convert List of String to List of Integer using Lists.transform(). This is done using passing Integer.parseInt() method as lambda expression for transformation.
    3. Return/Print the list of String.

    Program:




    // Java Program to convert
    // List<String> to List<Integer> 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 <T, U> List<U>
        convertStringListToIntList(List<T> listOfString,
                                   Function<T, U> function)
        {
            return Lists.transform(listOfString, function);
        }
      
        public static void main(String args[])
        {
      
            // Create a list of String
            List<String> 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<Integer>
                listOfInteger = convertStringListToIntList(listOfString,
                                                     Integer::parseInt);
      
            // Print the list of Integer
            System.out.println("List of Integer: " + listOfInteger);
        }
    }
    Output:
    List of String: [1, 2, 3, 4, 5]
    List of Integer: [1, 2, 3, 4, 5]
    



    My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!