Java: Compare Two TreePath

(2 votes, average: 5.00 out of 5)
 Loading ...

Here is a method I created to compare two TreePath objects if they have the same set of paths from the leaf node up to the root. The code is pretty simple. I first check if they have the same number of path objects. If they do not, then it is considered as not equal. Second, if any of the node label text is not the same from the other, then the method returns false.

1
2
3
4
5
6
7
8
9
10
11
12
13
public static boolean isSameTreePath(TreePath tp1, TreePath tp2) {
    boolean is_it = true;
    if (tp1.getPathCount() != tp2.getPathCount()) is_it = false;
    else {
        for (int i=0; i
            if (!tp1.getPathComponent(i).equals(tp2.getPathComponent(i))) {
                is_it = false;
                break;
            }
        }
    }
    return is_it;
}

Found this post useful? Buy me a cup of coffee or help support the sponsors on the right.

Related Posts with Thumbnails