JAVA8: Map list of objects to String[] [duplicate] - java

This question already has answers here:
How to convert a Java 8 Stream to an Array?
(9 answers)
Closed 5 years ago.
I have a List of 'Client' objects each one with a field "email".
I need something like:
List<String> listEmails = clients.stream().map(client->client.getEmail())
.collect(Collectors.toList());
...but returning directly a String[].
Is there a proper way to map a List<Client> to a String[] listEmails using Java 8 streams?

Sure :
String[] result = clients
.stream()
.map(client->client.getEmail())
.toArray(String[]::new)

Related

transform List using lambda expressions [duplicate]

This question already has answers here:
How can I turn a List of Lists into a List in Java 8?
(12 answers)
Closed 2 years ago.
I have created a function to transform the elements of a list:
private List<Hostel> build(List<Hotel> hotels) {
return hotels.stream().map(h -> convert(h)).collect(toList());
}
but I have a compilation error:
required type: List<Hostel>
Provided: List<List<Hostel>>
From your error it seems convert(h) return a List<Hostel>, for that when you use a map, and collect the result is List<List<Hostel>>, to get List<Hostel>, you have to use flatMap instead of map, like this:
.flatMap(h -> convert(h).stream())

How do I go from Stream<ArrayList<Object>>> to ArrayList<Object> in Java? [duplicate]

This question already has answers here:
Java 8 Streams FlatMap method example
(7 answers)
Closed 3 years ago.
I have a Stream<ArrayList<Object>> and I want to "extract" the ArrayList from it and assign it to a variable. How do I do that?
My resulting variable needs to be of type ArrayList<Object> so I can iterate over it and do stuff.
If you want to get one ArrayList then use
ArrayList<Object> result = strm.flatMap(ArrayList::stream)
.collect(Collectors.toCollection(ArrayList::new));
Stream.flatMap method lets you replace each value of a stream with
another stream and then concatenates all the generated streams into a single stream.
List<Object> objectList = new ArrayList<>();
List<Object> collect = Stream.of(objectList)
.flatMap(m -> m.stream())
.collect(Collectors.toList());

Java streams statement: find the error [duplicate]

This question already has answers here:
Why does this java 8 stream operation evaluate to Object instead of List<Object> or just List?
(2 answers)
Why do we have to cast the List returned by Collectors.toList() to List<Integer> even though the elements of the Stream are already mapped to Integer? [duplicate]
(1 answer)
What is a raw type and why shouldn't we use it?
(16 answers)
Closed 4 years ago.
This works as expected
Collection<String> items = combo.getItems();
items.stream().filter(item -> item.startsWith("New")).findFirst()...
But this fails to compile. Why?
Collection items = combo.getItems();
items.stream().map(Object::toString).filter(item -> item.startsWith("New")).findFirst()...
^^^^^^^^^^

Java 8: Map of field to list of items [duplicate]

This question already has answers here:
Java 8 lambdas group list into map
(2 answers)
Closed 5 years ago.
Lets say I have a List<Person> Gathering and I want a Map<String, List<Person>>, mapping Person.surname to a List of Person:s that have the same surname. Is there a convenient way to do this using streams?
Yes, by using Collectors.groupingBy(...):
Map<String, List<Person>> personsBySurname = gathering.stream()
.collect(Collectors.groupingBy(Person::get‌​Surname));

How to map from one container into another w/Java 8 streams? [duplicate]

This question already has answers here:
Collection to stream to a new collection
(4 answers)
Closed 7 years ago.
So, in Clojure, I can just say something like this...
(into [] (map some-function some-collection))
And I get a new vector of my transformed data.
Is there some simple equivalent of into for Java 8 streams? For example, I don't see a constructor on ArrayList that takes a stream, nor do I see some sort of helper function in java.util.Collections, nor the stream interface.
You can do it using Collectors:
someCollection.stream()
.map(someFunction)
.collect(Collectors.toList());
You can do other cool stuff with Collectors, as explained in its javadoc:
Map<Department, Integer> totalByDept =
employees.stream()
.collect(Collectors.groupingBy(Employee::getDepartment,
Collectors.summingInt(Employee::getSalary)));

Categories