How can I create a stream from an array?

You can use Arrays.stream E.g. Arrays.stream(array); You can also use Stream.of as mentioned by @fge , which looks like public static<T> Stream<T> of(T… values) { return Arrays.stream(values); } But note Stream.of(intArray) will return Stream<int[]> whereas Arrays.stream(intArr) will return IntStream providing you pass an array of type int[]. So in a nutshell for primitives type you … Read more

Sorting a list with stream.sorted() in Java

This is not like Collections.sort() where the parameter reference gets sorted. In this case you just get a sorted stream that you need to collect and assign to another variable eventually: List result = list.stream().sorted((o1, o2)->o1.getItem().getValue(). compareTo(o2.getItem().getValue())). collect(Collectors.toList()); You’ve just missed to assign the result

Get last element of Stream/List in a one-liner

It is possible to get the last element with the method Stream::reduce. The following listing contains a minimal example for the general case: Stream<T> stream = …; // sequential or parallel stream Optional<T> last = stream.reduce((first, second) -> second); This implementations works for all ordered streams (including streams created from Lists). For unordered streams it … Read more

Difference between Java 8 streams and RxJava observables

Short answer All sequence/stream processing libs are offering very similar API for pipeline building. The differences are in API for handling multi-threading and composition of pipelines. Long answer RxJava is quite different from Stream. Of all JDK things, the closest to rx.Observable is perhaps java.util.stream.Collector Stream + CompletableFuture combo (which comes at a cost of … Read more

Copy a stream to avoid “stream has already been operated upon or closed”

I think your assumption about efficiency is kind of backwards. You get this huge efficiency payback if you’re only going to use the data once, because you don’t have to store it, and streams give you powerful “loop fusion” optimizations that let you flow the whole data efficiently through the pipeline. If you want to … Read more

Java 8 Streams – collect vs reduce

reduce is a “fold” operation, it applies a binary operator to each element in the stream where the first argument to the operator is the return value of the previous application and the second argument is the current stream element. collect is an aggregation operation where a “collection” is created and each element is “added” … Read more

Java 8: performance of Streams vs Collections

Stop using LinkedList for anything but heavy removing from the middle of the list using iterator. Stop writing benchmarking code by hand, use JMH. Proper benchmarks: @OutputTimeUnit(TimeUnit.NANOSECONDS) @BenchmarkMode(Mode.AverageTime) @OperationsPerInvocation(StreamVsVanilla.N) public class StreamVsVanilla { public static final int N = 10000; static List<Integer> sourceList = new ArrayList<>(); static { for (int i = 0; i < … Read more

Zipping streams using JDK8 with lambda (java.util.stream.Streams.zip)

I needed this as well so I just took the source code from b93 and put it in a “util” class. I had to modify it slightly to work with the current API. For reference here’s the working code (take it at your own risk…): public static<A, B, C> Stream<C> zip(Stream<? extends A> a, Stream<? … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)