java-stream

Collect complex objects in one lambda expression

这一生的挚爱 提交于 2019-12-01 09:28:51
I have a list of objects. At first, I need to sort it by type. Than by faceValue. In the end, summarize all quantities: class Coin{ String type; BigInteger faceValue; BigInteger quantity; ... } List<Coin> coins = new ArrayList<>(); coins.add(new Coin("USD", 1, 150)); coins.add(new Coin("USD", 1, 6)); coins.add(new Coin("USD", 1, 60)); coins.add(new Coin("USD", 2, 100)); coins.add(new Coin("USD", 2, 100)); coins.add(new Coin("CAD", 1, 111)); coins.add(new Coin("CAD", 1, 222)); Result list must contains only 3 new coin objects: Coin("USD", 1 , 216) Coin("USD", 2 , 200) Coin("CAD", 1 , 333) How

How to sort hash map based on number of keys for a value using flatmap java8?

依然范特西╮ 提交于 2019-12-01 09:25:18
This is a follow-up of How to get the count of keys for values in a hash map using lambda . I have a HashMap and I want to find the number of keys for each value Map<Integer, List<Integer>> map = new HashMap<Integer, List<Integer>>() {{ put(0, Arrays.asList(1, 2)); put(1, Arrays.asList(2, 0, 3)); put(2, Arrays.asList(4,0,1)); put(3, Arrays.asList(4,1, 5)); put(4, Arrays.asList(5,2,3)); put(5, Arrays.asList(4,3)); }}; According to the above post, I tried flat mapping: Map<Object, Long> ex = map.values() .stream() .flatMap(Collection::stream) .collect(Collectors.groupingBy(v -> v, Collectors

How to process chuncks of a file with java.util.stream

末鹿安然 提交于 2019-12-01 09:11:06
问题 To get familliar with the stream api, I tried to code a quite simple pattern. Problem: Having a text file containing not nested blocks of text. All blocks are identified by start/endpatterns (e.g. <start> and <stop> . The content of a block isn't syntactically distinguishable from the noise between the blocks. Therefore it is impossible, to work with simple (stateless) lambdas. I was just able to implement something ugly like: Files.lines(path).collect(new

Java Stream Collectors.toList() wont compile

早过忘川 提交于 2019-12-01 09:05:35
Can any one explain why the below code will not compile but the second one does? Do not compile private void doNotCompile() { List<Integer> out; out = IntStream .range(1, 10) .filter(e -> e % 2 == 0) .map(e -> Integer.valueOf(2 * e)) .collect(Collectors.toList()); System.out.println(out); } Compilation errors on the collect line The method collect(Supplier, ObjIntConsumer, BiConsumer) in the type IntStream is not applicable for the arguments (Collector>) Type mismatch: cannot convert from Collector> to Supplier Compiles private void compiles() { List<Integer> in; in = Arrays.asList(1, 2, 3, 4,

java8 stream of arrays to 2 dimensional array

孤人 提交于 2019-12-01 09:02:34
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: boolean[][] result = { {false, true, true}, {true, false, true}, {true, true, false} }; This is the code in

Understanding sequential vs parallel stream spliterators in Java 8 and Java 9

自古美人都是妖i 提交于 2019-12-01 09:01:14
A question about spliterators that at first glance is not straightforward. In streams, .parallel() changes the behaviour that the stream is processed. However I was expecting the spliterators created from sequential and parallel streams to be the same. For example, in sequential streams typically, the .trySplit() is never invoked , while in parallel streams it is, in order to hand over the split spliterator to another thread. Differences between stream.spliterator() vs stream.parallel().spliterator() : They may have different characteristics: Stream.of(1L, 2L, 3L).limit(2); // ORDERED Stream

Why my collector method is not processing data parallely?

梦想的初衷 提交于 2019-12-01 08:40:53
Suppose, however, that the result container used in this reduction was a concurrently modifiable collection -- such as a ConcurrentHashMap. In that case, the parallel invocations of the accumulator could actually deposit their results concurrently into the same shared result container, eliminating the need for the combiner to merge distinct result containers. This potentially provides a boost to the parallel execution performance. We call this a concurrent reduction. also A Collector that supports concurrent reduction is marked with the Collector.Characteristics.CONCURRENT characteristic.

Getting the max of a property using Java 8

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-01 08:40:41
问题 I want to rewrite the below code using the Stream library ( allPeople is a List<Person> ). int maxYear = Integer.MIN_VALUE; Person oldest = null; for (Person p : allPeople) { if (p.getDateOfDeath() > maxYear) { oldest = p; maxYear = p.getDateOfDeath(); } } I am trying to find the oldest person in a list of people (assuming there is no Age property on the Person object, it's just an example). How can I rewrite this using Java 8? 回答1: Person oldest = allPeople.stream().max(comparingInt(Person:

Compact stream expression for transposing double[][] Matrix

天涯浪子 提交于 2019-12-01 08:35:39
问题 I want to tranpose a double[][] matrix with the most compact and efficient expression possible. Right now I have this: public static Function<double[][], double[][]> transpose() { return (m) -> { final int rows = m.length; final int columns = m[0].length; double[][] transpose = new double[columns][rows]; range(0, rows).forEach(r -> { range(0, columns).forEach(c -> { transpose[c][r] = m[r][c]; }); }); return transpose; }; } Thoughts? 回答1: You could have: public static UnaryOperator<double[][]>

Java: making List from primitive array using Stream API

丶灬走出姿态 提交于 2019-12-01 08:34:35
I'm trying to make a List from a primitive array int[] values={4,5,2,3,42,60,20}; List<Integer> greaterThan4 = Arrays.stream(values) .filter(value -> value > 4) .collect(Collectors.toList()); But the last function collect gives me an error because it wants other arguments. It wants 3 arguments Supplier, ObjIntConsumer and BiConsumer. I don't understand why it wants 3 arguments when I have seen different examples that just use collect(Collectors.toList()); and get the list. What I'm doing wrong? Yes this is because Arrays.stream returns an IntStream . You can call boxed() to get a Stream