java-stream

Intermediate stream operations not evaluated on count

霸气de小男生 提交于 2020-04-29 06:16:34
问题 It seems I'm having trouble understanding how Java composes stream operations into a stream pipeline. When executing the following code public static void main(String[] args) { StringBuilder sb = new StringBuilder(); var count = Stream.of(new String[]{"1", "2", "3", "4"}) .map(sb::append) .count(); System.out.println(count); System.out.println(sb.toString()); } The console only prints 4 . The StringBuilder object still has the value "" . When I add the filter operation: filter(s -> true)

Intermediate stream operations not evaluated on count

依然范特西╮ 提交于 2020-04-29 06:12:09
问题 It seems I'm having trouble understanding how Java composes stream operations into a stream pipeline. When executing the following code public static void main(String[] args) { StringBuilder sb = new StringBuilder(); var count = Stream.of(new String[]{"1", "2", "3", "4"}) .map(sb::append) .count(); System.out.println(count); System.out.println(sb.toString()); } The console only prints 4 . The StringBuilder object still has the value "" . When I add the filter operation: filter(s -> true)

Produce a Map from an IntStream of keys

北战南征 提交于 2020-04-17 22:10:07
问题 I know I can initialize a Map, and then fill it using a stream. For example, here I use an IntStream of random numbers to populate the key of the predefined map. int initialCapacity = 3; Map < Integer, UUID > map = new HashMap <>( initialCapacity ); IntStream numbers = ThreadLocalRandom.current().ints( initialCapacity ); numbers.forEach( number -> map.put( number , UUID.randomUUID() ) ); Dump to console. System.out.println( "map = " + map ); map = {-14054365=ee739423-1200-45e6-80da

Java stream distinct does not work on custom objects? [duplicate]

不打扰是莪最后的温柔 提交于 2020-04-17 20:43:54
问题 This question already has an answer here : Stream and the distinct operation (1 answer) Closed 28 days ago . Javadocs say that distinct() - Returns a stream consisting of the distinct elements (according to Object.equals(Object)) of this stream. I have a list of custom objects with some duplicates. When I run distinct() method on the streamed list, I still get the original list back. Why are the duplicates not getting removed even though I defined an equals method in the custom object ? Code

Java stream distinct does not work on custom objects? [duplicate]

守給你的承諾、 提交于 2020-04-17 20:42:10
问题 This question already has an answer here : Stream and the distinct operation (1 answer) Closed 28 days ago . Javadocs say that distinct() - Returns a stream consisting of the distinct elements (according to Object.equals(Object)) of this stream. I have a list of custom objects with some duplicates. When I run distinct() method on the streamed list, I still get the original list back. Why are the duplicates not getting removed even though I defined an equals method in the custom object ? Code

Remove first element of a Stream in Java 8

こ雲淡風輕ζ 提交于 2020-04-10 06:49:19
问题 I have generated a Stream in Java 8 with Files.walk() method from java.nio library. The problem is that the method includes by default the root path but I do not want this element. I have solved in this case with this code using filter() method: public void listFiles(String directoryPath) { try { Path root = Paths.get(directoryPath); Files.walk(root,1) .filter(x -> !x.equals(root)) .forEach(System.out::println); } catch (IOException ex) { System.err.println("Error reading file: " +

Remove first element of a Stream in Java 8

无人久伴 提交于 2020-04-10 06:49:06
问题 I have generated a Stream in Java 8 with Files.walk() method from java.nio library. The problem is that the method includes by default the root path but I do not want this element. I have solved in this case with this code using filter() method: public void listFiles(String directoryPath) { try { Path root = Paths.get(directoryPath); Files.walk(root,1) .filter(x -> !x.equals(root)) .forEach(System.out::println); } catch (IOException ex) { System.err.println("Error reading file: " +

Is there any performance difference in toArray vs stream.toArray in java

ぃ、小莉子 提交于 2020-04-10 04:49:52
问题 I need to convert a list of ids to array of ids. I can do it in many ways but not sure which one should be used. Say, 1. ids.stream().toArray(Id[]::new) 2. ids.toArray(new Id[ids.length]) Which one is more efficient and why? 回答1: java-11 introduced Collection::toArray that has this implementation: default <T> T[] toArray(IntFunction<T[]> generator) { return toArray(generator.apply(0)); } To make it simpler in your case, it is actually doing : ids.toArray(new Id[0]) ; that is - it is not

Is there a way to coalesce repeated numbers in a list using streams in Java 8?

人盡茶涼 提交于 2020-04-07 19:10:41
问题 e.g.#1 [1, 1, 1, 2, 22, 35, 35, 120, 320] ==>> [3, 2, 22, 70, 120, 320] note how repeated consecutive 1's and 35's are coalesced to 3 and 70 respectively e.g.#2 [1,1,3,1,1] ==>> [2,3,2] 回答1: Stream.of(1, 1, 1, 2, 22, 35, 35, 120, 320) .collect(Collectors.toMap( Function.identity(), Function.identity(), Integer::sum, LinkedHashMap::new )) .values() .forEach(System.out::println); In the case the you posted a comment, you would need a custom collector, actually: static class Custom implements

Is there a way to coalesce repeated numbers in a list using streams in Java 8?

跟風遠走 提交于 2020-04-07 19:09:34
问题 e.g.#1 [1, 1, 1, 2, 22, 35, 35, 120, 320] ==>> [3, 2, 22, 70, 120, 320] note how repeated consecutive 1's and 35's are coalesced to 3 and 70 respectively e.g.#2 [1,1,3,1,1] ==>> [2,3,2] 回答1: Stream.of(1, 1, 1, 2, 22, 35, 35, 120, 320) .collect(Collectors.toMap( Function.identity(), Function.identity(), Integer::sum, LinkedHashMap::new )) .values() .forEach(System.out::println); In the case the you posted a comment, you would need a custom collector, actually: static class Custom implements