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

When is a Java Class loaded?

There is no simple answer to this question. The specification leaves room for different implementation strategies and even within one implementation, it will depend on how the class is been used. Further, your question is more about “when can it fail” which depends on why it may fail, e.g. a class may be loaded at … Read more

Difference between method reference Bound Receiver and Unbound Receiver

The idea of the unbound receiver such as String::length is you’re referring to a method of an object that will be supplied as one of the lambda’s parameters. For example, the lambda expression (String s) -> s.toUpperCase() can be rewritten as String::toUpperCase. But bounded refers to a situation when you’re calling a method in a … Read more

javafx 8 compatibility issues – FXML static fields or methods

It sounds like you are trying to inject a TextField into a static field. Something like @FXML private static TextField myTextField ; This apparently worked in JavaFX 2.2. It doesn’t work in JavaFX 8. Since no official documentation ever supported this use, it’s doesn’t really violate backward compatibility, though in fairness the documentation on exactly … Read more

Java 8 stream emitting a stream

In Java 9, you could use static final Pattern LINE_WITH_CONTINUATION = Pattern.compile(“(\\V|\\R\\+)+”); … try(Scanner s = new Scanner(file)) { s.findAll(LINE_WITH_CONTINUATION) .map(m -> m.group().replaceAll(“\\R\\+”, “”)) .forEach(System.out::println); } Since Java 8 lacks the Scanner.findAll(Pattern) method, you may add a custom implementation of the operation as a work-around public static Stream<MatchResult> findAll(Scanner s, Pattern pattern) { return StreamSupport.stream(new Spliterators.AbstractSpliterator<MatchResult>( 1000, … Read more

Consumer mapped Class in HashMap

This is essentially just like the type-safe heterogeneous container described by Joshua Bloch, except you can’t use the Class to cast the result. Weirdly, I can’t find a great example existing on SO, so here is one: package mcve; import java.util.*; import java.util.function.*; class ClassToConsumerMap { private final Map<Class<?>, Consumer<?>> map = new HashMap<>(); @SuppressWarnings(“unchecked”) … Read more

non-interference requirement on Java 8 streams

Well the oracle example is self-explanatory here. First one is this: List<String> l = new ArrayList<>(Arrays.asList(“one”, “two”)); Stream<String> sl = l.stream(); l.add(“three”); String s = l.collect(Collectors.joining(” “)); If you change l by adding one more elements to it before you call the terminal operation (Collectors.joining) you are fine; but notice that the Stream consists of … Read more

Spring 3.2.x with Java 8

There is a best effort support of JDK8 in the 3.2.x line, as of 3.2.9+. See SPR-11656 for initial support in 3.2.9 and SPR-11979 for bytecode support improvements in 3.2.10. Please note the support limitations explained in the comments. For comprehensive support of JDK8, please upgrade to Spring 4.x – there’s a dedicated wiki page … Read more