Java memoization方法 [英] Java memoization method

查看:140
本文介绍了Java memoization方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了一个有趣的问题,并想知道是否以及如何在Java中完成:
创建一个可以记住任何函数/方法的方法。该方法具有以下参数:方法/函数及其参数。

I came across an interesting problem and was wondering if and how could this be done in Java: Create a method which can memoize any function/method . The method has the following arguments : the method/function and the argument(s) for it.

例如,假设我有这种方法:

For example let's say i have this method :

int addOne(int a) { return a + 1;}

我用相同的参数调用我的memoization方法两次:例如addOne和5,第一个调用应该实际调用addOne方法并返回结果并存储给定的结果论点。我第二次打电话时应该知道之前已经打过电话,只是查看上一个答案。

and i call my memoization method two times with the same arguments : addOne and 5 for example, the first call should actually call the addOne method and return the result and also store that result for that given argument. The second time when i call it should know this has been called before and just look up the previous answer.

我的想法是提供类似<$ c $的东西c> HashMap< Callable,HashMap< List< Objects>,Object>> 您将存储以前的答案并在以后查找它们。我认为这可以通过lambda表达式以某种方式完成但是我我不熟悉他们。我不太清楚如何编写这种方法,并希望得到一些帮助。

My idea would be to have something like a HashMap<Callable,HashMap<List<Objects>,Object>> where you would store the previous answers and look them up later on.I think this can be somehow done with lambda expressions but i'm not that familiar with them.I'm not quite sure how to write this method and would appreciate some help.

这可以通过这种方法完成吗?

Can this be done with this approach?

推荐答案

在Java 8中你可以这样做:

In Java 8 you can do it like that:

Map<Integer, Integer> cache = new ConcurrentHashMap<>();

Integer addOne(Integer x) {
    return cache.computeIfAbsent(x -> x + 1);
}

这是一个很好的教程。它适用于任何方法。

来自教程:

Memoizer类:

The Memoizer class:

public class Memoizer<T, U> {
    private final Map<T, U> cache = new ConcurrentHashMap<>();

    private Memoizer() {}
    private Function<T, U> doMemoize(final Function<T, U> function) {
        return input -> cache.computeIfAbsent(input, function::apply);
    }

    public static <T, U> Function<T, U> memoize(final Function<T, U> function) {
        return new Memoizer<T, U>().doMemoize(function);
    }
}

如何使用课程:

Integer longCalculation(Integer x) {
    try {
        Thread.sleep(1000);
    } catch (InterruptedException ignored) {
    }
    return x * 2;
}
Function<Integer, Integer> f = this::longCalculation;
Function<Integer, Integer> g = Memoizer.memoize(f);

public void automaticMemoizationExample() {
    long startTime = System.currentTimeMillis();
    Integer result1 = g.apply(1);
    long time1 = System.currentTimeMillis() - startTime;
    startTime = System.currentTimeMillis();
    Integer result2 = g.apply(1);
    long time2 = System.currentTimeMillis() - startTime;
    System.out.println(result1);
    System.out.println(result2);
    System.out.println(time1);
    System.out.println(time2);
}

输出:

2
2
1000
0

这篇关于Java memoization方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆