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

Java 8Object Oriented ProgrammingProgramming

Here’s our List of String −

List<String> listString = Arrays.asList("25", "50", "75", "100", "125", "150");

Now, convert the list of string to list of integer −

List<Integer> listInteger = listString.stream().map(Integer::parseInt).collect(Collectors.toList());

Following is the program to convert List of String to List of Integer in Java −

Example

import java.util.*;
import java.util.stream.*;
import java.util.function.*;
public class Demo {
   public static void main(String[] args) {
      List<String> listString = Arrays.asList("25", "50", "75", "100", "125", "150");
      System.out.println("List of String = " + listString);
      List<Integer> listInteger = listString.stream().map(Integer::parseInt)
         .collect(Collectors.toList());
      System.out.println("List of Integer (converted from List of String) = " + listInteger);
   }
}

Output

List of String = [25, 50, 75, 100, 125, 150]
List of Integer (converted from List of String) = [25, 50, 75, 100, 125, 150]
raja
Published on 23-Sep-2019 12:57:55
Advertisements