0

I need to have clarified what's behind map.put() method's logic while updating.

By instance:

I have a Map which inside these few records:

{
  [K="key1", V="value1"],
  [K="key2", V="value2"],
  [K="key3", V="value3"]
}

If I wish to update record number two I'd write map.put("key2", "value22") and Java automatically replace value2 with value22.

How could do that Java?

If the key had been a complex object composed by a few attributes...

{
  [K=new CustomKey("something1.1", "something2.1"), V="value1"],
  [K=new CustomKey("something1.2", "something2.2"), V="value2"],
  [K=new CustomKey("something1.3", "something2.3"), V="value3"]
}

...and I wish to update value2 searching for it by a key with only one attribute valorized, how is possible to do something like this:

map.put(new CustomKey("something1.2"), "value22");

or do I need to provide exactly the entire CustomKey object?

Andrea Grimandi
  • 631
  • 2
  • 8
  • 32

6 Answers6

2

Search is done using the whole key, not only a part of it.

Internally there are two methods that are invoked on the key object and that are used to find the associated value. Those methods are equals() and hashCode()

If you write a custom class CustomKey for a more complex key you have to rewrite those methods (equals() and hashCode()), otherwise you will have unexpected results. Please refer to the javadoc documentation of class Object where you will find the guidelines to write reliable implementations of those methods.

Davide Lorenzo MARINO
  • 26,420
  • 4
  • 39
  • 56
2

If I wish to update record number two I'd write map.put("key2", "value22") and Java automatically replace value2 with value22.

How could do that Java?

In a Map you need to understand the concept of hashCode and equals first. For every record first hash-code will be computed that will determine the bucket for this record. It is possible for multiple records to have same hash-code and in that case they will be stored in a linked list.

How search for a record works?

First of all hash-code will be computed to determine the bucket. Then for every record (entry) in the list corresponding to that bucket, it will be check for equality usingequals method.The default implementation of these methods simply checks for instance equality (in other words, two Objects will only be equal if they are in fact the same object).

I wish to update value2 searching for it by a key with only one attribute valorized, how is possible to do something like this:

map.put(new CustomKey("something1.2"), "value22");

or do I need to provide exactly the entire CustomKey object?

Yes you need to provide exactly the same key else it will not be able to locate the records in the map. The computation for hash-code will be using the complete key and not a part of it. You need to provide (override actually) implementation of these two methods.

akhil_mittal
  • 23,309
  • 7
  • 96
  • 95
1

The code you described is already valid in Java. If you write

map.put(obj1, val1);
map.put(obj1, val2);

map.get(obj1); //Returns val2 with no errors

This is considered valid syntax.

Xirema
  • 19,889
  • 4
  • 32
  • 68
0

A map is a collection, it has no logic specific to your application.

For general information about the functionning of a map, pease look at the java.util.Map javadoc

Here it is said than adding an already present value replace the old one

Steve Marion
  • 289
  • 1
  • 9
0

do I need to provide exactly the entire CustomKey object?

Yes, you have. Keys are compared via equals method of underlying objects. So even if you have another 1-arg constructor for CustomKey constructed object will not be equal to key object in Map and put method will not replace value object.

ka4eli
  • 5,294
  • 3
  • 23
  • 43
0

Reached the final solution after some days: Implement a delegate which inside a custom put() method. In my opinion it's a quite AWFUL approach ciclyng a Map until some key has what you're looking for. Thought Java had something like IEqualityComparer in C# but I was wrong.

Andrea Grimandi
  • 631
  • 2
  • 8
  • 32