java-stream

How do I get an IntStream from a List<Integer>?

点点圈 提交于 2019-12-17 18:44:56
问题 I can think of two ways: public static IntStream foo(List<Integer> list) { return list.stream().mapToInt(Integer::valueOf); } public static IntStream bar(List<Integer> list) { return list.stream().mapToInt(x -> x); } What is the idiomatic way? Maybe there is already a library function that does exactly what I want? 回答1: I guess (or at least it is an alternative) this way is more performant: public static IntStream baz(List<Integer> list) { return list.stream().mapToInt(Integer::intValue); }

Is the accumulator of reduce in Java 8 allowed to modify its arguments?

大兔子大兔子 提交于 2019-12-17 18:36:24
问题 In Java 8, Stream has a method reduce: T reduce(T identity, BinaryOperator<T> accumulator); Is the accumulator operator allowed to modify either of its arguments? I presume not since the JavaDoc says the accumulator should be NonInterfering, though all examples talk of modifying the collection, rather than modifying the elements of the collection. So, for a concrete example, if we have integers.reduce(0, Integer::sum); and suppose for a moment that Integer was mutable, would sum be allowed to

Avoid NoSuchElementException with Stream

主宰稳场 提交于 2019-12-17 18:23:30
问题 I have the following Stream : Stream<T> stream = stream(); T result = stream.filter(t -> { double x = getX(t); double y = getY(t); return (x == tx && y == ty); }).findFirst().get(); return result; However, there is not always a result which gives me the following error: NoSuchElementException: No value present So how can I return a null if there is no value present? 回答1: You can use Optional.orElse, it's much simpler than checking isPresent : T result = stream.filter(t -> { double x = getX(t)

Is there any way to reuse a Stream?

拟墨画扇 提交于 2019-12-17 17:47:14
问题 I'm learning the new Java 8 features, and while experimenting with streams ( java.util.stream.Stream ) and collectors, I realized that a stream can't be used twice. Is there any way to reuse it? 回答1: If you want to have the effect of reusing a stream, you might wrap the stream expression in a Supplier and call myStreamSupplier.get() whenever you want a fresh one. For example: Supplier<Stream<String>> sup = () -> someList.stream(); List<String> nonEmptyStrings = sup.get().filter(s -> !s

Java stream toArray() convert to a specific type of array

◇◆丶佛笑我妖孽 提交于 2019-12-17 17:42:22
问题 Maybe this is very simple but I'm actually a noob on Java 8 features and don't know how to accomplish this. I have this simple line that contains the following text: "Key, Name" and I want to convert that line into a String array, separating each value by the comma (,), however, I also want to trim every field before returning the final array, so I did the following: Arrays.stream(line.split(",")).map(String::trim).toArray(); However, this returns an Object[] array rather than a String[]

Alternative for throwingMerger in Java 8

风流意气都作罢 提交于 2019-12-17 16:52:50
问题 I'm implementing own collector that uses merge function . Unfortunately, for some of my cases, I can't reuse the following JDK merger function that thrown IllegalStateException . java.util.stream.Collectors#throwingMerger It happens due to the fact that it has private access modifier and access from other(not inner) classes is restricted. However, javadoc says the following: This can be used to enforce the assumption that the elements being collected are distinct But, as I see, java doc is

Using multiple map functions vs. a block statement in a map in a java stream

本秂侑毒 提交于 2019-12-17 16:33:56
问题 Say I have the following code data.stream() .map(x -> { Object a = maybeReturnsNull(x); return a == null ? defaultValue : a; }) I have some function that might be returning null , and I'm applying it to an element of the stream. I then want to make sure that any null results get changed to some default value instead. Is there any significant difference between using two maps as in the following example, as compared to using the previous example that defines a helper variable a and uses a code

Are Java streams stages sequential?

北战南征 提交于 2019-12-17 16:08:33
问题 I have a question on the intermediate stages sequential state - are the operations from a stage applied to all the input stream (items) or are all the stages / operations applied to each stream item? I'm aware the question might not be easy to understand, so I'll give an example. On the following stream processing: List<String> strings = Arrays.asList("Are Java streams intermediate stages sequential?".split(" ")); strings.stream() .filter(word -> word.length() > 4) .peek(word -> System.out

Partition a Stream by a discriminator function

依然范特西╮ 提交于 2019-12-17 15:45:39
问题 One of the missing features in the Streams API is the "partition by" transformation, for example as defined in Clojure. Say I want to reproduce Hibernate's fetch join : I want to issue a single SQL SELECT statement to receive this kind of objects from the result: class Family { String surname; List<String> members; } I issue: SELECT f.name, m.name FROM Family f JOIN Member m on m.family_id = f.id ORDER BY f.name and I retrieve a flat stream of (f.name, m.name) records. Now I need to transform

Modify file using Files.lines

家住魔仙堡 提交于 2019-12-17 15:43:32
问题 I'd like to read in a file and replace some text with new text. It would be simple using asm and int 21h but I want to use the new java 8 streams. Files.write(outf.toPath(), (Iterable<String>)Files.lines(inf)::iterator, CREATE, WRITE, TRUNCATE_EXISTING); Somewhere in there I'd like a lines.replace("/*replace me*/","new Code()\n"); . The new lines are because I want to test inserting a block of code somewhere. Here's a play example, that doesn't work how I want it to, but compiles. I just need