Map collection study notes in Java

Map overview

    The Map interface is a two-column set, each element of which contains a key object key and a value object value, and the key and value have a one-to-one correspondence. When accessing elements in the map, as long as you find the specified key, you can find the corresponding value.
Insert picture description here

HashMap collection

    The HashMap collection is an implementation class of the Map interface. It is thread-unsafe and is used to store key-value mapping relationships, but it is guaranteed that the same keys cannot appear. If there are the same keys, sum up one sentence: the keys are the same, the values ​​are overwritten .
Examples:

public class Exception05 {
    
    
    public static void main(String[] args) {
    
    
        //创建一个map
        Map hashMap = new HashMap();
        //通过put(key, value)方法存储
        hashMap.put("name1", "张一");
        hashMap.put("name1", "李四");
        hashMap.put("name2", "张二");
        hashMap.put("name3", "张三");
        hashMap.put("name4", "张四");
        //通过get(key)方法取值输出
        System.out.println("取出key为name1的value为:" + hashMap.get("name1"));
        //map集合的遍历
        for (Object entry : hashMap.entrySet()){
    
    
            System.out.println(entry);
        }
    }
}

Operation result:
Insert picture description here
    We use the put() method to save five keys and corresponding values. One key is the same. From the results, it can be seen that the value equal to Zhang Yi is not output and printed. This is the key is the same, the value is overwritten.

LinkedHashMap collection

    As can be seen from the running results of the above example, the results output by traversing the map collection are not in the order in which we store them. If you want to make the traversal result output consistent with the stored results, you must use the LinkedHashMap class, which is a subclass of HashMap. . It also uses a doubly linked list to maintain the relationship of internal elements.
Examples:

public class Exception06 {
    
    
    public static void main(String[] args) {
    
    
        //创建LinkedHashMap集合
        Map linkedHashMap = new LinkedHashMap();
        //存储值
        linkedHashMap.put("1","jack1");
        linkedHashMap.put("2","jack2");
        linkedHashMap.put("3","jack3");
        //循环遍历输出
        for (Object entry: linkedHashMap.entrySet()){
    
    
            System.out.println(entry);
        }
    }
}

Operation result:
Insert picture description here
    It can be seen from the operation result that the order of the iterated elements is the same as the order in which they are stored.

TreeMap collection

    TreeMap is also an implementation class of the Map interface. The collection is used to store the key-value mapping relationship. It does not allow duplicate keys . The TreeMap collection uses the principle of a binary tree to ensure the uniqueness of the keys.
Examples:

public class Exception07 {
    
    
    public static void main(String[] args) {
    
    
        Map treeMap = new TreeMap();//创建TreeMap集合
        treeMap.put("1","张三");//存入三个值
        treeMap.put("2","李四");
        treeMap.put("3","王二");
        Set keySet = treeMap.keySet();//获得键的集合
        Iterator iterator = keySet.iterator();//获得iterator对象
        while (iterator.hasNext()){
    
    //判断是否存在下一个对象
            Object key = iterator.next();//取出元素
            Object value = treeMap.get(key);//根据获得的键找对应的key
            System.out.println(key + "=" + value);
        }
    }
}

operation result:
Insert picture description here

Properties collection

    The Properties class is an implementation class of Hashtable, and Hashtable implements the Map interface. Hashtable and Hashmap are very similar, the difference is that Hashtable is thread-safe, it is very slow in accessing elements, and it is basically not used now. But a subclass of Hashtable is Properties. However, Properties are very important in practical applications. In actual development, the Properties collection is often used to access the configuration items of the application.
Come on! ! !
Next time I will learn how to traverse List and Map collections.

Guess you like

Origin blog.csdn.net/qq_42494654/article/details/109588531