私はJava 8のリストストリーミングメソッドに変換したい非常に単純なコードスニペットを持っています。どんな助けでも本当に感謝しています。Java 8:Compare 2コレクションを別のプロパティを使用して別のコレクションを作成する
私は2つのコレクションを持っています.1つは文字列のセットで、もう1つは単純なPOJOのリストで、それ自体が文字列のリストで構成されています。
POJOサンプルIは、一度オブジェクトのリストの上に行くと、ちょうど名前がであるかどうかをチェックしませなぜ
Set<String> someStrings // Contains a list of strings that needs to be compared
List<SimplePojo> pojoObjectList // Contains list of SimplePojo objects
/* Each of someStrings need to be compared to each of SimplePojo.name property
and corresponding actions which is List object needs to be populated as a separate list
Below is the code snippet which has a mixture of Java 8 stream and regular For-Each loop
*/
Set<String> desiredStrings = new HashSet<String>();
for(String s : someStrings) {
List<String> interimDesiredStrings = pojoObjectList.stream()
.filter(o -> StringUtils.equals(s, o.getName()))
.flatMap(o -> o.getDesiredStringList().stream())
.collect(Collectors.toList());
desiredStrings.addAll(interimDesiredStrings);
}
はい私はApache Lang3のStringUtilsを使っていました。述べているので、エラーを指摘してくれてありがとう。 他の人は同じ/類似のソリューションを提供していますが、私はこれを正しい答えとして受け入れて、他の人が利益を得ることができるように説明します。 ありがとうございました!!! – Sayantan