java中的动态编程(记忆多个关键参数)

时间:2016-01-10 06:36:20

标签: java hashtable dynamic-programming

我在java中进行动态编程,我遇到了这个问题,其中函数有三个参数,其中两个是整数,第三个是整数数组。我知道可以使用1d / 2d数组实现缓存1或2参数函数的值。我想可能正在使用HashMap,但我正在寻找一种更好的方法将这三个索引捆绑在一起作为hashmap的关键

3 个答案:

答案 0 :(得分:1)

以下是使用Apache Commons Collections的示例:

public static void main(String[] args) {

    MultiKeyMap multiKeyMap = new MultiKeyMap();

    int[] array1 = new int[10];
    int[] array2 = new int[10];

    multiKeyMap.put(13,52,array1, "First");
    multiKeyMap.put(81,22,array2, "Second");

    System.out.println(multiKeyMap.get(81,22,array2));  
}

答案 1 :(得分:1)

创建一个包含所有参数的新类并将其添加到地图中 确保同时实施hashCode()& equals()

class MemoKey {
    Integer a;
    Integer b;
    Integer[] array;

    public MemoKey(Integer a,Integer b, Integer[] array) {
        this.a = a;
        this.b = b;
        this.array = array;
    }

    @Override
    public int hashCode() { 
        // implement 
    }

    @Override
    public boolean equals(Object obj) {
        // implement
    }
}

然后将此类放在带有结果对象的地图中。

HashMap map = new HashMap<MemoKey, Object>();

Object result = map.get(memoKey);
if (result == null)
    map.put(memoKey, calcResult());

答案 2 :(得分:0)

创建不可变类,接受两个泛型和一个整数数组作为常量类型(如果您知道将使用其他数字类型,则可以将其更改为:)

public final class CustomKey<F,S> {
private final F firstValue;
private final S secondValue;
private final int[] thirdValue;

public CustomKey(final F firstValue,
                 final S secondValue,
                 final Integer[] thirdValue){
    this.firstValue = firstValue;
    this.secondValue = secondValue;
    this.thirdValue = unboxingArray(thirdValue);
}

public S getSecondValue() {
    return secondValue;
}


public F getFirstValue() {
    return firstValue;
}

public int[] getThirdValue() {
    return thirdValue;
}

private static int[] unboxingArray(final Integer[] array){
    final int[] result = new int[array.length];
    IntStream.range(0,array.length)
            .forEach(index -> result[index] =  array[index]);
    return result;
}

@Override
public boolean equals(final Object o) {
    if (this == o) return true;
    if (o == null || getClass() != o.getClass()) return false;

    final CustomKey<?, ?> customKey = (CustomKey<?, ?>) o;
    return getFirstValue().equals(customKey.getFirstValue()) 
            && getSecondValue().equals(customKey.getSecondValue()) 
            && Arrays.equals(getThirdValue(), customKey.getThirdValue());

}

@Override
public int hashCode() {
    int result = getFirstValue().hashCode();
    result = 31 * result + getSecondValue().hashCode();
    result = 31 * result + Arrays.hashCode(getThirdValue());
    return result;
}

}

如果您希望能够比较数组中的整数,则必须使用整数数组。如果要实现其他类型,请在取消装箱之前进行instanceOf检查。

然后,像使用任何其他键一样加载地图:

public static void main(String[]args){
    //Assume your Value is a String which does not really matter
    Map<CustomKey<Integer,Integer>,String> customKeyMap =
            new HashMap<>();
    customKeyMap.put(new CustomKey<>(1,2,new Integer[]{1,2,3}),"First Value");
    customKeyMap.put(new CustomKey<>(1,3,new Integer[]{1,2,3}),"Second Value");

    //Can use anonymous instance since equals is implemented
    //Expect Second Value
    System.out.println(customKeyMap.get(new CustomKey<>(1,3,new Integer[]{1,2,3})));
}