Lambda expression to convert array/List of String to array/List of Integers
You could create helper methods that would convert a list (array) of type T
to a list (array) of type U
using the map
operation on stream
.
//for lists
public static <T, U> List<U> convertList(List<T> from, Function<T, U> func) {
return from.stream().map(func).collect(Collectors.toList());
}
//for arrays
public static <T, U> U[] convertArray(T[] from,
Function<T, U> func,
IntFunction<U[]> generator) {
return Arrays.stream(from).map(func).toArray(generator);
}
And use it like this:
//for lists
List<String> stringList = Arrays.asList("1","2","3");
List<Integer> integerList = convertList(stringList, s -> Integer.parseInt(s));
//for arrays
String[] stringArr = {"1","2","3"};
Double[] doubleArr = convertArray(stringArr, Double::parseDouble, Double[]::new);
Note that
s -> Integer.parseInt(s)
could be replaced with Integer::parseInt
(see Method references) Convert array of Strings to list of Integers?
You only need to stream once.
Instead of using int Integer::parseInt(String s)
, you should use Integer Integer::valueOf(String s)
, so you don't have to call boxed()
or rely on auto-boxing.
Then use collect(Collectors.toList())
directly, instead of creating intermediate array first.
List<Integer> allAnswerList = Arrays.stream(allAnswers) // stream of String
.map(Integer::valueOf) // stream of Integer
.collect(Collectors.toList());
How to simplify a lambda method that receives a list of integers and returns a string by adding a letter before the number?
You can simply map
to String as:
return list.stream()
.map(x -> x % 2 == 0 ? "e" + x : "o" + x)
.collect(Collectors.joining(", "));
Converting a List of Strings to a List of custom made objects in Java using Lambda Expression
If you add a constructor to your Person
class that takes a string, you can just use:
List<Person> people = persons.stream()
.map(Person::new)
.collect(Collectors.toList());
If you don't have a constructor, then you will need a lambda:
List<Person> people = persons.stream()
.map(s -> { Person p = new Person(); p.setName(s); return p; })
.collect(Collectors.toList());
Convert ListString to ListInteger directly
No, you need to loop over the array
for(String s : strList) intList.add(Integer.valueOf(s));
Java 8 lambda create list of Strings from list of objects
You need to collect
your Stream
into a List
:
List<String> adresses = users.stream()
.map(User::getAdress)
.collect(Collectors.toList());
For more information on the different Collectors
visit the documentation.
User::getAdress
is just another form of writing (User user) -> user.getAdress()
which could as well be written as user -> user.getAdress()
(because the type User
will be inferred by the compiler)
ListString to Map using Lambda expressions
You need to iterate. Here's one-line using an int stream:
IntStream.range(0, fillMyList.size()).forEach(i -> fillMyList.set(i, ""));
Using lambda expression of Java 8, convert ListByte to primitive byte[], without external libraries
You can try doing like this:
listByte.stream().collect(ByteArrayOutputStream::new, (baos, i) -> baos.write((byte) i),
(baos1, baos2) -> baos1.write(baos2.toByteArray(), 0, baos2.size()))
.toByteArray()
Error trying to convert a ListString to ListInteger
You should split
the lines of String
using the delimeter ,
and flatMap
them to collect as a List.
lineas = bf.lines()
.flatMap(s -> Arrays.stream(s.split(",")))
.collect(Collectors.toList());
then return the List<Integer>
mapped as:
return lineas.stream()
.map(Integer::parseInt)
.collect(Collectors.toList());
Related Topics
Bidirectional Multi-Valued Map in Java
Differencebetween Class.This and This in Java
Spring Data JPA - "No Property Found for Type" Exception
Declaring and Initializing Variables Within Java Switches
Load Resource from Anywhere in Classpath
Swingworker, Thread.Sleep(), or Javax.Swing.Timer? I Need to "Insert a Pause"
How to Programmatically Set the Sslcontext of a Jax-Ws Client
Convert String to Calendar Object in Java
Method to Dynamically Load Java Class Files
Using Jasperreports with a Relative Path
Flutter Doctor --Android-Licenses Gives a Java Error
Lambda Expression to Convert Array/List of String to Array/List of Integers
How to Lower Java Heap When Not in Use