1

I have defined a hashmap of type :

HashMap<Position, Double> list = new HashMap<>(); 

Where Position is class with two attributes x and y .

I would like to verify if a position is already in the list or not, I have tried this :

public void addToList(Position p, double somme) { if (this.list.containsKey(p)) { this.list.replace(p, this.list.get(p) + somme);//add the old value somme } else { this.list.put(p, somme); } } 

I think I should be more specific and verify values of x any y instead of check if key is exist because each time the check failed to detect an exist position.

How can check if a position is in the list or not?

2
  • What does Position.equals check? Would this not already be checking the x and y? Commented Apr 12, 2020 at 18:21
  • 5
    (Don't call a variable of type HashMap "list". That's just confusing) Commented Apr 12, 2020 at 18:23

2 Answers 2

3

You should override the equals and hashCode methods of your Position object so that two elements with the same value for x and y are equals.

@Override public boolean equals(Object obj) { if (obj == this) { return true; } if (obj == null || obj.getClass() != this.getClass()) { return false; } Position otherPos = (Position) obj; return x == otherPos.x && y == otherPos.y; } @Override public int hashCode() { return Objects.hash(x, y); } 
Sign up to request clarification or add additional context in comments.

6 Comments

You also need hashCode for that, not just equals.
I still have a problem when i use list.put and hashCode(), i want to add to the list points even with same (x,y). for example (2,3) and (2,3).it is possible only if i disable hashCode().
Your list variable is a HashMap. HashMap can not store duplicated keys, they automatically do what you asked for (if the key exists the last value is stored) so to update the value you just need to put the new value to that same key. You might want to create another more specific question if you want something different now
If you need to store different values to the same key in a hashmap, which doesn't seem to be the case according to the question, check this: baeldung.com/java-map-duplicate-keys
I have followed the instructions and when i tried to implement :---> protected Multimap<Position, Double> donneeTraitee = TreeMultimap.create();, i got this error : Type mismatch: cannot convert from TreeMultimap<Comparable,Comparable> to Multimap<Position,Double> i changed it and then i have got this warning : Comparable is a raw type. References to generic type Comparable<T> should be parameterized
|
3

Implement both equals AND hashcode in your Position class, based on the x and y properties. Hashmap uses the latter to find the bucket in which to check for the key - equals alone won't work.

Note that hashcode and equals must be consistent in that two objects that are equal must have the same hashcode.

As the other answer mentioned, implement those and containsKey method based on a Position will work as you expect.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.