Skip to content

Tag: lambda

Map getOrDefault VS getOrUseSupplier

I am starting to learn lambdas and i do not understand why java Map has: and not(working just the same, but if value is absent, then defaultValue will be taken from supplier): Adventages i currently see of current solution: defaultValue does not have to be a final/effectively final value looks simpler & n…

Merge Map<String, List Java 8 Stream

I would like to merge two Map with JAVA 8 Stream: I try to use this implementation: However, this implementation only create a result like: Map<String, List<Object>> If one key is not contained in the mapGlobal, it would be added as a new key with the corresponding List of String. If the key is du…

Exception handling with Consumer functions in Java 8

This code gives me a compile error on the line processBatch(batch, this::backupMetacard); The process batch method wraps the consumer in a try/catch block, but Java will not compile the call. Answer The problem is that in the following snippet, the method backupMetacard declares to throw the checked IOExcepti…

Merge sets when two elements in common

This is the follow up of compare sets I have I want to merge the sets when there are two elements in common. For example 0,1,2 and 0,2,6 has two elements in common so merging them to form [0,1,2,6]. Again [0,1,2,6] and [2,6,7] has 2 and 6 common. so merging them and getting [0,1,2,6,7]. The final output shoul…

Return interface implementation with Kotlin and lambda

I have this simple interface : This interface is used in one function of a class : My question is : is there a way to simplify the return statement with a lambda ? I try some stuff like this but it doesn’t work : EDIT : Kotlin supports SAM interfaces now so declaring it like so : allows us

Java 8: Lambda with variable arguments

I am looking for a way to invoke multiple argument methods but using a lambda construct. In the documentation it is said that lambda is only usable if it can map to a functional interface. I want to do something like: Is there any way one can do this elegantly without defining 10 interfaces, one for each argu…