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.
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.
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.
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);
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.
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