Further comprising determining whether the map map

  Further comprising determining whether the map map:

Unlike the list set map, list set directly determines whether the set contains a collection of methods or other elements.

  • boolean contains(Object o) 

    If the list contains the specified element, it returns true.

  • boolean containsAll(Collection<?> c) 

    If the list contains all of the elements of the specified collection, it returns true.

Although not directly map is determined whether the map contains other methods, but the method has a map in accordance with the map key or value exists.

  • containsKey(Object key) 

     If this map contains a mapping for the specified key relationship, then return true.

  • containsValue(Object value) 

    If this mapping one or more keys to the specified value, returns true.

 

With the above two methods, you can take the other way to judge the conversion. Ideas are as follows:

  Since only compare key and value, and to judge whether the map contains another map, while comparing it to the key value, the method may be: a string key and value transformed into a particular format, or as a key value, performing comparison of key or value, you can achieve the effect of simultaneous comparison of the key and value.

Of course, this process is also used in the traversal of the map: map traverse a variety of ways:

1, Map.Entry traversal

for (Map.Entry<Integer, Integer> entry : map.entrySet()) {
     System.out.println("key = " + entry.getKey() + ", value = " + entry.getValue());
}

  This way you can get to the key and value.

 

2, keySet traversal

for (Integer key : map.keySet()) {
    System.out.println("key = " + key);
}

  In this way traversal traversal key

 

3, values ​​traversal

for (Integer value : map.values()) {
      System.out.println("key = " + value);
}

  In this way traversal value.

 

4, Iterator traversal

Iterator<Map.Entry<Integer, Integer>> it = map.entrySet().iterator();
     while (it.hasNext()) {
         Map.Entry<Integer, Integer> entry = it.next();
         System.out.println("key = " + entry.getKey() + ", value = " + entry.getValue());
}

 

A simple example is as follows:

public static void main(String[] args) {
        Map<String, String> map1 = new HashMap<>();
        Map<String, String> map2 = new HashMap<>();
        Map<String, String> mapA = new HashMap<>();
        map1.put("a", "b");
        map1.put("b", "d");
        map1.put("a", "c");
        map1.put("e", "r");

        map2.put("a", "b");
        map2.put("a", "c");

        for (Map.Entry<String, String> entry : map1.entrySet()) {
            String map1Key = entry.getKey();
            String map1Value = entry.getValue();
            String map1KeyVal = map1Key + ":" + map1Value;
            String map1ValKey = map1Value + ":" + map1Key;
            mapA.put(map1KeyVal, map1ValKey);
        }

        // 判断map1是否包含map2
        for (Map.Entry<String, String> entry : map2.entrySet()) {
            String map2Key = entry.getKey();
            String map2Value = entry.getValue();
            String map2KeyVal = map2Key + ":" + map2Value;
            boolean keyExist = mapA.containsKey(map2KeyVal);
            boolean valExist = mapA.containsValue(map2KeyVal);
            if (keyExist == false){
                System.out.println("map1 不包含 map2");
            }
        }
    }

 

Guess you like

Origin www.cnblogs.com/jylee/p/11283938.html
Map
Map
map
map