How remove from two maps the identical keys

Laura Galera

I created two maps in Java that contain the same type of Keys and Values:

Map<Position,Movement> A;
Map<Position,Movement> B;

I want to remove from them both (not create a new Map) the keys that are the same. I don't care if the value is different or not. For example, if A has Position: a2, Movement: n,n and B has Position: a2, Movement: 1,2 those entries should be removed.

I wonder if there is a fast way to do that without iterating the shortest map and compare every single key.

Thanks

Arvind Kumar Avinash

You can use an Iterator on the keySet() of one of the maps and remove the element from the iterator and the other map if the key is present in the other map.

Iterator<String> itr = map1.keySet().iterator();
String key;
while (itr.hasNext()) {
    key = itr.next();
    if (map2.containsKey(key)) {
        itr.remove();
        map2.remove(key);
    }
}

Demo:

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

public class Main {
    public static void main(String[] args) {
        Map<String, String> map1 = new HashMap<String, String>();
        map1.put("a", "1");
        map1.put("b", "1");
        map1.put("x", "1");
        map1.put("c", "1");
        map1.put("z", "1");
        map1.put("d", "1");

        Map<String, String> map2 = new HashMap<String, String>();
        map2.put("w", "1");
        map2.put("x", "1");
        map2.put("b", "1");
        map2.put("y", "1");
        map2.put("c", "1");
        map2.put("z", "1");

        Iterator<String> itr = map1.keySet().iterator();
        String key;
        while (itr.hasNext()) {
            key = itr.next();
            if (map2.containsKey(key)) {
                itr.remove();
                map2.remove(key);
            }
        }

        System.out.println(map1);
        System.out.println(map2);
    }
}

Output:

{a=1, d=1}
{w=1, y=1}

[Update]

Solution using Stream:

map1.keySet()
    .stream()
    .filter(k -> map2.containsKey(k))
    .collect(Collectors.toList())
    .forEach(k -> {
        map1.remove(k);
        map2.remove(k);
    });

[Another Update]

Given below is a compact version (Thanks to Holger) of the first solution:

for (Iterator<String> itr = map1.keySet().iterator(); itr.hasNext();) {
    if (map2.keySet().remove(itr.next())) {
        itr.remove();
    }
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How remove from two maps the identical keys

From Dev

Create a list of lists from two maps with values of the common keys

From Dev

How do I remove identical items from a list and sort it in Python?

From Dev

How to remove consecutive identical words from a string in python

From Dev

How do I intersect two Maps by their keys in F#

From Dev

Java 8: how to 'JOIN' two Maps having the same Keys?

From Dev

How to merge two nested maps with same keys and keep the values

From Dev

How to remove the unwanted nested keys from JSON

From Dev

How to remove keys from TypedArray in JSON

From Dev

how to convert values of identical keys to list

From Dev

How to nicely intersect two sets build from two maps?

From Dev

How do I remove contiguous sequences of almost identical records from database

From Dev

Extract identical files from two directories

From Dev

Getting different hashes from two identical strings

From Dev

Merging two unordered_maps with overlapping keys

From Dev

Compare two maps for keys and return boolean

From Dev

How to remove flagged identical lines in text files

From Dev

How do maps check keys?

From Dev

How to remove data from a Google Maps Data Layer?

From Dev

How to remove RichMarker shadow class from google maps api?

From Dev

How Can I remove a marker from a Here Maps

From Dev

How to Remove from List Of Maps in DynamoDB (Must be Atomic)

From Dev

How to remove markers from google maps v3?

From Dev

How Can I remove a marker from a Here Maps

From Dev

How to remove building names and street names from google maps

From Dev

How to remove com.google.android.maps from a Xamarin project

From Dev

How to remove "find me" button from Google Maps widget in Android?

From Dev

How do I select records with max from id column if two of three other fields are identical

From Dev

How does git deal with identical branch names from two different remote repo?

Related Related

  1. 1

    How remove from two maps the identical keys

  2. 2

    Create a list of lists from two maps with values of the common keys

  3. 3

    How do I remove identical items from a list and sort it in Python?

  4. 4

    How to remove consecutive identical words from a string in python

  5. 5

    How do I intersect two Maps by their keys in F#

  6. 6

    Java 8: how to 'JOIN' two Maps having the same Keys?

  7. 7

    How to merge two nested maps with same keys and keep the values

  8. 8

    How to remove the unwanted nested keys from JSON

  9. 9

    How to remove keys from TypedArray in JSON

  10. 10

    how to convert values of identical keys to list

  11. 11

    How to nicely intersect two sets build from two maps?

  12. 12

    How do I remove contiguous sequences of almost identical records from database

  13. 13

    Extract identical files from two directories

  14. 14

    Getting different hashes from two identical strings

  15. 15

    Merging two unordered_maps with overlapping keys

  16. 16

    Compare two maps for keys and return boolean

  17. 17

    How to remove flagged identical lines in text files

  18. 18

    How do maps check keys?

  19. 19

    How to remove data from a Google Maps Data Layer?

  20. 20

    How to remove RichMarker shadow class from google maps api?

  21. 21

    How Can I remove a marker from a Here Maps

  22. 22

    How to Remove from List Of Maps in DynamoDB (Must be Atomic)

  23. 23

    How to remove markers from google maps v3?

  24. 24

    How Can I remove a marker from a Here Maps

  25. 25

    How to remove building names and street names from google maps

  26. 26

    How to remove com.google.android.maps from a Xamarin project

  27. 27

    How to remove "find me" button from Google Maps widget in Android?

  28. 28

    How do I select records with max from id column if two of three other fields are identical

  29. 29

    How does git deal with identical branch names from two different remote repo?

HotTag

Archive