As already known it’s easy to add Serialization support to a lambda expression when the target interface does not already inherit Serializable, just like (TargetInterface&Serializable)()->{/*code*/}. What I ask for, is a way to do the opposite, explicitly remove Serialization support when the target in…
Tag: java-8
How to right align a button in Java FX toolbar
I am building a UI using Java FX scene builder and I want a button in a toolbar to float towards the right side of the toolbar. I have tried changing the node orientation of the parent(toolbar) and also the button but both seem to be ignored. Answer Add a pane with no content which always grows to fit availab…
Accumulator Generator test – Java 8
Paul Graham, in his great article Revenge of the Nerds, claimed that languages vary in power. He mentioned a nice exercise – writing an accumulator generator: We want to write a function that generates accumulators– a function that takes a number n, and returns a function that takes another number…
Functional style of Java 8’s Optional.ifPresent and if-not-Present?
In Java 8, I want to do something to an Optional object if it is present, and do another thing if it is not present. This is not a ‘functional style’, though. Optional has an ifPresent() method, but I am unable to chain an orElse() method. Thus, I cannot write: In reply to @assylias, I don’t…
Why is “final” not allowed in Java 8 interface methods?
One of the most useful features of Java 8 are the new default methods on interfaces. There are essentially two reasons (there may be others) why they have been introduced: Providing actual default implementations. Example: Iterator.remove() Allowing for JDK API evolution. Example: Iterable.forEach() From an A…
Iterate an Enumeration in Java 8
Is it possible to iterate an Enumeration by using Lambda Expression? What will be the Lambda representation of the following code snippet: I didn’t find any stream within it. Answer In case you don’t like the fact that Collections.list(Enumeration) copies the entire contents into a (temporary) list befo…
What is difference between Collection.stream().forEach() and Collection.forEach()?
I understand that with .stream(), I can use chain operations like .filter() or use parallel stream. But what is difference between them if I need to execute small operations (for example, printing the elements of the list)? Answer For simple cases such as the one illustrated, they are mostly the same. However…
Java 8’s streams: why parallel stream is slower?
I am playing with Java 8’s streams and cannot understand the performance results I am getting. I have 2 core CPU (Intel i73520M), Windows 8 x64, and 64-bit Java 8 update 5. I am doing simple map over …
Merge lists with stream API
I have the following situation I have to merge all the lists lst from the ListContainer objects from a Map map. Any idea how, using Java 8 stream API? Answer I think flatMap() is what you’re looking for. For example:
Using Streams with primitives data types and corresponding wrappers
While playing around with Java8’s Streams-API, I stumbled over the following: To convert an array of primitive wrapper classe objects into a Stream I just have to call Stream.of(array). But to convert an array of primitive data types, I have to call .of(array) from the corresponding wrapper (class) stre…