Java 8 Streams FlatMap method example

It doesn’t make sense to flatMap a Stream that’s already flat, like the Stream<Integer> you’ve shown in your question. However, if you had a Stream<List<Integer>> then it would make sense and you could do this: Stream<List<Integer>> integerListStream = Stream.of( Arrays.asList(1, 2), Arrays.asList(3, 4), Arrays.asList(5) ); Stream<Integer> integerStream = integerListStream .flatMap(Collection::stream); integerStream.forEach(System.out::println); Which would print: 1 … Read more

Why does parallel stream with lambda in static initializer cause a deadlock?

I found a bug report of a very similar case (JDK-8143380) which was closed as “Not an Issue” by Stuart Marks: This is a class initialization deadlock. The test program’s main thread executes the class static initializer, which sets the initialization in-progress flag for the class; this flag remains set until the static initializer completes. … Read more

How do I turn a Java Enumeration into a Stream?

Why not using vanilla Java : Collections.list(enumeration).stream()… However as mentionned by @MicahZoltu, the number of items in the enumeration has to be taken into account, as Collections.list will first iterate over the enumeration to copy the elements in an ArrayList. From there the regular stream method can be used. While this is usual for many … Read more

How to concatenate a string with the new 1.8 stream API [duplicate]

The official documentation for what you want to do: https://docs.oracle.com/javase/8/docs/api/java/util/stream/Collectors.html // Accumulate names into a List List<String> list = people.stream().map(Person::getName).collect(Collectors.toList()); // Convert elements to strings and concatenate them, separated by commas String joined = things.stream() .map(Object::toString) .collect(Collectors.joining(“, “)); For your example, you would need to do this: // Convert elements to strings and concatenate them, … Read more

Modifying Objects within stream in Java8 while iterating

Yes, you can modify state of objects inside your stream, but most often you should avoid modifying state of source of stream. From non-interference section of stream package documentation we can read that: For most data sources, preventing interference means ensuring that the data source is not modified at all during the execution of the … Read more

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

Use toArray(size -> new String[size]) or toArray(String[]::new). String[] strings = Arrays.stream(line.split(“,”)).map(String::trim).toArray(String[]::new); This is actually a lambda expression for .toArray(new IntFunction<String[]>() { @Override public String[] apply(int size) { return new String[size]; } }); Where you are telling convert the array to a String array of same size. From the docs The generator function takes an integer, … Read more

Is it possible to use the Java 8 Stream API on Android API < 24?

[original answer] You can not use Java8 streams on API level < 24. However, there are some libraries that backport some of the stream functionality https://github.com/aNNiMON/Lightweight-Stream-API https://github.com/konmik/solid https://sourceforge.net/projects/streamsupport/ (mentioned by @sartorius in comment) [update k3b 2019-05-23] https://github.com/retrostreams/android-retrostreams is a spinoff from streamsupport which takes advantage of Android Studio 3.x D8 / desugar toolchain’s capability to … Read more

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