What is the lambda expression to convert array/List of String to array/List of Integers in java?

Java 8Object Oriented ProgrammingProgramming

You can convert an array list of strings to an array list of integers as:

Example

Live Demo

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

public class LamdaExpressions {
   public static void main(String args[]) {
      ArrayList <String> list = new ArrayList<String>();
      list.add("123");
      list.add("223");
      list.add("323");
      list.add("334");
      List<Integer> listInteger =
         list.stream().map(s -> Integer.parseInt(s)).collect(Collectors.toList());
      System.out.println(listInteger);
   }
}

Output

[123, 223, 323, 334]
raja
Published on 09-Jan-2018 13:00:15
Advertisements