LoginSignup
0
2

More than 5 years have passed since last update.

JAVA Map HashMap

Last updated at Posted at 2015-11-27

Map

2つの情報をキー(key)と値(value)のペアとして格納するデータ構造

get(キー値)
キー値に対応する値を取得(なければnull)

■TestHashMap2.java

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

public class TestHashMap2 {
    public static void main(String[] args) {
        Map<String, String> fish = new HashMap<String, String>();
        fish.put("めばる", "black rockfish");
        fish.put("ぶり", "yellowtail");
        fish.put("たら", "cod");
        fish.put("ふぐ", "globefish");
        fish.put("はりせんぼん", "porcupinefish");
        fish.put("ぶり", "yellowtail");
        fish.put("すずき", "sea bass");
        fish.put("たい", "sea bream");

        System.out.println(fish.get("すずき"));
        System.out.println(fish.get("さんま"));

        System.out.println("値を上書き");
        fish.put("たい", "SEA BREAM");
        System.out.println(fish.get("たい"));

        System.out.println("キーを指定した値を取得");
        String word = fish.get("はりせんぼん");
        System.out.println("はりせんぼんは英語で" + word);

        fish.remove("めばる");
        System.out.println(fish.get("めばる"));

    }
}

■TestHashMap2.java 実行結果
sea bass
null
値を上書き
SEA BREAM
キーを指定した値を取得
はりせんぼんは英語でporcupinefish
null

HashMapの値を1つづつ取り出す

for(キーの型 key : マップ変数 .keySet()){
 値の型 value = マップ変数 .get(key);
System.out.println(key + "の値は" + value);

■PopulationOfState.java

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

public class PopulationOfState {
    public static void main(String[] args) {
        Map<String, Integer> population = new HashMap<String, Integer>();
        population.put("Kentucky", 4339367);
        population.put("Virginia", 8001024);
        population.put("Arkansas", 2937979);
        population.put("North Dakota", 672591);
        population.put("Maine", 17535);
        population.put("Illinois", 12830632);

        for (String key : population.keySet()) {
            Integer value = population.get(key);
            System.out.println("The population of the State of " + key + " is "
                    + value + ".");
        }

    }

}

■PopulationOfState.java 実行結果
The population of the State of Maine is 17535.
The population of the State of Illinois is 12830632.
The population of the State of Kentucky is 4339367.
The population of the State of Virginia is 8001024.
The population of the State of Arkansas is 2937979.
The population of the State of North Dakota is 672591.

LinkedHashMap

格納順に取り出したい場合
■TestHashMap2.java

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

public class PopulationOfState {
    public static void main(String[] args) {
        Map<String, Integer> population = new LinkedHashMap<String, Integer>();
        population.put("Kentucky", 4339367);
        population.put("Virginia", 8001024);
        population.put("Arkansas", 2937979);
        population.put("North Dakota", 672591);
        population.put("Maine", 17535);
        population.put("Illinois", 12830632);

        for (String key : population.keySet()) {
            Integer value = population.get(key);
            System.out.println("The population of the State of " + key + " is "
                    + value + ".");
        }

    }

}

■TestHashMap2.java 実行結果
The population of the State of Kentucky is 4339367.
The population of the State of Virginia is 8001024.
The population of the State of Arkansas is 2937979.
The population of the State of North Dakota is 672591.
The population of the State of Maine is 17535.
The population of the State of Illinois is 12830632.

TreeMap

自然順序で取り出したい場合はTreeMapを使う

■TestHashMap2.java

import java.util.Map;
import java.util.TreeMap;

public class PopulationOfState {
    public static void main(String[] args) {
        Map<String, Integer> population = new TreeMap<String, Integer>();
        population.put("Kentucky", 4339367);
        population.put("Virginia", 8001024);
        population.put("Arkansas", 2937979);
        population.put("North Dakota", 672591);
        population.put("Maine", 17535);
        population.put("Illinois", 12830632);

        for (String key : population.keySet()) {
            Integer value = population.get(key);
            System.out.println("The population of the State of " + key + " is "
                    + value + ".");
        }

    }

}

■TestHashMap2.java 実行結果
The population of the State of Arkansas is 2937979.
The population of the State of Illinois is 12830632.
The population of the State of Kentucky is 4339367.
The population of the State of Maine is 17535.
The population of the State of North Dakota is 672591.
The population of the State of Virginia is 8001024.

HashMap

■TestHashMap2.java 例②

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

public class TestHashMap2 {
    public static void main(String[] args) {
        Map<String, String> fish = new HashMap<String, String>();
        fish.put("めばる", "black rockfish");
        fish.put("ぶり", "yellowtail");
        fish.put("たら", "cod");
        fish.put("ふぐ", "globefish");
        fish.put("はりせんぼん", "porcupinefish");
        fish.put("ぶり", "yellowtail");
        fish.put("すずき", "sea bass");
        fish.put("たい", "sea bream");

        System.out.println("");
        System.out.println("マップに格納された情報を1つづつ取り出す");
        for (String key : fish.keySet()) {
            String value = fish.get(key);
            System.out.println(value);
        }
        System.out.println("");
        System.out.println("Mapの正しい拡張for文");
        for (String key : fish.keySet()) {
            String value = fish.get(key);
            System.out.println(key + "は英語で" + value);
        }

    }
}

■TestHashMap2.java 例② 実行結果

マップに格納された情報を1つづつ取り出す
black rockfish
yellowtail
sea bream
sea bass
porcupinefish
cod
globefish

Mapの正しい拡張for文
めばるは英語でblack rockfish
ぶりは英語でyellowtail
たいは英語でsea bream
すずきは英語でsea bass
はりせんぼんは英語でporcupinefish
たらは英語でcod
ふぐは英語でglobefish

■TestHashMap.java

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

public class TestHashMap {
    public static void main(String[] args) {
        Map<String, String> fish = new HashMap<String, String>();
        fish.put("めばる", "black rockfish");
        fish.put("ぶり", "yellowtail");
        fish.put("たら", "cod");
        fish.put("ふぐ", "globefish");
        fish.put("はりせんぼん", "porcupinefish");
        fish.put("ぶり", "yellowtail");
        fish.put("すずき", "sea bass");
        fish.put("たい", "sea bream");

        System.out.println(fish.get("すずき"));
        System.out.println(fish.get("さんま"));

        System.out.println("値を上書き");
        fish.put("たい", "SEA BREAM");
        System.out.println(fish.get("たい"));

        System.out.println("キーを指定した値を取得");
        String word = fish.get("はりせんぼん");
        System.out.println("はりせんぼんは英語で" + word);

        fish.remove("めばる");
        System.out.println(fish.get("めばる"));

        System.out.println("");
        System.out.println("マップに格納された情報を1つづつ取り出す");
        for (String key : fish.keySet()) {
            String value = fish.get(key);
            System.out.println(value);
        }
        System.out.println("");
        System.out.println("Mapの正しい拡張for文");
        for (String key : fish.keySet()) {
            String value = fish.get(key);
            System.out.println(key + "は英語で" + value);
        }

        System.out.println("");
        System.out.println("拡張for文");
        for (String key : fish.keySet()) {
            System.out.println(key);
        }
        System.out.println("");
        System.out.println("拡張for文②");
        for (String ff : fish.keySet()) {
            System.out.println(ff);
        }

        System.out.println("");
        System.out.println("理解してない");
        System.out.println("Iteratorを使って1つづつ取り出す");
        for (Iterator<String> iterator = fish.values().iterator(); iterator
                .hasNext();) {
            String value = iterator.next();
            System.out.println(value);
        }

    }
}

■TestHashMap.java 実行結果
sea bass
null
値を上書き
SEA BREAM
キーを指定した値を取得
はりせんぼんは英語でporcupinefish
null

マップに格納された情報を1つづつ取り出す
yellowtail
SEA BREAM
sea bass
porcupinefish
cod
globefish

Mapの正しい拡張for文
ぶりは英語でyellowtail
たいは英語でSEA BREAM
すずきは英語でsea bass
はりせんぼんは英語でporcupinefish
たらは英語でcod
ふぐは英語でglobefish

拡張for文
ぶり
たい
すずき
はりせんぼん
たら
ふぐ

拡張for文②
ぶり
たい
すずき
はりせんぼん
たら
ふぐ

理解してない
Iteratorを使って1つづつ取り出す
yellowtail
SEA BREAM
sea bass
porcupinefish
cod
globefish

0
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
2