如何使用MATLAB数组作为键HashMap的Java对象? [英] How can I use MATLAB arrays as keys to the HashMap java objects?

查看:205
本文介绍了如何使用MATLAB数组作为键HashMap的Java对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

认沽功能工作正常,但get函数没有。显然,我不知道的伎俩。

The put function works fine but the get function does not. Apparently I don't know the trick.

>> X = [ 1, 2, 3];
>> M = java.util.HashMap;
>> M.put(X,1);
>> M.get([1,2,3])

ans = []

我已经搜查,看了很多帖子,但无法找到一个解决这个问题。
这将是巨大的,如果有人可以让我知道的伎俩。

I have searched and read many posts but could not find a solution to this problem. It would be great if someone can let me know the trick.

推荐答案

我认为这个问题是Java原始数组不为您提供正确的equals()和哈希code()。他们使用由对象标识,而不是包含的值进行比较的标准对象的方法。当一个HashMap中使用非标量数组作为键,MATLAB将它们转换为加倍[],但他们会是不同的Java objecs,因此他们会得到这种行为。

I think the problem is that Java primitive arrays don't provide the right equals() and hashCode() for you. They use the standard Object methods that compare by object identity instead of contained values. When using nonscalar arrays as keys in a HashMap, Matlab will convert them to double[], but they'll be distinct Java objecs, so they'll get this behavior.

如果你在一个Java对象包装您的数组值​​的规定等于按值行为()和hash code(使用它们作为键之前),这可能是工作。幸运的是,java.util.Arrays中提供了价值实现的基本数组。我们只需要一巴掌他们在一个包装类,提供的HashMap期待的接口。

If you wrapped your array values in a Java object that provided by-value behavior for equals() and hashCode() before using them as keys, this could work. Luckily, java.util.Arrays provides by-value implementations for the primitive arrays. We just need to slap them in a wrapper class that provides the interface that HashMap is expecting.

package test;
import java.util.Arrays;

/**
 * A double[] that with by-value semantics for equals() and hashCode() so you
 * can use it in HashMaps.
 * In a non-toy class, you'd probably use switch statements to support arrays
 * of any primitive type. In a language with real generics, you'd just template
 * this.
 */
public class EqualByValueDoubleArray {
    private double[] x;
    public EqualByValueDoubleArray(double[] x) { this.x = x; }
    public double[] getArray() { return x; };
    public boolean equals(Object obj) {
        if (obj instanceof EqualByValueDoubleArray) {
            return Arrays.equals(this.x, ((EqualByValueDoubleArray)obj).x);
        } else {
            return false;
        }
    }
    public int hashCode() { return Arrays.hashCode(x); }
}

现在你可以用和使用它们作为键从MATLAB。

Now you can wrap them and use them as keys from Matlab.

function scratch_array_keyed_hashmap
import test.EqualByValueDoubleArray;
map = java.util.HashMap;
a = [1 2 3 4 5]';

key = EqualByValueDoubleArray(a);
map.put(key, 'my value');
% Separate key so we know it's comparing by value, not Java object identity
key2 = EqualByValueDoubleArray(a);
gotBack = map.get(key2)

这工作R2008b下我。

This works under R2008b for me.

>> scratch_array_keyed_hashmap
gotBack =
my value

有关方便使用,您可以创建检查其输入键的类型,并自动包裹基本数组在这个由价值包装一个HashMap子类。

For easier use, you could create a HashMap subclass that checked the type of its input keys, and automatically wrapped primitive arrays in this by-value wrapper.

这篇关于如何使用MATLAB数组作为键HashMap的Java对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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