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.
Pass the List<String> as a parameter to the constructor of a new ArrayList<Object> . List<Object> objectList = new ArrayList<Object>(stringList);
// 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.
List<Integer> intList = strList.stream() .map(Integer::valueOf) .collect(Collectors.toList());
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With