[JAVA] Note to have multiple values in HashMap

Introduction

Arrays are obsolete, and it's even suggested (not) that they can be fueled. Around this time, I had the opportunity to use HashMap, especially LinkedHashMap, so I'll take a note for myself. I'm sorry for the basics.

Use

There are several types of Map systems. It doesn't matter which one you use to put it in or take it out normally, but it makes a big difference when managing what's inside.

name the difference
HashMap The sequence of key and value is appropriate
Hashtable keys are in descending order
TreeMap keys are in ascending order
LinkedHashMap FIFO,LRU(Order obtained)

Basically, just put and get. I'm not doing anything else. However, if the key has the same name, it will be that, so be careful. Even if the value is the same, it will not be overwritten if the key is different. The code below continues to output as much as the loop is set.

Sample.java


import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

public class Main {

	public static void main(String[] args) {
		int loop = 50;
		 //Map declaration
        Map<String, Integer> map = new HashMap<String, Integer>();
        
        //Add element to Map
        map.put("uni", 1);
        map.put("kura", 2);
        map.put("kurage", 3);
        map.put("nameko", 4);
        map.put("nameko", 5);
        
         //Turn the iterator
        Iterator<Map.Entry<String, Integer>> itr = map.entrySet().iterator();
        
        // key,Get value
        while(true) {
            //Use next to get the value
            Map.Entry<String, Integer> entry = itr.next();
            
            //When you reach the end, go back to the beginning
            if(!itr.hasNext()) {
            	if(loop < 0) {
            		break;
            	}
            	loop--;
            	itr = map.entrySet().iterator();
            	
            }
            System.out.println(entry.getKey() + " : " + entry.getValue());
        }
	}

}

The caveat with the above code is that it doesn't come out in the order you put it in. It's a normal Map, so it needs to be different. If you are in trouble, you can use LinkedHashMap. For details on each, please go to read each explanation.

Have multiple values

This time, it's a little bit, but I have to have two values for the key. For the time being, I found that I can have a List in the Map. It is a complicated and angry structure for me as a beginner. List is different from Map, put in with add and get with get. The image is like this: key = "unikura", value = {1993,25} When I declared this with LRU of LinkedHashMap, it became like this

LinkedHashMap


//The third argument is important
//false: FIFO (default), true: LRU
    LinkedHashMap<String, List<Integer>> Samplekey = new LinkedHashMap<String, List<Integer>>(30, 0.75f, true);
    List<Integer> Samplevalues = new ArrayList<Integer>();

Conceptually (I don't know) the interface, it seems normal to write them separately. The writing style below seems to be normal, so please use that. I think Map is the same.

List<Data type>List name= new ArrayList<Data type>(Initial size); 

Now it is necessary to think about how to use ** LinkedList ** and ** ArrayList ** properly. What I'm trying to do this time is to frequently rewrite the contents of the List. ** ArrayList is numbered in sequence, so random access to specific elements is possible. ** LinkedList can't do that, so I wondered if it would be better to use ArrayList.

Write in another class (finally over here)


public class Sample {
    private int populality;
    private int freshness;

    public Sample(int populality, int freshness) {
        this.populality = populality;
        this.freshness = freshness;
    }
    public Sample() {
        this.populality = 0;
        this.freshness = 0;
    }
    public int getPopulality() {
        return populality;
    }
    public void setPopulality(int populality) {
        this.populality = populality;
    }
    public int getFreshness() {
        return freshness;
    }
    public void setFreshness(int freshness) {
        this.freshness = freshness;
    }
}
private LinkedHashMap<String, Sample> Map = new LinkedHashMap<String, Sample>(30, 0.75f, false);
Sample info = this.Map.get(kename);
int Freshness = info.getFreshness();
int getPopulality = info.getPopulality();
info.setFreshness(0);
info.setPopulality(0);

Finally

I'm sorry, it got messed up (although I only need to know) If the number of values is small, it seems easier to write it by yourself.

References

http://kaworu.jpn.org/kaworu/2008-04-10-2.php https://code.i-harness.com/ja-jp/q/7d9261 https://www.task-notes.com/entry/20160402/1459595902 https://qiita.com/BumpeiShimada/items/522798a380dc26c50a50

Recommended Posts

Note to have multiple values in HashMap
Note No.5 "HashMap value has multiple values using class" [Java]
How to make th: value of select have multiple values
Things to note in conditional expressions
How to have params in link_to method
How to execute multiple commands in docker-compose.yml
How to define multiple orm.xml in Spring4, JPA2.1
[Rails] How to search by multiple values ​​with LIKE
# 2 [Note] I tried to calculate multiplication tables in Java.
[Java] Things to note about type inference extended in Java 10
8 Corresponds to multiple arguments
To debug in eclipse
Initializing HashMap in Java
Connect to multiple MySQL instances with SSL enabled in JDBC
How to get values in real time with TextWatcher (Android)
Establish a search bar in Rails ~ Join multiple tables to search
I want to return multiple return values for the input argument