ただしbyte型、てめーはダメだ

2020/12/03

投げっぱなし

Googleマップの飲食店の「お手頃」って誰が書いてるんだろう。ごきげんよう。

C#のコードをJavaに移植する必要があって調べてたんですが。
ここまで同等に書けるのかと驚きました。
Java 8のStream APIでLinq的なことが可能になっていたようです。
comparing()内でキャストしてるのだけが少し格好悪いですが。

C#


using System.Collections.Generic;
using System.Linq;

    if (dictionary.Count > 0)
    {
        firstKey = dictionary
            .Where(keyValuePair => keyValuePair.Value.isA())
            .OrderBy(keyValuePair => keyValuePair.Value.getB())
            .ThenBy(keyValuePair => keyValuePair.Value.getC())
            .First().Key;
    }

Java


import java.util.Comparator;
import java.util.HashMap;
import java.util.Map;
import java.util.stream.Collectors;

    if (hashMap.size() > 0) {
        firstKey = hashMap.entrySet().stream()
            .filter(entry -> entry.getValue().isA())
            .sorted(
                Comparator.comparing(entry -> ((Map.Entry<TKey, TValue>)entry).getValue().getB())
                .thenComparing(entry -> ((Map.Entry<TKey, TValue>)entry).getValue().getC())
            ).collect(Collectors.toList()).get(0).getKey();
    }

ソーシャル/購読

このブログを検索

コメント

ブログ アーカイブ