java-stream

Unwrap Optional when using Collectors maxBy with groupingBy

我的梦境 提交于 2020-08-25 09:32:52
问题 I have a class with a String and an int field. public class Data { private String name; private int value; private Data(String name, int value) { this.name = name; this.value = value; } public String getName() { return name; } public int getValue() { return value; } } I have a List<Data> and if I want to create a map (grouping) to know the Data object with maximum value for each name , I could do like, Map<String, Optional<Data>> result = list.stream() .collect(Collectors.groupingBy(Data:

Java 8: How to convert List<String> to Map<String,List<String>>?

爷,独闯天下 提交于 2020-08-22 03:35:40
问题 I have a List of String like: List<String> locations = Arrays.asList("US:5423","US:6321","CA:1326","AU:5631"); And I want to convert in Map<String, List<String>> as like: AU = [5631] CA = [1326] US = [5423, 6321] I have tried this code and it works but in this case, I have to create a new class GeoLocation.java . List<String> locations=Arrays.asList("US:5423", "US:6321", "CA:1326", "AU:5631"); Map<String, List<String>> locationMap = locations .stream() .map(s -> new GeoLocation(s.split(":")[0

How to retrieve nested list from object using Stream API?

被刻印的时光 ゝ 提交于 2020-08-20 04:37:18
问题 Do you have any idea how to retrieve all SimpleProperty from TopComplexity object? I need to change that for loop into stream "kind" piece of code. @Data public class TopComplexity { List<SuperComplexProperty> superComplexProperties; } @Data public class SuperComplexProperty { List<SimpleProperty> simpleProperties; ComplexProperty complexProperty; } @Data public class ComplexProperty { List<SimpleProperty> simpleProperties; } public class MainClass { public static void main(String[] args) {