randomuser1:
私にはJava
アプリケーション私はクラスを持っています:
public class MyStructure {
SomeClass someClass;
String[] fields;
...
}
今、私は上記の構造体のリストを持っています:
List< MyStructure> structures = getStructures();
私はまた、文字列のリストを持っています:
List<String> myList = getStrings();
私は(最初のリストをフィルタリングする必要がありstructures
、それが唯一の要素が含まれるように、彼らの中にいることを、)fields
アレイ上に文字列の存在のいずれかを含みますmyList
。
私は、forループのようなものを書くことについて考えました:
List<MyStructure> outcomeStructures = new ArrayList<>();
for (MyStructure mystructure : structures) {
for (String temp : mystructure.getFields()) {
if (myList.contains(temp) {
outcomeStructures.add(mystructure);
}
}
}
多分そうする良い方法はありますか?ありがとう!
デッドプール :
パフォーマンスを向上させるために、あなたは、変換することができますList<String> myList = getStrings();
するSet<String> mySet
ので、HashSetの時間の複雑さのためcontains
であるO(1)
常に。次に使用します。
List<MyStructure> outcomeStructures = structures.stream()
.filter(st -> Arrays.stream(st.getFields()).anyMatch(mySet::contains))
.collect(Collectors.toList());