How to Get a List from Some Class Properties with Java 8 Stream

How can I get a List from some class properties with Java 8 Stream?

You can use map :

List<String> names = 
personList.stream()
.map(Person::getName)
.collect(Collectors.toList());

EDIT :

In order to combine the Lists of friend names, you need to use flatMap :

List<String> friendNames = 
personList.stream()
.flatMap(e->e.getFriends().stream())
.collect(Collectors.toList());

Get aggregated list of properties from list of Objects(Java 8)

You may do it using the flatMap operator. Here's how it looks.

List<Section> sections = divisions.stream()
.flatMap(d -> d.getSections().stream())
.collect(Collectors.toList());

How to collect objects to List based on properties of the class and then Map to HashMap in java 8 or 11?

Class Source needs to properly override hashCode and equals methods to be used as a key in Map.

However, another POJO should be implemented to contain the desired output:

@AllArgsConstructor
class Grouped {
@JsonProperty("Source")
Source source;

@JsonProperty("Target")
List<Target> target;
}

Then the list of responses may be converted as follows:

List<Response> responses; // set up the input list

List<Grouped> grouped = responses.stream()
.collect(Collectors.groupingBy(
Response::getSource,
Collectors.mapping(Response::getTarget, Collectors.toList())
)) // Map<Source, List<Target>> is built
.entrySet()
.stream() // Stream<Map.Entry<Source, List<Target>>>
.map(e -> new Grouped(e.getKey(), e.getValue()))
.collect(Collectors.toList());

Java: Obtain class member list, for a list within multilevel nested list

Convert sub-list to a stream and use flatMap to convert stream of streams of elements to stream of elements.

Example:

package x.mvmn.demo;

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

public class Demo {

public static class CustomerSales {
public List<Product> productList;
}

public static class Product {
public List<ProductSubItem> productSubItemList;

public List<ProductSubItem> getProductSubItemList() {
return productSubItemList;
}
}

public static class ProductSubItem {
public String itemName;

public ProductSubItem(String itemName) {
this.itemName = itemName;
}

public String getItemName() {
return itemName;
}
}

public static void main(String args[]) throws Exception {
// Setup mock data
CustomerSales customerSales = new CustomerSales();
Product p1 = new Product();
p1.productSubItemList = Arrays.asList(new ProductSubItem("p1 item one"), new ProductSubItem("p1 item two"));
Product p2 = new Product();
p2.productSubItemList = Arrays.asList(new ProductSubItem("p2 item one"), new ProductSubItem("p2 item two"));
customerSales.productList = Arrays.asList(p1, p2);

// Get list of item names
System.out.println(customerSales.productList.stream().map(Product::getProductSubItemList).flatMap(List::stream)
.map(ProductSubItem::getItemName).collect(Collectors.toList()));
// Alternative syntax
System.out.println(customerSales.productList.stream().flatMap(product -> product.productSubItemList.stream())
.map(subItem -> subItem.itemName).collect(Collectors.toList()));
}
}

Output:

[p1 item one, p1 item two, p2 item one, p2 item two]
[p1 item one, p1 item two, p2 item one, p2 item two]

Collecting lists from an object list using Java 8 Stream API

Use flatMap:

List<Integer> concat = examples.stream()
.flatMap(e -> e.getIds().stream())
.collect(Collectors.toList());


Related Topics



Leave a reply



Submit