LoginSignup
0
0

More than 3 years have passed since last update.

JAVA(Map)

Posted at

【Map】
キー(値につける名前)に対してキーに紐づく値を保持することができる。

Mapはインターフェースなので、インスタンスを生成するために実装クラスを使用する必要がある。
今回はよく使用されるHashMapクラスを使用。
初期化を以下のようにして行う。
Map<キーの型名, 値の型名> オブジェクト名 = new HashMap<>();

Map<String, String> map1 = new HashMap<String, String>();

次に「put」を使用して値を追加する。

map1.put("春", "サクラ");
map1.put("夏", "ヒマワリ");
map1.put("秋", "キンモクセイ");
map1.put("冬", "シクラメン");

「get」を使用して値を出力すると以下のようになる。

String name1 = map1.get("冬");
System.out.println(name1);

[シクラメン]
0
0
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
0