How to convert Java 8 map.remove to Java 1.6?

I have the following:

fruitMap.remove(fruitId, fruitProperties); 

FruitMap File:

 private Map<FruitId, FruitProperties> fruitMap = new HashMap<FruitId, FruitProperties>(); 

When I try to create my code, I get:

 ERROR The method remove(Object) in the type Map<MyImplementation.FruitId, FruitProperties> is not applicable for the arguments (Map<MyImplementation.FruitId, FruitProperties>) 

What is the problem?

Note that the thiis call is inside the removeFruit () method inside my FruitImplementation class.

+6
source share
7 answers

The remove(key, value) method removes the entry for key if it is currently mapped to value . This method was added in Java 1.8. Javadoc for the Map interface mentions the following default implementation:

 if (map.containsKey(key) && Objects.equals(map.get(key), value)) { map.put(key, newValue); return true; } else return false; 

Since the Objects class was added only in Java 1.7, for Java 1.6 you need to write an equality test yourself. So, if you do not need the return value of the method, you can replace map.remove(key, value) with:

 if (map.containsKey(key) { Object storedValue = map.get(key); if (storedValue == null ? value == null : storedValue.equals(value)) { map.remove(key); } } 

Please note that this is not thread safe. If you access the map from multiple threads, you will need to add a synchronized block.

+2
source

From Javadocs :

The default implementation is equivalent for this card:

 if (map.containsKey(key) && Objects.equals(map.get(key), value)) { map.remove(key); return true; } else return false; 

The default implementation makes no guarantees regarding the synchronization or atomic properties of this method. Any implementation that provides atomicity guarantees should override this method and document its concurrency properties.

So you can use this default implementation. Put it in a static helper method.

But if this should be thread safe, you may need to add some synchronization code (or consider using ConcurrentMap, which, by the way, already has a delete method from Java 5).

+7
source

You will need to check the value yourself:

 if(fruitProperties.equals(fruitMap.get(fruitId)) { fruitMap.remove(fruitId); } 

Note. My implementation here assumes that you are testing an object with non-zero fruitProperties .

+2
source

You need to do the following if your values โ€‹โ€‹cannot be null

 if (fruitProperties.equals(fruitMap.get(fruitId)) fruitMap.remove(fruitId); 

Note. To make it safe for threads, you need to wrap this in a synchronized block.

+1
source

Here's a complete solution, handling synchronization and specific cases, such as null values.

 synchronized (fruitMap) { if ((fruitMap.containsKey(fruitId) // The key is present && ( (fruitProperties == null && fruitMap.get(fruitId) == null) // fruitProperties is null, so is the stored value || (fruitProperties != null && fruitProperties.equals(fruitMap.get(fruitId))) ) ) { fruitMap.remove(fruitId); } } 

It works in Java 6, which is equivalent to:

 fruitMap.remove(fruitId, fruitProperties); 
+1
source

Objects.equals has the following implementation:

 public static boolean equals(Object a, Object b) { return (a == b) || (a != null && a.equals(b)); } 

Therefore, the default implementation is remove:

  if (map.containsKey(key) && Objects.equals(map.get(key), value)) { map.remove(key); return true; } else return false; 

May be written in Java 6 as:

 if (map.containsKey(key) && ((map.get(key) == value) || (map.get(key) != null && map.get(key).equals(value)))) { map.remove(key); return true; } else return false; 
+1
source

According to Java Doc delete (Object key, Object value)

Deletes the entry for the specified key only if it is currently mapped to the specified value.

If your equals () is correctly defined, you can do something like this

 FruitProperties valueFromMap = map.get(key); if(valueFromMap != null){ if( valueFromMap == originalValue || valueFromMap.equals(originalValue)){ map.remove(key); } } 

Now that you are using a simple HashMap, I assume that you will take care of thread safety by either synchronizing or changing it to ConcurrentHashMap :)

+1
source

Source: https://habr.com/ru/post/981135/


All Articles