このコードスニペットがあります。どのように変換できますか?
機能的なスタイルに?実際、
List<X>
があります
。各Xには
List<V>
が含まれます
。このリストの各Vには
List<M>
がありますパラメータとして。そして、私は
Map<X,Y>
を構築する必要があります
ここで、Yは、オブジェクトXに集約されたすべてのVオブジェクトに格納されているすべてのMオブジェクトの量です。
HashMap<Country, Integer> modelsPerCountryMap = new HashMap<>();
int count;
for (Country country : CountryDataSingleton.getCountryDataCollection()) {
count = 0;
for (CarMaker cm : country.getListOfMakers()) {
count += cm.getModels().size();
}
modelsPerCountryMap.put(country, count);
}
回答 3 件
このようなものはどうですか:
パブリッククラスCountrysFunctionalTest {
private class CarMaker { private final String name; private final List<String> models; public CarMaker(String name, List<String> models) { this.name = name; this.models = models; } public String getName() { return name; } public List<String> getModels() { return models; } } private class Country { private final String name; private final List<CarMaker> makers; public Country(String name, List<CarMaker> makers) { this.name = name; this.makers = makers; } public String getName() { return name; } public List<CarMaker> getMakers() { return makers; } } @Test public void test() throws DeliveryException { List<Country> countries = Arrays.asList(new Country[]{ new Country("Country A", Arrays.asList(new CarMaker[]{ new CarMaker("Maker A", Arrays.asList(new String [] {"model a", "model a1", "model a2"})) })), new Country("Country B", Arrays.asList(new CarMaker[]{ new CarMaker("Maker B", Arrays.asList(new String [] {"model b", "model b1"})) })), new Country("Country C", Arrays.asList(new CarMaker[]{ new CarMaker("Maker C", Arrays.asList(new String [] {"model c", "model c1", "model c2", "model c3"})) })) }); Map<Country, Integer> conuntriesModels = IntStream.range(0, countries.size()).mapToObj(i -> new AbstractMap.SimpleEntry<Country, Integer>(countries.get(i), IntStream.range(0, countries.get(i).getMakers().size()) .map(ix -> countries.get(i).getMakers().get(ix).getModels().size()).sum() ) ).collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)); for (Country next : conuntriesModels.keySet()) { System.out.println(next.getName() + " models -> " + conuntriesModels.get(next)); } }
}
出力:
国Bモデル-> 2
国Aモデル-> 3
国Cモデル-> 4
Javaで機能する場合は、vavr.ioの使用を検討してください。 Vavrは、機能パラダイムを適用するために必要なツールを提供します便利 そして一貫した 仕方。この場合不変のデータ構造。
io.vavr.collection.Stream
の活用 およびio.vavr.collection.HashMap
、データをMap<Country, Integer>
に収集する 次のようになります。Stream.ofAll(CountryDataSingleton.getCountryDataCollection()) .map(country -> Tuple.of(country, country.getListOfMakers().foldLeft(0, (acc, cm) -> acc + cm.getModels().size()))) .foldLeft(HashMap.<Country, Integer>empty(), (acc, entry) -> acc.put(entry));
関連した質問
- Javaクラスで使用されるKotlinのAOP
- マップに値が存在するかどうかを確認し、キーを返すか、例外をスローします
- 1つが列挙型である2つのフィールドでソートする方法は?
- 5つ以上のtxtファイルを1つのファイルにJavaでマージするにはどうすればよいですか?
- グループ化、並べ替え、Javaでグループを並べ替える
- 通常のメソッドよりもラムダを使用するのはなぜですか?
- 「Java8メソッドリファレンス」オブジェクトをストリームに渡すことは可能ですか?
- Functionapply-Methodは、上書きされていない場合、カスタムインターフェイスで何をしますか?
- ストリームJava8を使用してリストJavaで変更する
- 複数のcsvファイルからIDと外部IDを取得する方法
国をストリーミングし、それらをマップに収集します。キーは国で、値はメーカーサイズの合計ストリームになります。