当前位置:网站首页>Map collection traversal, adding, replacing and deleting elements

Map collection traversal, adding, replacing and deleting elements

2022-06-21 13:25:00 zhangsan3333

  • map It will be saved in the order of storage value
  • Access can only be based on key obtain value, Cannot pass value obtain key.

Add elements :

  • public V put(K key, V value): Add the specified key and specified value to Map Collection .

    explain :

    ​ In the use of put Store a pair of elements (key-value) Object time , Will take it first key To judge Map Whether the collection already exists .

    If Map There are no identical in the set key There is : Just put key-value Store in Map Collection , And back to null value .

    If Map There are the same in the set key There is : Will put the previously stored value Object to cover . And return to the previous value object ( used value object ).

    This can be understood as modifying value, But it can't be modified key.

    Be careful : because Map It's the interface , Cannot create object , Only use Map The following subclasses HashMap Create objects .

Remove elements :

  • public V remove(Object key): Put the specified key The corresponding key value pair element stay Map Delete... From the collection , Returns the value of the deleted element .
    ​ remove It's based on key Delete the current key Corresponding value This set of data :

    If Map The set contains the specified key The same elements exist : Delete a pair key-value object , Instead of just deleting value, And return to delete this group value value ;

    If Map There is no relation to the specified in the collection key The same elements exist : No delete operation , return null;

    problem : Why not through value To delete a set of data ?

    because key Is the only one. , and value There can be multiple , So we can't go through value To delete a set of data , Only through key Value to delete .

  • Empty the set .void clear()

package MapTest;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

public class Map12 {
    
    public static void main(String[] args) {
    
        Map<Integer, String> map = new HashMap<>();
        map.put(1, "0000049259131");
        map.put(2, "0000049363609");
        map.put(3, "0000049363685");
        map.put(4, "0000049363654");
        map.put(5, "0019428006716");
        map.put(6, "0010343864245");
        map.put(7, "0010343865709");
        map.put(8, "0010343865716");
        map.put(9, "0010343865723");
        map.put(10, "0010343865730");
        map.put(11, "0010343865747");
        map.put(12, "0010343865754");
        map.put(13, "0010343865761");
        map.put(14, "0010942205456");
        map.put(15, "0010942206545");
        map.put(16, "0010942206613");
        map.put(17, "0010942206699");
        map.put(18, "0010942207528");
        //put Method actually has a return value , yes V type 
        String put1 = map.put(19, "0010942207542");
        //map The collection has no duplicates key, return null, Deposit in key-value
        String put2 = map.put(20, "2022032600003");
        //map Collection has duplicate key, Go back to the old value, Overwrite old value, Replace with new value
        String put3 = map.put(20, "2022032600001");

        System.out.println("put1 = " + put1);
        System.out.println("put2 = " + put2);
        System.out.println(" used value = " + put3);

        System.out.println("map = " + map);

        String remove = map.remove(20);
        System.out.println(" Remove elements  = " + remove);
        // Re insert the element 
        map.put(20, "2022032600002");

        // By traversing keySet, obtain value
        Set<Integer> keys = map.keySet();
        for (Integer key : keys) {
    
            String value = map.get(key);
            System.out.println(key + "------" + value);
        }

        map.clear();
        System.out.println(" After emptying map = " + map);
        // Print nothing 
        for (Integer key : keys) {
    
            String value = map.get(key);
            System.out.println(key + "------" + value);
            System.out.println(" Yes ...");
        }
        System.out.println(" end ");
    }
}

 Insert picture description here

原网站

版权声明
本文为[zhangsan3333]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/172/202206211050320881.html