Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

contains function linkedlist

public class state implements Comparator<state>{
        Point a;
        Point b;
        private int path_cost=0;
        ...
}

    class Point {
        int x;
        int y;
        ...
    }

for above i have:

PriorityQueue<state> openNode= new PriorityQueue<state>();
LinkedList<state> closed =new LinkedList<state>();
state currNode;

I need to check if the Point a of ANY openNode or closed equals currNode's Point a.

I could use contains if i had to match the entire object but here i just care about one variabale (Point a) of state class. I want the method to check all the nodes in PriorityQueue and LinkedList.

addition: I am thinking about using Iterator on my priorityQueue and LinkedList. But i am not sure how to read the value of Point a using Iterator.

like image 870
change Avatar asked Nov 16 '25 17:11

change


1 Answers

EDIT: Looked like I'd misunderstood slightly. It's simpler than I thought.

// I've assumed more conventional names
Point currPoint = currNode.getPointA();
for (State openNode : openNodes) {
    if (openNode.getPointA().equals(currPoint)) {
        return true;
    }
}

for (State closedNode : closedNodes) {
    if (closedNode.getPointA().equals(currPoint)) {
        return true;
    }
}
// No matching points
return false;

You could potentially use Guava's Iterables.concat() method to make this slightly simpler:

for (State node : Iterables.concat(closedNodes, openNodes)) {
    if (node.getPointA().equals(currPoint)) {
        return true;
    }
}
return false;

If you need to know which node has an equal point A, just change it to:

for (State node : Iterables.concat(closedNodes, openNodes)) {
    if (node.getPointA().equals(currPoint)) {
        return node;
    }
}
return null;

That will only find one such node, of course - there may be multiple matches.

like image 85
Jon Skeet Avatar answered Nov 19 '25 08:11

Jon Skeet