LinkedHashMap stores ordered key-value pairs and iterative methods

HashMap stores key-value pairs out of order:

package com.example.myapp.linkedhashmap;

import org.junit.Test;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * Created by mike.
 * Created on 2020/4/8.
 */
public class LinkedHashMapTest {
@Test
    public void main() {
        Map<String, String> linkedHashMap = new HashMap<String, String>();

        linkedHashMap.put("编排年分和日期00", "甲");
        linkedHashMap.put("编排年分和日期01", "乙");
        linkedHashMap.put("编排年分和日期02", "丙");
        linkedHashMap.put("编排年分和日期03", "丁");
        linkedHashMap.put("编排年分和日期04", "戊");
        linkedHashMap.put("编排年分和日期05", "己");
        linkedHashMap.put("编排年分和日期06", "庚");
        linkedHashMap.put("编排年分和日期07", "辛");
        linkedHashMap.put("编排年分和日期08", "壬");
        linkedHashMap.put("编排年分和日期09", "癸");

        for (Map.Entry<String, String> entry : linkedHashMap.entrySet()){
            if(!"".equals(entry.getValue())){
                System.out.println(entry.getKey() + "\t" + entry.getValue());
            }

        }

    }
}

Print the result:

编排年分和日期05	己
编排年分和日期04	戊
编排年分和日期07	辛
编排年分和日期06	庚
编排年分和日期01	乙
编排年分和日期00	甲
编排年分和日期03	丁
编排年分和日期02	丙
编排年分和日期09	癸
编排年分和日期08	壬

LinkedHashMap stores the key-value pairs in order, and the acquisition is also in accordance with the storage order;

package com.example.myapp.linkedhashmap;

import org.junit.Test;

import java.util.LinkedHashMap;

/**
 * Created by mike.
 * Created on 2020/4/8.
 */
public class LinkedHashMapTest {
    @Test
    public void main() {
        LinkedHashMap<String, String> linkedHashMap = new LinkedHashMap<String, String>();

        linkedHashMap.put("编排年分和日期00", "甲");
        linkedHashMap.put("编排年分和日期01", "乙");
        linkedHashMap.put("编排年分和日期02", "丙");
        linkedHashMap.put("编排年分和日期03", "丁");
        linkedHashMap.put("编排年分和日期04", "戊");
        linkedHashMap.put("编排年分和日期05", "己");
        linkedHashMap.put("编排年分和日期06", "庚");
        linkedHashMap.put("编排年分和日期07", "辛");
        linkedHashMap.put("编排年分和日期08", "壬");
        linkedHashMap.put("编排年分和日期09", "癸");

        for (LinkedHashMap.Entry<String, String> entry : linkedHashMap.entrySet()) {
            if (!"".equals(entry.getValue())) {
                System.out.println(entry.getKey() + "\t" + entry.getValue());
            }

        }

    }
}

Get the result:

编排年分和日期00	甲
编排年分和日期01	乙
编排年分和日期02	丙
编排年分和日期03	丁
编排年分和日期04	戊
编排年分和日期05	己
编排年分和日期06	庚
编排年分和日期07	辛
编排年分和日期08	壬
编排年分和日期09	癸

It should be noted:

1. When traversing LinkedHashMap, do not use map.keySet (); and map.keySet (). Iterator (); traversal, these two ways of traversal, if the LinkedHashMap collection is operated during the traversal, java will be reported .util.ConcurrentModificationException exception, the reason is: if the current element is manipulated during the iteration process, according to the rules of the LinkedHashMap access order, the current element will be placed at the end, when the next cycle is executed, it is to get the last element The last element of the, so it will report an error.

2. The meaning of the accessOrder parameter and the impact of LinedHashMap in storing data for a long time without use, please refer to the reference:

https://blog.csdn.net/baidu_39534448/article/details/103935450      said in detail

 

 

Looking at the analysis of LinkedHashMap source code, we can know that LinkedHashMap has 5 constructors:

Among these 5 constructors, only one constructor can set the accessOrder parameter,

When accessOrder is true: the stored data is out of order

package com.example.myapp.linkedhashmap;

import org.junit.Test;

import java.util.LinkedHashMap;

/**
 * Created by mike.
 * Created on 2020/4/8.
 */
public class LinkedHashMapTest {
    @Test
    public void main() {
        LinkedHashMap<Integer, String> linkedHashMap = new LinkedHashMap<Integer, String>(10, 0.75f, true);

        linkedHashMap.put(0, "甲");
        linkedHashMap.put(1, "乙");
        linkedHashMap.put(2, "丙");
        linkedHashMap.put(3, "丁");
        linkedHashMap.put(4, "戊");
        linkedHashMap.put(5, "己");
        linkedHashMap.put(6, "庚");
        linkedHashMap.put(7, "辛");
        linkedHashMap.put(8, "壬");
        linkedHashMap.put(9, "癸");
        linkedHashMap.put(7, "辛00");

        for (LinkedHashMap.Entry<Integer, String> entry : linkedHashMap.entrySet()) {
            if (!"".equals(entry.getValue())) {
                System.out.println(entry.getKey() + "\t" + entry.getValue());
            }

        }

    }
}

Output result:

0	甲
1	乙
2	丙
3	丁
4	戊
5	己
6	庚
8	壬
9	癸
7	辛00

 

When accessOrder is false: the stored data is ordered

package com.example.myapp.linkedhashmap;

import org.junit.Test;

import java.util.LinkedHashMap;

/**
 * Created by mike.
 * Created on 2020/4/8.
 */
public class LinkedHashMapTest {
    @Test
    public void main() {
        LinkedHashMap<Integer, String> linkedHashMap = new LinkedHashMap<Integer, String>(10, 0.75f, false);

        linkedHashMap.put(0, "甲");
        linkedHashMap.put(1, "乙");
        linkedHashMap.put(2, "丙");
        linkedHashMap.put(3, "丁");
        linkedHashMap.put(4, "戊");
        linkedHashMap.put(5, "己");
        linkedHashMap.put(6, "庚");
        linkedHashMap.put(7, "辛");
        linkedHashMap.put(8, "壬");
        linkedHashMap.put(9, "癸");
        linkedHashMap.put(7, "辛00");

        for (LinkedHashMap.Entry<Integer, String> entry : linkedHashMap.entrySet()) {
            if (!"".equals(entry.getValue())) {
                System.out.println(entry.getKey() + "\t" + entry.getValue());
            }

        }

    }
}

Output result:

0	甲
1	乙
2	丙
3	丁
4	戊
5	己
6	庚
7	辛00
8	壬
9	癸

It should be noted that if no parameters are passed in, the default is false, that is, ordered data.

 

 

 

 

 

 

 

 

 

 

 

 

Published 57 original articles · liked 0 · 10,000+ views

Guess you like

Origin blog.csdn.net/java9832/article/details/105400947