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…
Tag: lambda
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…
java8 possibility to apply mathematical operators to functions
Assume, I’ve got two functions. I want to multiply these functions. So I want to get this lambda expression as a result: Is it possible in Java 8? Answer No, you won’t be able to do that with Function. Suppose you have a Function.multiply(other) method, what would be the result of performing multi…
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…
Convert a for loop to concat String into a lambda expression
I have the following for loop which iterates through a list of strings and stores the first character of each word in a StringBuilder. I would like to know how can I transform this to a lambda expression Answer Assuming you call toString() on the StringBuilder afterwards, I think you’re just looking for…
Why does Stream.allMatch() return true for an empty stream?
My colleague and I had a bug that was due to our assumption that an empty stream calling allMatch() would return false. Of course, it is kind of our fault for assuming and not reading documentation. But what I don’t understand is why the default allMatch() behavior for an empty stream returns true. What…
Any way to stream a map like “(k,v)” instead of working with (entry)?
Basically I look for a way to avoid working with and similar to what Map.forEach() does. If only I could get a way to work as map.stream().filter((k,v) -> )… and so on It seems the interface is called BiConsumer. Perhaps with a converter to BiConsumer or a Stream.generate() someway Answer Since this …