[JAVA] How to use Map

LinkedHashMap if you want to retrieve the values in the order they are inserted into the Map TreeMap if you want to retrieve in the order of Key HashMap if you are not particular about it

I remembered.

MapTest


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

public class Main {
    public static void main(String[] args) throws Exception {
        new MapTester(new LinkedHashMap<String, String>()).test();
        new MapTester(new HashMap<String, String>()).test();
        new MapTester(new TreeMap<String, String>()).test();
    }
}

class MapTester {
    private Map<String, String> map;

    public MapTester(Map<String, String> map) {
        this.map = map;
    }
    
    public void test() {
        this.putValues();
        this.dump();
    }
    
    private void putValues() {
        this.map.put("4", "Geryos");
        this.map.put("1", "Airou");
        this.map.put("5", "Rio Leia");
        this.map.put("2", "Yan Cook");
        this.map.put("3", "Dos Jaggi");
        this.map.put("6", "Rio Leus");
    }
    
    private void dump() {
        System.out.println(this.map.getClass().getName() + "Contents");
        for (Map.Entry<String, String> entry : this.map.entrySet()) {
            System.out.println(entry.getKey() + ":[" + entry.getValue() + "]");
        }
        System.out.println("");
    }
}

Execution result


java.util.Contents of LinkedHashMap
4:[Geryos]
1:[Airou]
5:[Rio Leia]
2:[Yan Cook]
3:[Dos Jaggi]
6:[Rio Leus]

java.util.Contents of HashMap
1:[Airou]
2:[Yan Cook]
3:[Dos Jaggi]
4:[Geryos]
5:[Rio Leia]
6:[Rio Leus]

java.util.Contents of TreeMap
1:[Airou]
2:[Yan Cook]
3:[Dos Jaggi]
4:[Geryos]
5:[Rio Leia]
6:[Rio Leus]

Recommended Posts

How to use Map
How to use map
How to use Map
[Java] How to use Map
How to use Java Map
How to use with_option
How to use fields_for
How to use java.util.logging
How to use collection_select
How to use Twitter4J
How to use active_hash! !!
How to use MapStruct
How to use TreeSet
[How to use label]
How to use hashes
How to use JUnit 5
How to use org.immutables
How to use java.util.stream.Collector
How to use VisualVM
[Rails] How to use the map method
How to use Chain API
How to use Priority Queuing
[Rails] How to use enum
How to use java Optional
How to use JUnit (beginner)
How to use Ruby return
[Rails] How to use enum
How to use @Builder (Lombok)
[Swift] How to use UserDefaults
How to use java class
How to use Swift UIScrollView
How to use Big Decimal
How to use String [] args
[Java] How to use string.format
How to use rails join
Ruby: How to use cookies
How to use dependent :: destroy
How to use Eclipse Debug_Shell
[Rails] How to use validation
How to use Java variables
[Rails] How to use authenticate_user!
[Rails] How to use "kaminari"
How to use GC Viewer
[Java] How to use Optional ①
How to use Lombok now
[Creating] How to use JUnit
[Rails] How to use Scope
How to use the link_to method
How to use arrays (personal memorandum)
How to use scope (JSP & Servlet)
How to use the include? method
[Rails] How to use devise (Note)
How to use the form_with method
How to use EventBus3 and ThreadMode
How to use Spring Data JDBC
How to use binding.pry [53 days left]
How to use Java HttpClient (Post)
[Java] How to use join method
How to use Ruby on Rails
How to use equality and equality (how to use equals)
How to use setDefaultCloseOperation () of JFrame