LoginSignup
16
11

More than 5 years have passed since last update.

MapでStream APIを使う

Posted at

こんな Map<String, String> があります。

Map<String, String> map = new HashMap<String, String>() {
    {
        put("key 1", "value 1");
        put("key 2", "value 2");
        put("key 3", "value 3");
    }
};

これをStream APIで処理するには Map::entrySet を使います。

map.entrySet().stream()
    .map(e -> e.getKey() + ": " + e.getValue())
    .forEach(System.out::println);

出力結果。 

key 3: value 3
key 2: value 2
key 1: value 1
16
11
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
16
11