java-stream

java8 stream of arrays to 2 dimensional array

夙愿已清 提交于 2020-01-21 07:39:06
问题 I'm new to Java8 and I can't use streams to map one array into another 2 dimensional array. I have one 2-dimensional array which is a pattern: boolean[][] pattern = { {true, true, false}, {true, false, true}, {false, true, true} }; And second array which contains keys. 0 means: take 0-element from pattern 1 means: take 1-element from pattern and so on int[] keys = {2, 1, 0}; From these 2 arrays I'd like to produce another 2-dimensional array. In this case the result will look like this:

List of objects to map in java 8

北城以北 提交于 2020-01-21 07:23:35
问题 I have a class like this ObjectA id type where id is unique but the type can be duplicate so if I have a list of ObjectA like this (id, type) = (1, "A") , (2, "B"), (3, "C"), (4, "A") then I want Map<type, List<id>> where map is < "A",[1,4], "B",[2], "C",[3] > It is working with Java 7 but can't find a way to do it in Java 8. In Java 8 I can create key, value pair but not able to create a list if a type is same. 回答1: yourTypes.stream() .collect(Collectors.groupingBy( ObjectA::getType,

List of objects to map in java 8

谁说胖子不能爱 提交于 2020-01-21 07:23:25
问题 I have a class like this ObjectA id type where id is unique but the type can be duplicate so if I have a list of ObjectA like this (id, type) = (1, "A") , (2, "B"), (3, "C"), (4, "A") then I want Map<type, List<id>> where map is < "A",[1,4], "B",[2], "C",[3] > It is working with Java 7 but can't find a way to do it in Java 8. In Java 8 I can create key, value pair but not able to create a list if a type is same. 回答1: yourTypes.stream() .collect(Collectors.groupingBy( ObjectA::getType,

How to filter only specific elements by Java 8 Predicate?

﹥>﹥吖頭↗ 提交于 2020-01-21 05:18:38
问题 I have collection List<Foo> of Foo elements: class Foo { private TypeEnum type; private int amount; //getters, setters ... } Foo type can be TypeEnum.A and TypeEnum.B . I would like to get only those Foo elements from list which if the element have type == TypeEnum.B then amount is greater than zero ( amount > 0 ). How can I do it by Java 8 Streams filter() method? If I use: List<Foo> l = list.stream() .filter(i -> i.getType().equals(TypeEnum.B) && i.getAmount() > 0) .collect(Collectors.<Foo

Is there any advantage of calling map after mapToInt, where ever required

拈花ヽ惹草 提交于 2020-01-21 04:17:45
问题 I am trying to calculate the sum of squares of values in the list. Below are three variations which all calculates the required value. I want to know which one is the most efficient. I am expecting the third one to be more efficient as auto boxing is only done once. // sum of squares int sum = list.stream().map(x -> x * x).reduce((x, y) -> x + y).get(); System.out.println("sum of squares: " + sum); sum = list.stream().mapToInt(x -> x * x).sum(); System.out.println("sum of squares: " + sum);

Is there any advantage of calling map after mapToInt, where ever required

纵然是瞬间 提交于 2020-01-21 04:17:30
问题 I am trying to calculate the sum of squares of values in the list. Below are three variations which all calculates the required value. I want to know which one is the most efficient. I am expecting the third one to be more efficient as auto boxing is only done once. // sum of squares int sum = list.stream().map(x -> x * x).reduce((x, y) -> x + y).get(); System.out.println("sum of squares: " + sum); sum = list.stream().mapToInt(x -> x * x).sum(); System.out.println("sum of squares: " + sum);

Why is the “Variable used in Lambda expression must be final or effectively final” warning ignored for instance variables [duplicate]

大城市里の小女人 提交于 2020-01-21 03:45:08
问题 This question already has answers here : Variable used in lambda expression should be final or effectively final (6 answers) Lambdas: local variables need final, instance variables don't (10 answers) Closed 7 months ago . I'm studying java 8 streams and the only problem I have is understanding lambdas is why the effectively final warning in lambdas is ignored for instance(and static) variables. I can't seem to find any reference to it online, as most pages will just talk about the definition

Sort map by value using lambdas and streams

时光怂恿深爱的人放手 提交于 2020-01-19 07:13:47
问题 I'm new to Java 8, not sure how to use streams and it's methods to sort. If I have map as below, how to sort this map by value to take only top 10 entries using Java 8. HashMap<String, Integer> map = new HashMap<String, Integer>(); map.put("a", 10); map.put("b", 30); map.put("c", 50); map.put("d", 40); map.put("e", 100); map.put("f", 60); map.put("g", 110); map.put("h", 50); map.put("i", 90); map.put("k", 70); map.put("L", 80); I know before Java 8, we can sort as this link: https:/

Sort map by value using lambdas and streams

南楼画角 提交于 2020-01-19 07:11:39
问题 I'm new to Java 8, not sure how to use streams and it's methods to sort. If I have map as below, how to sort this map by value to take only top 10 entries using Java 8. HashMap<String, Integer> map = new HashMap<String, Integer>(); map.put("a", 10); map.put("b", 30); map.put("c", 50); map.put("d", 40); map.put("e", 100); map.put("f", 60); map.put("g", 110); map.put("h", 50); map.put("i", 90); map.put("k", 70); map.put("L", 80); I know before Java 8, we can sort as this link: https:/

Sort map by value using lambdas and streams

◇◆丶佛笑我妖孽 提交于 2020-01-19 07:11:29
问题 I'm new to Java 8, not sure how to use streams and it's methods to sort. If I have map as below, how to sort this map by value to take only top 10 entries using Java 8. HashMap<String, Integer> map = new HashMap<String, Integer>(); map.put("a", 10); map.put("b", 30); map.put("c", 50); map.put("d", 40); map.put("e", 100); map.put("f", 60); map.put("g", 110); map.put("h", 50); map.put("i", 90); map.put("k", 70); map.put("L", 80); I know before Java 8, we can sort as this link: https:/