Delete a record from the card without repeating

How to delete a record from Java Map without using iteration using a value or key. Mostly on my map, I use containsKey() , then map.remove() to remove it.

+6
source share
8 answers

You delete the entry from the card using the key of the item you want to delete.

 map.remove("aKey"); 

If you do not know the key of the element, you must iterate it to get it, for example:

 public static <T, E> Set<T> getKeysByValue(Map<T, E> map, E value) { Set<T> keys = new HashSet<T>(); for (Entry<T, E> entry : map.entrySet()) { if (value.equals(entry.getValue())) { keys.add(entry.getKey()); } } return keys; } 

This will return all the keys that were found on the map.

+14
source
 Map<Integer, String> abcMap = new HashMap<Integer, String>(); abcMap.put(1,"A"); abcMap.put(2,"B"); abcMap.put(3,"C"); /// now for remove item abcMap.remove(1); // this will remove "A" from abcMap.. 
+5
source

Use Map#remove(Object key) :

 public V remove(Object key) 

Deletes the display of this key from this card, if present (additional operation). More formally, if this map contains a mapping from key k to v, such that (key==null ? k==null : key.equals(k)) , this mapping is deleted. (A map can contain no more than one such display.)

Returns the value to which the map was previously bound, or null if there was no mapping in the map for this key. (A null return may also indicate that the previously associated card is null with the specified key, if the implementation supports null values.) The card will not contain a mapping for the specified key after the call is returned.

In principle, you can call remove even if the key does not exist. In this case, it will fail (return null). An object does not have to be identical or the same if yourKey.equals(key) true for the key that you want to delete.

+2
source
 public Object remove(Object key) 

remove in Map .

From javadoc :

Deletes the display of this key from this card, if present (additional operation). More formally, if this map contains a map from key k to v, so that (key==null ? k==null : key.equals(k)), , that map is deleted. (A map can contain no more than one such display.)

Returns the value to which the map was previously bound, or null if there was no mapping in the map for this key. (A null return may also indicate that the previously associated card is null with the specified key, if the implementation supports null values.) The card will not contain a mapping for the specified key after the call returns.

Options:

key - a key whose display must be removed from the card.

Returns

The previous value associated with the specified key, or null if there was no mapping for the key.

Example:

 Map<Integer, String> map=new HashMap<>(); map.put(20, "stackOverflow"); System.out.println(map.remove(20)); 

This code will print "stackOverflow" ie The previous value associated with the specified key. Hope it helps.

+2
source

In most cases, you can delete an entry (key-value pair) from the map using the Map.remove(key) method. There will be no iterations on the map. You do not need to use Map.containsKey(key) until Map.remove(key) , because Map.remove already does this. It returns either the previous associated value with key , or null if there was no mapping for key .

But you cannot do the same using only the value (without the key). The only option is to iterate over the map and find the record (or records) with this value.

Java 5-7:

 for (Iterator<MyValue> it = map.values.iterator(); it.hasNext(); ) { if(it.next().equals(myValue)) { it.remove(); } } 

Java 8:

 map.values().removeIf(v -> v.equals(myValue)); 
+2
source

Use the Map remove method , which takes the key as an argument. You do not need the original object; you just need an object that compares with the existing key on the map using equals and creates the same hashCode as the existing key on the map.

+1
source

Nobody seems to have actually answered the OP question. They asked how to delete a record using an iterator using a value or key . Of course, remove(key) works if you have a key, but not if you are looking for a value.

Alfredo's answer was the closest as it searches the side of the Map value but uses an iterator.

It uses a solution using the Java 8 Stream API; no iterators:

 Map<K, V> newMap = map.entrySet().stream() .filter(entry -> !entry.getKey().equals(searchValue)) .filter(entry -> !entry.getValue().equals(searchValue)) .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)); 

(Substitute K and V for your key types and values.)

+1
source

public static void main(String[] args) { //Constant.mapCustomer.remove("s"); Map<String, String> mapCustomer=new HashMap<String, String> ();; mapCustomer.put("s","ss"); System.out.println(mapCustomer.get("s")); mapCustomer.remove("s"); System.out.println(mapCustomer.get("s")); }

0
source

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


All Articles