java-stream

Iterate two Java-8-Streams together [duplicate]

匆匆过客 提交于 2019-12-17 07:28:43
问题 This question already has answers here : Zipping streams using JDK8 with lambda (java.util.stream.Streams.zip) (14 answers) Closed 5 years ago . I'd like to iterate two Java-8-Streams together, so that I have in each iteration-step two arguments. Something like that, where somefunction produces something like Stream<Pair<A,B>> . Stream<A> as; Stream<B> bs; somefunction (as, bs) .forEach ((a, b) -> foo (a, b)); // or something like somefunction (as, bs) .forEach ((Pair<A, B> abs) -> foo (abs

Why does Stream.allMatch() return true for an empty stream?

落花浮王杯 提交于 2019-12-17 07:26:38
问题 My colleague and I had a bug that was due to our assumption that an empty stream calling allMatch() would return false . if (myItems.allMatch(i -> i.isValid()) { //do something } Of course, it is kind of our fault for assuming and not reading documentation. But what I don't understand is why the default allMatch() behavior for an empty stream returns true . What was the reasoning for this? Like the anyMatch() (which contrarily returns false), this operation is used in an imperative way that

Stream.skip behavior with unordered terminal operation

血红的双手。 提交于 2019-12-17 07:21:33
问题 I've already read this and this questions, but still doubt whether the observed behavior of Stream.skip was intended by JDK authors. Let's have simple input of numbers 1..20: List<Integer> input = IntStream.rangeClosed(1, 20).boxed().collect(Collectors.toList()); Now let's create a parallel stream, combine the unordered() with skip() in different ways and collect the result: System.out.println("skip-skip-unordered-toList: " + input.parallelStream().filter(x -> x > 0) .skip(1) .skip(1)

Use method reference with parameter

烂漫一生 提交于 2019-12-17 06:39:09
问题 I just started learning Java streams and faced a problem. Please take a look at a the following example. This is part of a Node class: private Map<String, Node> nodes; public Optional<Node> child(String name) { return Optional.<Node>ofNullable(nodes.get(name)); } private void findChildren(String name, List<Node> result) { child(name).ifPresent(result::add); nodes.values().stream() // .map(Node::findChildren(name, result)) // .forEach(Node::findChildren(name, result)) .forEach(node -> node

Can you split a stream into two streams?

扶醉桌前 提交于 2019-12-17 06:27:49
问题 I have a data set represented by a Java 8 stream: Stream<T> stream = ...; I can see how to filter it to get a random subset - for example Random r = new Random(); PrimitiveIterator.OfInt coin = r.ints(0, 2).iterator(); Stream<T> heads = stream.filter((x) -> (coin.nextInt() == 0)); I can also see how I could reduce this stream to get, for example, two lists representing two random halves of the data set, and then turn those back into streams. But, is there a direct way to generate two streams

Grouping by object value, counting and then setting group key by maximum object attribute

核能气质少年 提交于 2019-12-17 06:15:30
问题 I have managed to write a solution using Java 8 Streams API that first groups a list of object Route by its value and then counts the number of objects in each group. It returns a mapping Route -> Long. Here is the code: Map<Route, Long> routesCounted = routes.stream() .collect(Collectors.groupingBy(gr -> gr, Collectors.counting())); And the Route class: public class Route implements Comparable<Route> { private long lastUpdated; private Cell startCell; private Cell endCell; private int

Why does findFirst() throw a NullPointerException if the first element it finds is null?

浪子不回头ぞ 提交于 2019-12-17 06:06:47
问题 Why does this throw a java.lang.NullPointerException ? List<String> strings = new ArrayList<>(); strings.add(null); strings.add("test"); String firstString = strings.stream() .findFirst() // Exception thrown here .orElse("StringWhenListIsEmpty"); //.orElse(null); // Changing the `orElse()` to avoid ambiguity The first item in strings is null , which is a perfectly acceptable value. Furthermore, findFirst() returns an Optional, which makes even more sense for findFirst() to be able to handle

How do I keep the iteration order of a List when using Collections.toMap() on a stream?

做~自己de王妃 提交于 2019-12-17 04:58:16
问题 I am creating a Map from a List as follows: List<String> strings = Arrays.asList("a", "bb", "ccc"); Map<String, Integer> map = strings.stream() .collect(Collectors.toMap(Function.identity(), String::length)); I want to keep the same iteration order as was in the List . How can I create a LinkedHashMap using the Collectors.toMap() methods? 回答1: The 2-parameter version of Collectors.toMap() uses a HashMap : public static <T, K, U> Collector<T, ?, Map<K,U>> toMap( Function<? super T, ? extends K

forEach vs forEachOrdered in Java 8 Stream

时间秒杀一切 提交于 2019-12-17 04:46:43
问题 I understand that these methods differ the order of execution but in all my test I cannot achieve different order execution. Example: System.out.println("forEach Demo"); Stream.of("AAA","BBB","CCC").forEach(s->System.out.println("Output:"+s)); System.out.println("forEachOrdered Demo"); Stream.of("AAA","BBB","CCC").forEachOrdered(s->System.out.println("Output:"+s)); Output: forEach Demo Output:AAA Output:BBB Output:CCC forEachOrdered Demo Output:AAA Output:BBB Output:CCC Please provide

Java 8 stream reverse order

空扰寡人 提交于 2019-12-17 02:10:33
问题 General question: What's the proper way to reverse a stream? Assuming that we don't know what type of elements that stream consists of, what's the generic way to reverse any stream? Specific question: IntStream provides range method to generate Integers in specific range IntStream.range(-range, 0) , now that I want to reverse it switching range from 0 to negative won't work, also I can't use Integer::compare List<Integer> list = Arrays.asList(1,2,3,4); list.stream().sorted(Integer::compare)