Programming with Java, part 5

記事
IT・テクノロジー

1.About Map of collection class

Following List, this time I will introduce Map from the utility
 "java.util". You store a combination of key and value in Map
as one data. You use the key if you want to retrieve the
value from the Map.
Map also has several classes, so I will explain about
"HashMap", "TreeMap", and "LinkedHashMap" in this time.


(1)HashMap

This is a standard Map. You cannot register the duplicate
keys. Also, the elements (key and value sets) are not
arranged in the order in which they were registered.
To use it, specify the type as shown below when you new.

For example,
  HashMap<String,String> map = new HashMap<String,String>();
or
  HashMap<Integer,String> map = new HashMap<Integer,String>();
etc. Like HashMap <key type, value type>.

You can also specify the size if you know the number
of elements to be registered in advance. It is as follows.
  HashMap<String,String> map = new HashMap<String,String>(20);
Even if you register an element without specifying the size,
it will be automatically expanded when the number of
registered elements exceeds a certain number, so you can
use it without problems. (If you do not specify the size,
the initial size is 16)

To register the key and value, use the put method as
shown below.
    map.put("JPN", "Japan");
    map.put("USA", "United States");
The first parameter (JPN, etc. this time) is the key, and
the second parameter (Japan, etc. this time) is the value.
Also, here's how to avoid duplicate keys.
    if(!map.containsKey("JPN")) {
        map.put("JPN", "Japan");
    }
In this way, it is also possible to register after checking
with the containsKey method whether the key to be
registered is already registered in HashMap.

To retrieve the value from HashMap, use the get method
as shown below.
    String strCountryName = map.get("JPN");
For the parameter, specify the key of the value you want to get.


(2)TreeMap

TreeMaps are automatically sorted (ascending) by key.
This is useful when you need to put them in order. It is the
same as HashMap that duplicate keys cannot be registered,
and elements are registered / acquired with the put and get
methods. When new, write as follows.
  TreeMap<String,String> map = new TreeMap<String,String>();
or
  TreeMap<Integer,String> map = new TreeMap<Integer,String>();
etc.


(3)LinkedHashMap

LinkedHashMap is arranged in the order in which the
keys are registered. For example, suppose you have
three keys, 「"2", "3", "1"」. If you register in this order,
TreeMap will be automatically sorted as 「"1", "2", "3"」.
However, in the case of LinkedHashMap, they are arranged
according to the registration order of 「"2", "3", "1"」.
When new, write as follows.
LinkedHashMap<String,String> map = new LinkedHashMap<String,String>();
or
LinkedHashMap<Integer,String> map = new LinkedHashMap<Integer,String>();
etc.


2.My recommended usage

Map can also be used in combination with ArrayList. As shown
in the figure below, register the element of String type as the
key and ArrayList type as the value in Map. In this example,
if you specify a 3-digit country code, you can get a list of
prefectures or states of that country.

2.png



[Japanese]

1.コレクションクラスのMapについて

Listに続いて、今回はユーティリティ「java.util」からMapについて紹介します。Mapでは、キーと値を組み合わせたものを1つのデータとして、Mapに格納します。Mapから値を取り出すときは、キーを使います。
Mapもいくつかのクラスがありますので、その中から「HashMap」、
「TreeMap」、「LinkedHashMap」について解説します。


(1)HashMap

スタンダードなMapです。重複するキーは登録できません。また、登録した順番に要素(キーと値のセット)が並んではいません。
使い方は、newするときに
HashMap<String,String> map = new HashMap<String,String>();
HashMap<Integer,String> map = new HashMap<Integer,String>();
のStringやIntegerのように、型を指定します。HashMap<キーの型, 値の型>です。また、
HashMap<String,String> map = new HashMap<String,String>(20);
の(20)のように、予め登録する要素数が分かっていれば、サイズの指定もできます。サイズを指定せずに要素を登録しても、登録した要素が一定の割合を超えると自動で拡張されますので、問題なく利用できます。(サイズを指定しない場合、初期サイズは16です)

キーと値の登録は、
    map.put("JPN", "Japan");
    map.put("USA", "United States");
のように、putメソッドを利用します。第1パラメータ(今回はJPNなど)がキー、第2パラメータ(今回はJapanなど)が値です。
また、重複するキーは登録できないため、
    if(!map.containsKey("JPN")) {
        map.put("JPN", "Japan");
    }
のように、これから登録するキーがHashMapに既に登録されていないかどうかを、containsKeyメソッドで調べた後に登録することも可能です。

HashMapから値を取り出すときは、
    String strCountryName = map.get("JPN");
のように、getメソッドを利用します。パラメータには、取得したい値のキーを指定します。


(2)TreeMap

TreeMapは、キーによって自動的にソート(昇順)されます。順番に並べる必要がある場合に活躍します。重複するキーを登録できないのはHashMapと同じで、putやgetのメソッドで同じように要素の登録・取得をします。
newするときは、
TreeMap<String,String> map = new TreeMap<String,String>();
TreeMap<Integer,String> map = new TreeMap<Integer,String>();
のように記述します。


(3)LinkedHashMap

LinkedHashMapは、キーを登録した順に並びます。例えば、キーが「"2", "3", "1"」の3つあり、この順番に登録した場合、TreeMapの場合は「”1”, "2", "3"」のように自動でソートされます。しかし、LinkedHashMapの場合は、「"2", "3", "1"」の登録順序の通りに並びます。
newするときは、
LinkedHashMap<String,String> map = new LinkedHashMap<String,String>();
LinkedHashMap<Integer,String> map = new LinkedHashMap<Integer,String>();
のように記述します。


2.私がおすすめする使い方

Mapは、ArrayListと組み合わせて使用することも可能です。下図のように、キーはString型、値はArrayList型の要素をMapに登録します。この例では、3桁の国コードを指定すると、その国の都道府県や州のリストが取得できるようになっています。

1.png


サービス数40万件のスキルマーケット、あなたにぴったりのサービスを探す