How to change the following keys from the map without any variables full functional to I tried with map and was able to change the keys to the same strings but not to different ones. Answer You can apply a map to all the items but focus only on the keys: You can define f to be a function or
Tag: functional-programming
how to fix the second parameter of a function with vavr?
Suppose I have a function which takes two parameter. I want to fix the second parameter and makes it a Function1<T1,R>. With Function2.apply(T1 t), I can only fix the first parameter, is there a way to fix the second parameter? Answer There’s no utility function built into vavr that does a partial application of the second argument. The available utility
How to reduce a limited stream (e.g. an ArrayList) from right in Java?
I would like to convert a list to a self-implemented linked list using reduce. I managed to do that in a reverse order: import java.util.Arrays; import java.util.List; public class Reverse { …
Java 8 convert for loop and summing of values to stream/lambda?
I have some String inputs that I am looping over that I am trying to convert to java 8 stream/lambdas but was having some issues. My boilerplate code looks like this: public static int count(List<…
Java functional programming: create list of object from a different list
I’d like to convert to funcional programming the following method: public static List
returning early from a method if an Optional is empty
Is there a more functional way to return early from a method if an Optional is empty than this? public boolean validate(Optional obj) { if (obj.isPresent(obj) { var object = obj….
Creating a map from nested lists
Suppose there are 3 classes: Suppose there is a List of Level1 objects called initial state I want to create a map from initialList where: I am able to achieve this using for loops, but I want to achieve this in a more elegant way using Java 8 features (streams and the functions). I tried using Collectors.toMap() also tried grouping
How to implement the Elvis operator in Java 8?
I have the classic “Elvis operator” case, where I’m calling methods that each may return null and chaining them together: In Java 8, the most faithful implementation I’ve found is something like this: I wish that Java’s Optional had something akin to the elvis operator: So that I wouldn’t have to wrap each return value: Is there a more efficient
Count the same items in a row in Java 8 Stream API
I have a bean and a stream and want to apply some intermediate operation to the stream that returns another stream of objects of TokenBag. In this example there must be two: (“a”, 3), (“b”, 3) and (“a”, 2). Please think it as a very simplistic example. In real there will be much more complicated logic than just counting the
Java strange class cast exception
Could anyone help me understand why in 1 situation I do not have ClassCastException? At least String::trim is not a MagicFunction. Answer So, method references (like String::trim) are a bit weird; unlike most expressions in Java, they don’t really have a type of their own. Something like this: won’t even compile, because it doesn’t give the compiler enough information about