Java Hashtable overwrites existing key with new key during 'put'

I am trying to add entries to a hash table in Java using Eclipse. During the put operation, only one of the keys is overwritten with the new key and value. The hash table count is maintained properly, but one of the pair (key, value) is lost.

Here is my sample code:

ArrayList<Double> list;
Hashtable<Val,ArrayList<Double>> numbers = new Hashtable<Val,ArrayList<Double>>();

while((line = brMyHashval.readLine()) != null)
{
    if(!(line.isEmpty()))
    {               
        String[] temp;
        temp = line.split(" ");      
        eDouble = Double.parseDouble(temp[2].toString());

        Val key = new Val(Double.parseDouble(temp[0].toString()) ,Double.parseDouble(temp[1].toString()) );

        if(!(numbers.containsKey(key)))
        {
            list = new ArrayList<Double>();
            numbers.put(key, list);

        }
        else
        {
            list = numbers.get(key);
        }
        list.add(eDouble); 
     }
}

I used the built-in hashcode and equals method in eclipse to compare class objects.

Input text file:

1.0 2.0 9.0
3.0 4.0 9.0
5.0 6.0 9.0
1.0 2.0 8.0
5.0 6.0 8.0
1.0 2.0 7.0
**7.0 8.0 7.0** // After this point a new hash entry gets added for key(7,8), But key (1,2) get deleted from the hashtable, though count gets increased to 4.
3.0 4.0 7.0
5.0 6.0 10.0
1.0 2.0 10.0
1.0 3.0 10.0
1.0 4.0 10.0

Why is the key deleted at this particular moment.?

[edit] hashcode and equals: I used eclipse to automatically import these methods // (x, y) is (a, b)

  class Val

{
    double x;
    double y;

Val(double X, double Y)
{
    x = X;
    y = Y;
}

@Override
public int hashCode() {
    final int prime = 31;
    int result = 1;
    long temp;
    temp = Double.doubleToLongBits(x);
    result = prime * result + (int) (temp ^ (temp >>> 32));
    temp = Double.doubleToLongBits(y);
    result = prime * result + (int) (temp ^ (temp >>> 32));
    return result;
}

@Override
public boolean equals(Object obj) {
    if (this == obj)
        return true;
    if (obj == null)
        return false;
    if (getClass() != obj.getClass())
        return false;
    Val other = (Val) obj;
    if (Double.doubleToLongBits(x) != Double.doubleToLongBits(other.x))
        return false;
    if (Double.doubleToLongBits(y) != Double.doubleToLongBits(other.y))
        return false;
    return true;
}

}

+5
source share
4 answers

, HashMap.

, (1,2) (7,8) HashTable, . (7,8), (1,2) "" (7,8) - next (7,8).

enter image description here

, , HashMap:

    for (Val key : numbers.keySet()) {
        System.out.printf("%.1f %.1f: %s%n", key.x, key.y, numbers.get(key));
    }
+6

, , equals() hashCode(), . (:

public boolean equals(K other) {
    return a == other.a && b == other.b;
}

public int hashCode() {
    return new Double(a).hashCode() ^ new Double(b).hashCode();
}

, :

  • K retuyrn ,
  • K -, .

.

+2

, .

, equals , . , . . .

+1

, , ( , ).

public class HashProblem {

    public static class Val {
        private double x;
        private double y;

        public Val(double x, double y) {
            this.x = x;
            this.y = y;
        }

        @Override
        public int hashCode() {
            final int prime = 31;
            int result = 1;
            long temp;
            temp = Double.doubleToLongBits(x);
            result = prime * result + (int) (temp ^ (temp >>> 32));
            temp = Double.doubleToLongBits(y);
            result = prime * result + (int) (temp ^ (temp >>> 32));
            return result;
        }

        @Override
        public boolean equals(Object obj) {
            if (this == obj)
                return true;
            if (obj == null)
                return false;
            if (getClass() != obj.getClass())
                return false;
            Val other = (Val) obj;
            if (Double.doubleToLongBits(x) != Double.doubleToLongBits(other.x))
                return false;
            if (Double.doubleToLongBits(y) != Double.doubleToLongBits(other.y))
                return false;
            return true;
        }
    }

    public static void main(String... args) throws Exception {
        ArrayList<Double> list;
        String line;
        BufferedReader brMyHashval = new BufferedReader(new InputStreamReader(new FileInputStream("HashProblem.txt")));
        Hashtable<Val, ArrayList<Double>> numbers = new Hashtable<Val, ArrayList<Double>>();

        while ((line = brMyHashval.readLine()) != null) {
            if (!(line.isEmpty())) {
                String[] temp;
                temp = line.split(" ");
                Double eDouble = Double.parseDouble(temp[2].toString());

                Val key = new Val(Double.parseDouble(temp[0].toString()), Double.parseDouble(temp[1].toString()));

                if (!(numbers.containsKey(key))) {
                    list = new ArrayList<Double>();
                    numbers.put(key, list);
                    System.err.println("Created " + key.x + " " + key.y);
                } else {
                    list = numbers.get(key);
                }
                list.add(eDouble);
                System.err.println("Inserted into " + key.x + " " + key.y + " value " + eDouble + " size " + list.size() + " " + list);
            }
        }
    }

, ,

Created 1.0 2.0
Inserted into 1.0 2.0 value 9.0 size 1 [9.0] 
Created 3.0 4.0
Inserted into 3.0 4.0 value 9.0 size 1 [9.0]
Created 5.0 6.0
Inserted into 5.0 6.0 value 9.0 size 1 [9.0]
Inserted into 1.0 2.0 value 8.0 size 2 [9.0, 8.0]
Inserted into 5.0 6.0 value 8.0 size 2 [9.0, 8.0]
Inserted into 1.0 2.0 value 7.0 size 3 [9.0, 8.0, 7.0]
Created 7.0 8.0
Inserted into 7.0 8.0 value 7.0 size 1 [7.0]
Inserted into 3.0 4.0 value 7.0 size 2 [9.0, 7.0]
Inserted into 5.0 6.0 value 10.0 size 3 [9.0, 8.0, 10.0]
Inserted into 1.0 2.0 value 10.0 size 4 [9.0, 8.0, 7.0, 10.0]
Created 1.0 3.0
Inserted into 1.0 3.0 value 10.0 size 1 [10.0]
Created 1.0 4.0
Inserted into 1.0 4.0 value 10.0 size 1 [10.0]

, ?

- . , toString() , .

+1

All Articles