HashTable containsValue无法正常工作

时间:2016-02-23 14:48:21

标签: java hashtable

我有一个巨大的浮点数组,我必须从中删除重复数据。我尝试创建一个HashTable来填充唯一值并将其传递回另一个数组并返回它。问题出在containsValue方法中,它始终返回false,因此所有点都添加在HashTable中。

private float[] removeDuplicates1(float[] input){
    Hashtable<Integer, float[]> h= new Hashtable<>();
    for(int i=0; i<input.length/3; ++i) {
        float[] pt= new float[]{input[i * 3], input[i * 3 + 1], input[i * 3 + 2]};
        Log.i(TAG, Float.toString(pt[0]) + " " +Float.toString(pt[1]) + " " +Float.toString(pt[2]));  //ok
        Log.i(TAG, Boolean.toString(h.containsValue(pt)));      //always false !?
        if(!(h.containsValue(pt))){
            h.put(i,pt);
            Log.i(TAG, "added");
        }
        else Log.i(TAG, "NOT added");
    }
    float[] whitelist = new float[h.size()*3];
    int a=0;
    for(int j=0; j<h.size(); j++){
        float[] f= h.get(j);
        whitelist[a]= f[0];
        whitelist[a+1]= f[1];
        whitelist[a+2]= f[2];
        a=a+3;
    }
    return whitelist;
}

我非常感谢你的帮助。

2 个答案:

答案 0 :(得分:2)

h.containsValue(pt)在查找匹配项时比较数组的地址,而不是它们的内容。

要实现您想要的目标,您可以编写一个包装类,用作地图中的值,并覆盖equalshashcode

答案 1 :(得分:0)

解决

private float[] removeDuplicates1(float[] input){
    Hashtable<Integer, Point> h= new Hashtable<>();
    int hn=0;
    for(int i=0; i<input.length/3; ++i) {
        Point pt= new Point(input[i * 3], input[i * 3 + 1], input[i * 3 + 2]);
        Log.i(TAG, Float.toString(pt.x) + " " +Float.toString(pt.y) + " " +Float.toString(pt.z));
        Log.i(TAG, Boolean.toString(h.containsValue(pt)));
        if(!(h.containsValue(pt))){
            h.put(hn,pt);
            hn++;
            Log.i(TAG, "added");
        }
        else Log.i(TAG, "NOT added");
    }
    float[] whitelist = new float[h.size()*3];
    int a=0;
    for(int j=0; j<h.size(); ++j){
        Point p = new Point(h.get(j));
        whitelist[a] = p.x;
        whitelist[a + 1] = p.y;
        whitelist[a + 2] = p.z;
        a = a + 3;
    }
    return whitelist;
}

包装

public class Point {
    public float x;
    public float y;
    public float z;

    public Point(float x, float y, float z){
        this.x=x;
        this.y=y;
        this.z=z;
    }

    public Point(Point p){
        this.x=p.x;
        this.y=p.y;
        this.z=p.z;
    }

    @Override
    public boolean equals(Object o){
        final Point p = (Point) o;
        if((p.x==this.x) && (p.y==this.y) && (p.z==this.z)) return true;
        else return false;
    }

    @Override
    public int hashCode() {
        int hash = 3;
        hash = 53 * hash + Float.floatToIntBits(this.x);
        hash = 53 * hash + Float.floatToIntBits(this.y);
        hash = 53 * hash + Float.floatToIntBits(this.z);
        return hash;
    }
}