Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Lambda expression to convert array/List of String to array/List of Integers

Since Java 8 comes with powerful lambda expressions,

I would like to write a function to convert a List/array of Strings to array/List of Integers, Floats, Doubles etc..

In normal Java, it would be as simple as

for(String str : strList){    intList.add(Integer.valueOf(str)); } 

But how do I achieve the same with a lambda, given an array of Strings to be converted to an array of Integers.

like image 580
RaceBase Avatar asked Apr 14 '14 10:04

RaceBase


People also ask

How do I convert a List of strings to a List of objects?

Pass the List<String> as a parameter to the constructor of a new ArrayList<Object> . List<Object> objectList = new ArrayList<Object>(stringList);

How do I print a List in lambda expression?

// Create a list from a String array List list = new ArrayList(); String[] strArr = { "Chris", "Raju", "Jacky" }; for( int i = 0; i < strArr. length; i++ ) { list. add( strArr[i]); } // // Print the key and value of Map using Lambda expression // list. forEach((value) -> {System.


1 Answers

List<Integer> intList = strList.stream()                                .map(Integer::valueOf)                                .collect(Collectors.toList()); 
like image 131
ZehnVon12 Avatar answered Sep 29 '22 16:09

ZehnVon12