1

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

                    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);
                            }
                        }

                    }
5
  • Can you post your two lists? Commented May 7, 2015 at 14:53
  • If your lists are sorted, you could avoid starting the inner loop from the beginning. Instead start from the last found index. Commented May 7, 2015 at 14:57
  • The Lists are just a custom object that hold a double and a string Commented May 7, 2015 at 14:59
  • You could create Sets from each List and do a retainAll to intersect the two sets. But this assumes you have defined equals and hashCode to be based on your path attribute. Commented May 7, 2015 at 14:59
  • The lists are both sorted by the double value but I want to find the same ones based on the string Commented May 7, 2015 at 15:00

3 Answers 3

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 ;)

Sign up to request clarification or add additional context in comments.

Comments

2

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.

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
  }
}

Comments

1

You should use a Map<String,P> and record all of the Ps in it in one pass.

    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.

    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);
        }
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.