嘿,我有两个类型的对象列表。我想遍历这两个列表并找到具有相同属性的对象,即文件夹路径,以便我可以合并它们都具有的另一个属性。现在,我使用2 for循环执行此操作,并检查内部循环中的匹配项,并且此方法有效。但是我的问题是有没有更有效的方法?谢谢
for(int z = 0; z < pList.size(); z++)
{
for(int c = 0; c < eList.size(); c++)
{
if(pList.get(z).path.equals(eList.get(c).path))
{
Pair rank = new Pair();
rank.k = z + c / 2.0;
rank.path = pList.get(z).path;
pcList.add(rank);
}
}
}
最佳答案
您应该使用一个Map<String,P>
并一次性记录所有P
。
for (int z = 0; z < pList.size(); z++) {
map.put(pList.get(z).path, pList.get(z));
}
然后遍历eList,在地图中查找路径以获取与其关联的pList条目。
for (int c = 0; c < eList.size(); c++) {
P p = map.get(eList.get(c).path);
if ( p != null ) {
Pair rank = new Pair();
rank.k = z + c / 2.0;
rank.path = p.path;
pcList.add(rank);
}
}