Issue
How can I put a delay Spring repository method execution to Map? Is it possible to do something like this?
final Map<T, Function<T, R>> maps = new LinkedHashMap<>();
maps.put(product, productRepository::save);
maps.put(client, clientRepository::save);
The productRepository
and the clientRepository
there're Spring repositories.
Solution
You can do it, for instance like that:
public class Sample {
public static void main(String[] args) {
Demo<Integer, String> demo = new Demo<>();
demo.put(1, String::valueOf);
System.out.println(demo.get(1).apply(15).getClass());
}
static class Demo<T, R> {
private final Map<T, Function<T, R>> maps = new ConcurrentHashMap<>();
void put(T key, Function<T, R> mapper) {
maps.put(key, mapper);
}
Function<T, R> get(T key) {
return maps.get(key);
}
}
}
Output: class java.lang.String
Answered By - Dmitrii B
Answer Checked By - Pedro (JavaFixing Volunteer)