コミュ障だから明日が僕らをよんだって返事もろくにしなかった

何かを創る人に憧れたからブログをはじめたんだと思うよ

僕の心のジャバイやつ

Javaというプログラミング言語に関しての中身のない話をします。

はい、おはようございます。僕です。今日も中身のない話していこうと思います。

コレの話をですね。
f:id:andron:20201011013724p:plain
前回やってたところの続きを終えて自慢したくなったのでそんな話をします。それだけの話なんで興味ない方はブラウザバック推奨だ!というか上に挙げた画像以上に話すことないんですよね……。

元サイトはこちら
codingbat.com

前回記事はこちら
inujini.hatenablog.com


ということで画像貼って自慢して話すことなくなってしまったので完走した感想でも以下に綴っていきます。

Java Map API

// Make a new empty map
Map<String, String> map = new HashMap<String, String>();
map.get(key); //-- retrieves the stored value for a key, or null if that key is not present in the map.
map.put(key, value); //-- stores a new key/value pair in the map. Overwrites any existing value for that key.
map.containsKey(key); //-- returns true if the key is in the map, false otherwise.
map.remove(key); //-- removes the key/value pair for this key if present. Does nothing if the key is not present.

まずはMAP編の感想でも、上にある仕様例通りMAP APIを駆使してやっていきます。

設問がシンプルなんで下みたいな条件分岐挟むだけで解ける問題多くて楽しかった。

if(map.get(*)){
  map.put(*)
}

次にFunctional編の感想でも。

Java Functional API

list.replaceAll(lambda) -- calls the lambda once for each item in the list, storing the result back into the list (or other type of collection).
e.g.

nums.replaceAll(n -> n * 2);

list.removeIf(lambda) -- calls the lambda once for each item in the collection, removing each item where the lambda returns true.
e.g.

nums.removeIf(n -> n < 0);

Simple Lambda Examples -- the data types are inferred from the context and from the type of the expression following the "->":
n -> n * 2 -- takes Integer, returns Integer
n -> n < 0 && n >= -10 -- takes Integer, returns boolean
s -> s.length() -- takes String, returns Integer
s -> s.startsWith("hi") -- takes String, returns boolean

The Java stream system provides more complicated lambda features. The stream calls do not modify the original list, returning a new data structure of the results. Note that the boolean logic of filter() is the opposite of removeIf().

  List<Integer> nums = -something-;
  nums = nums.stream()
    .map(n -> n * 2)
    .filter(n -> n >= 0)
    .collect(Collectors.toList());

The above are just the most common calls, for more information see the official
java.util.stream (Java Platform SE 8 )

上の例にもあるように下のような書き方できるようになるとめちゃくちゃ楽しく解ける。

nums = nums.stream()
    .map(n-> n * 2)
    .filter(n-> n> = 0)
    .collect(Collectors.toList());


といった感じで時間空いてしまった割りに楽しく解けたのでした。


おわり*1

*1: ※ また更新などあったら目ざとく見つけて解いていこうと思います