java-stream

stream on JPA lazy list

痴心易碎 提交于 2019-12-17 09:53:13
问题 I have JPA entity with list like this: @OneToMany(mappedBy = "scadaElement", orphanRemoval = true) private List<ElementParameter> elementParameters; and map form ElementParameter @ManyToOne @JoinColumn(name = "SCADAELEMENT_ID") ScadaElement scadaElement; when i get entity with elementParameters list and do stream on it stream do nothing, even when I trigger list with .size() but when I do the same with a for loop it work. System.out.println("elements size: " + s.getElementParameters().size())

Non-interference exact meaning in Java 8 streams

允我心安 提交于 2019-12-17 09:48:17
问题 Does the non-interference requirement for using streams of non-concurrent data structure sources mean that we can't change the state of an element of the data structure during the execution of a stream pipeline (in addition to that we can't change the source data structure itself)? (Question 1) In the section about non-interference, in the stream package description, its said: "For most data sources, preventing interference means ensuring that the data source is not modified at all during the

Most efficient way to get the last element of a stream

丶灬走出姿态 提交于 2019-12-17 08:34:08
问题 Stream doesn't have a last() method: Stream<T> stream; T last = stream.last(); // No such method What's the most elegant and/or efficient way to get the last element (or null for an empty Stream)? 回答1: Do a reduction that simply returns the current value: Stream<T> stream; T last = stream.reduce((a, b) -> b).orElse(null); 回答2: This heavily depends on the nature of the Stream . Keep in mind that “simple” doesn’t necessarily mean “efficient”. If you suspect the stream to be very large, carrying

Understanding deeply spliterator characteristics

落爺英雄遲暮 提交于 2019-12-17 08:32:26
问题 In order to try to deeply understand java streams and spliterators, I have some subtle questions about spliterator characteristics : Q1: Stream.empty() vs Stream.of() (Stream.of() without args) Stream.empty() : SUBSIZED, SIZED Stream.of() : SUBSIZED, IMMUTABLE , SIZED, ORDERED Why Stream.empty() doesn't have the same characteristics of Stream.of() ? Note that it has impacts when using in conjunction with Stream.concat() (specially not having ORDERED ). I would say that Stream.empty() should

Understanding deeply spliterator characteristics

依然范特西╮ 提交于 2019-12-17 08:31:50
问题 In order to try to deeply understand java streams and spliterators, I have some subtle questions about spliterator characteristics : Q1: Stream.empty() vs Stream.of() (Stream.of() without args) Stream.empty() : SUBSIZED, SIZED Stream.of() : SUBSIZED, IMMUTABLE , SIZED, ORDERED Why Stream.empty() doesn't have the same characteristics of Stream.of() ? Note that it has impacts when using in conjunction with Stream.concat() (specially not having ORDERED ). I would say that Stream.empty() should

Java 8 method references: provide a Supplier capable of supplying a parameterized result

你说的曾经没有我的故事 提交于 2019-12-17 08:11:47
问题 I'd like to use java.util.Optional.orElseThrow() with an Exception type that asks for a constructor parameter. Something like this: orElseThrow(MyException::new(someArgument)) // obviously NOT working Is there a way to create a Supplier that passes my argument value in? 回答1: Sure. orElseThrow(() -> new MyException(someArgument)) . 回答2: It appears that you can throw only RuntimeException from the method orElseThrow . Otherwise you will get an error message like MyException cannot be converted

Java 8 Stream IllegalStateException: Stream has already been operated on or closed

可紊 提交于 2019-12-17 07:43:19
问题 I'm trying to generate Order instances using the Stream API. I have a factory function that creates the order, and a DoubleStream is used to initialize the amount of the order. private DoubleStream doubleStream = new Random().doubles(50.0, 200.0); private Order createOrder() { return new Order(doubleStream.findFirst().getAsDouble()); } @Test public void test() { Stream<Order> orderStream = Stream.generate(() -> { return createOrder(); }); orderStream.limit(10).forEach(System.out::println); If

Java 8 Stream IllegalStateException: Stream has already been operated on or closed

╄→гoц情女王★ 提交于 2019-12-17 07:43:01
问题 I'm trying to generate Order instances using the Stream API. I have a factory function that creates the order, and a DoubleStream is used to initialize the amount of the order. private DoubleStream doubleStream = new Random().doubles(50.0, 200.0); private Order createOrder() { return new Order(doubleStream.findFirst().getAsDouble()); } @Test public void test() { Stream<Order> orderStream = Stream.generate(() -> { return createOrder(); }); orderStream.limit(10).forEach(System.out::println); If

Cartesian product of streams in Java 8 as stream (using streams only)

和自甴很熟 提交于 2019-12-17 07:37:53
问题 I would like to create a method which creates a stream of elements which are cartesian products of multiple given streams (aggregated to the same type at the end by a binary operator). Please note that both arguments and results are streams, not collections. For example, for two streams of {A, B} and {X, Y} I would like it produce stream of values {AX, AY, BX, BY} (simple concatenation is used for aggregating the strings). So far, I have came up with this code: private static <T> Stream<T>

Iterate two Java-8-Streams together [duplicate]

▼魔方 西西 提交于 2019-12-17 07:29:48
问题 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