[JAVA] LinkedHashMap referenced in insertion order and its usage example

Map all the time. ..

Map<key, value> map = new HashMap()<>;

I only knew. However, there is something called LinkedHashMap </ b>!

So, how to use it.

What is LinkedHashMap?

This is the best map that keeps the order of insertion </ b>.

As a scene to use ・ I want to arrange in the order of insertion ・ Arrange in an order that humans can understand, but computers do not.

I will introduce the scene of.

Sample contents

This is a sample that selects and displays multiple prefectures you want to go to </ b>.

Often "To Hokkaido in the north and to Okinawa in the south" But basically, they line up from north to south. .. ..

As a list ·Hokkaido ·Miyagi Prefecture ·Tokyo ・ Osaka ・ Aichi Prefecture ·Fukuoka Prefecture To set.

Insert in order from the north as described above.

For HashMap

import java.util.*;

public class Main {
    public static void main(String[] args) throws Exception {
        
        //List that contains the pressed prefecture
        List<String> prefectureClickedList = new ArrayList();
        //List of prefectures Map
        Map<String, Boolean> prefectureMap = new HashMap();
//        Map<String, Boolean> prefectureMap = new LinkedHashMap();
        {
            //Insert from the north
            prefectureMap.put("Hokkaido", false);
            prefectureMap.put("Miyagi Prefecture", false);
            prefectureMap.put("Tokyo", false);
            prefectureMap.put("Osaka", false);
            prefectureMap.put("Aichi prefecture", false);    
            prefectureMap.put("Fukuoka Prefecture", false);
        }
        
        //Pressing process (in different order)
        prefectureMap.put("Fukuoka Prefecture", true);
        prefectureMap.put("Osaka", true);
        prefectureMap.put("Tokyo", true);
        prefectureMap.put("Hokkaido", true);
        
        //Add to prefectureClickedList
        for(String prefecture: prefectureMap.keySet()) {
            Boolean bool = prefectureMap.get(prefecture);
            if(bool){
                prefectureClickedList.add(prefecture);
            }
        }
        
        //Output String
        String outPrefecture = new String();
        
        //Midpoint"・"Put in"
        for(String clickedPref : prefectureClickedList) {
            outPrefecture = outPrefecture + clickedPref + "・";
        }
        outPrefecture = outPrefecture.substring(0, outPrefecture.length() -1);
        
        System.out.println("Prefecture you want to go to:" + outPrefecture);
        
    }
}

Output result

Prefectures you want to go to: Hokkaido, Osaka, Tokyo, Fukuoka

Well, I want to change the order of Osaka and Tokyo! !! Ww

For LinkedHashMap


import java.util.*;

public class Main {
    public static void main(String[] args) throws Exception {
        
        //List that contains the pressed prefecture
        List<String> prefectureClickedList = new ArrayList();
        //List of prefectures Map
//        Map<String, Boolean> prefectureMap = new HashMap();
        Map<String, Boolean> prefectureMap = new LinkedHashMap();
        {
            //Insert from the north
            prefectureMap.put("Hokkaido", false);
            prefectureMap.put("Miyagi Prefecture", false);
            prefectureMap.put("Tokyo", false);
            prefectureMap.put("Osaka", false);
            prefectureMap.put("Aichi prefecture", false);    
            prefectureMap.put("Fukuoka Prefecture", false);
        }
        
        //Pressing process (in different order)
        prefectureMap.put("Tokyo", true);
        prefectureMap.put("Fukuoka Prefecture", true);
        prefectureMap.put("Osaka", true);
        prefectureMap.put("Hokkaido", true);
        
        //Add to prefectureClickedList
        for(String prefecture: prefectureMap.keySet()) {
            Boolean bool = prefectureMap.get(prefecture);
            if(bool){
                prefectureClickedList.add(prefecture);
            }
        }
        
        //Output String
        String outPrefecture = new String();
        
        //Midpoint"・"Put in"
        for(String clickedPref : prefectureClickedList) {
            outPrefecture = outPrefecture + clickedPref + "・";
        }
        outPrefecture = outPrefecture.substring(0, outPrefecture.length() -1);
        
        System.out.println("Prefecture you want to go to:" + outPrefecture);
        
    }
}

Output result

Prefectures you want to visit: Hokkaido, Tokyo, Osaka, Fukuoka

Yup. There is no sense of discomfort.

Highest & highest!

Summary

・ Order of prefectures: North to south -Washing order: wash, rinse, dehydrate ・ Four seasons: Spring, summer, autumn, winter -Char's MS: Zaku, Z'Gok, (Rick Dom), Gelgoog, Zeong. ..

These are commonplace for humans to see at a glance.

Check the latitude and longitude of Hokkaido and the latitude and longitude of Fukuoka, and arrange them in order from the north. .. You can do it, but I was able to deal with it by putting them in predetermined order </ b>.

at the end

Why didn't I know such a wonderful Linked Hash Map? .. p4.jpg Because I'm a little boy </ b>