List<Integer> result = new ArrayList<>();
for (Integer key : List1) {
for (MyObject obj : List2) {
if (obj.getSecondElement().equals(key)) {
result.add(obj.getFirstElement());
}
}
}
如果List2大于您更好地构建从第二个元素到第一个元素的地图,然后在此地图中进行搜索。
There are many possible approaches to this. The following basic solution has a quadratic time complexity, so it's not good if List2 is large:
List<Integer> result = new ArrayList<>();
for (Integer key : List1) {
for (MyObject obj : List2) {
if (obj.getSecondElement().equals(key)) {
result.add(obj.getFirstElement());
}
}
}
If the List2 is large than you better construct a map from it's second element to the first one first, and then make search in this map.
发布评论
评论(1)
有许多可能的方法。以下基本解决方案具有二次时间复杂性,因此如果List2很大,则不好:
如果List2大于您更好地构建从第二个元素到第一个元素的地图,然后在此地图中进行搜索。
There are many possible approaches to this. The following basic solution has a quadratic time complexity, so it's not good if List2 is large:
If the List2 is large than you better construct a map from it's second element to the first one first, and then make search in this map.