Hey I have two lists of type object. I want to loop through both these lists and find the objects that have the same attribute i.e folder path so that I can combine another attribute they both have. Right now Im doing this with 2 for loops and checking for the match in the inner loop and this works. But my question is is there a more efficient way? Thanks
嘿,我有两个类型对象列表。我想遍历这两个列表并找到具有相同属性的对象,即文件夹路径,以便我可以组合它们都具有的另一个属性。现在,我正在使用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);
}
}
}
3 个解决方案
#1
You should use a Map<String,P>
and record all of the P
s in it in one pass.
您应该使用Map
for (int z = 0; z < pList.size(); z++) {
map.put(pList.get(z).path, pList.get(z));
}
Then run through your eList, looking up the path in the map to get the pList entry associated with it.
然后运行您的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);
}
}
#2
I dont think there is another way if you want to check every element
如果你想检查每个元素,我不认为还有另一种方法
BTW, you should change your naming convention so that we understand what your lists contain.
顺便说一下,您应该更改命名约定,以便我们了解您的列表包含的内容。
Clean code book is your friend ;)
干净的密码本是你的朋友;)
#3
Best option would be to loop through first list, and put all paths inside a Set. Then when iterating over second list, just check if you have this path in your set.
最好的选择是遍历第一个列表,并将所有路径放在一个Set中。然后,当迭代第二个列表时,只需检查您的集合中是否有此路径。
You should reduce number of operation drasticly. Your code should look something like this:
你应该大幅减少操作次数。您的代码应如下所示:
Set<Path> paths = pList.stream().map(o - > o.path).collect(Collectors.asSet());
for(T obj : eList){
if(paths.contains(obj.path)){
... // rest of your code
}
}
#1
You should use a Map<String,P>
and record all of the P
s in it in one pass.
您应该使用Map
for (int z = 0; z < pList.size(); z++) {
map.put(pList.get(z).path, pList.get(z));
}
Then run through your eList, looking up the path in the map to get the pList entry associated with it.
然后运行您的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);
}
}
#2
I dont think there is another way if you want to check every element
如果你想检查每个元素,我不认为还有另一种方法
BTW, you should change your naming convention so that we understand what your lists contain.
顺便说一下,您应该更改命名约定,以便我们了解您的列表包含的内容。
Clean code book is your friend ;)
干净的密码本是你的朋友;)
#3
Best option would be to loop through first list, and put all paths inside a Set. Then when iterating over second list, just check if you have this path in your set.
最好的选择是遍历第一个列表,并将所有路径放在一个Set中。然后,当迭代第二个列表时,只需检查您的集合中是否有此路径。
You should reduce number of operation drasticly. Your code should look something like this:
你应该大幅减少操作次数。您的代码应如下所示:
Set<Path> paths = pList.stream().map(o - > o.path).collect(Collectors.asSet());
for(T obj : eList){
if(paths.contains(obj.path)){
... // rest of your code
}
}