java动态编程(记忆多个关键参数)
我用java做动态编程,遇到了这个问题,函数需要三个参数,其中两个是整数,第三个是整数数组。我知道缓存1或2参数函数的值可以使用1d/2d数组实现。我正在考虑使用HashMap,但我正在尝试寻找一种更好的方法,将这三个索引捆绑在一起作为HashMap的键java动态编程(记忆多个关键参数),java,hashtable,dynamic-programming,Java,Hashtable,Dynamic Programming,我用java做动态编程,遇到了这个问题,函数需要三个参数,其中两个是整数,第三个是整数数组。我知道缓存1或2参数函数的值可以使用1d/2d数组实现。我正在考虑使用HashMap,但我正在尝试寻找一种更好的方法,将这三个索引捆绑在一起作为HashMap的键 public static void main(String[] args) { MultiKeyMap multiKeyMap = new MultiKeyMap(); int[] array1 = new int[10]
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));
}
创建一个包含所有参数的新类,并将其添加到映射中。
确保还实现了
hashCode()
&equals()
然后将该类与结果对象放在映射中
HashMap map = new HashMap<MemoKey, Object>();
Object result = map.get(memoKey);
if (result == null)
map.put(memoKey, calcResult());
HashMap map=newhashmap();
对象结果=map.get(memoKey);
如果(结果==null)
put(memoKey,calcResult());
Createimmutable类接受两个泛型和一个整数数组作为常量类型(如果知道将使用其他数值类型,可以将其更改为):
公共最终类CustomKey{
私人最终价值;
私人最终价值;
私人最终整数[]第三值;
公钥(最终F值,
最终S秒值,
最终整数[]第三个值){
this.firstValue=firstValue;
this.secondValue=secondValue;
this.thirdValue=unboxingArray(thirdValue);
}
公共的getSecondValue(){
返回第二个值;
}
公共F getFirstValue(){
返回第一个值;
}
public int[]getThirdValue(){
返回第三个值;
}
私有静态int[]数组(最终整数[]数组){
最终int[]结果=新int[array.length];
IntStream.range(0,array.length)
.forEach(索引->结果[索引]=数组[索引]);
返回结果;
}
@凌驾
公共布尔等于(最终对象o){
如果(this==o)返回true;
如果(o==null | | getClass()!=o.getClass())返回false;
最终定制密钥定制密钥=(定制密钥)o;
返回getFirstValue().equals(customKey.getFirstValue())
&&getSecondValue().equals(customKey.getSecondValue())
&&等于(getThirdValue(),customKey.getThirdValue());
}
@凌驾
公共int hashCode(){
int result=getFirstValue().hashCode();
结果=31*result+getSecondValue().hashCode();
result=31*result+Arrays.hashCode(getThirdValue());
返回结果;
}
}
如果希望能够比较数组中的整数,则必须取消整型数组的装箱。如果要实现其他类型,请在取消装箱之前执行instanceOf check
然后,像使用任何其他键一样加载地图:
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})));
}
publicstaticvoidmain(字符串[]args){
//假设您的值是一个并不重要的字符串
地图自定义键地图=
新的HashMap();
put(新的CustomKey(1,2,新的整数[]{1,2,3}),“第一个值”);
put(新的CustomKey(1,3,新的整数[]{1,2,3}),“第二个值”);
//由于实现了equals,因此无法使用匿名实例
//期望第二个值
System.out.println(customKeyMap.get(新的CustomKey(1,3,新的整数[]{1,2,3}));
}
创建一个键
类,该类可以包含这3个值?是的,这就是它的美妙之处。您可以在代码中使用任意键,multiKeyMap.get(81,22,array1)
返回nullarray1
和array2
具有相同的值,但散列码值不同。当然,它返回null;这两个数组是两个不同的对象。
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;
}
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})));
}