comparing paths in java
<p>i've a set of paths like this : </p>

<pre><code>body/div[2]/div[3]/div/div[1]/div/div[2]/div[2]/ul/li[8]/div/div/div/div[2]/div[2]/div/span[2] 
body/div[2]/div[3]/div/div[1]/div/div[2]/div[2]/ul/li[8]/div/div/div/div[2]/div[2]/a/h2 
body/div[2]/div[3]/div/div[1]/div/div[2]/div[2]/ul/li[8]/div/div/div/div[2]/div[3]/div[1]/div[2]/a/span 
body/div[2]/div[3]/div/div[1]/div/div[2]/div[2]/ul/li[8]/div/div/div/div[2]/div[3]/div[1]/div[4]/div[2]/a/span[1] 
body/div[2]/div[3]/div/div[1]/div/div[2]/div[2]/ul/li[9]/div/div/div/div[2]/div[3]/div[1]/div[8]/a/span 
body/div[2]/div[3]/div/div[2]/div/div[2]/div/div/ul[4]/li[4]/a/span[1]
</code></pre>

<p>this is java code which find similar paths and return it : </p>

<pre><code>List&lt;String&gt; output= new ArrayList&lt;&gt;();
List&lt;String&gt; hh = new ArrayList&lt;&gt;();
output.add("body/div[2]/div[3]/div/div[1]/div/div[2]/div[2]/ul/li[8]/div/div/div/div[2]/div[2]/div/span[2]");
output.add("body/div[2]/div[3]/div/div[1]/div/div[2]/div[2]/ul/li[8]/div/div/div/div[2]/div[2]/a/h2");
output.add("body/div[2]/div[3]/div/div[1]/div/div[2]/div[2]/ul/li[8]/div/div/div/div[2]/div[3]/div[1]/div[2]/a/span");
output.add("body/div[2]/div[3]/div/div[1]/div/div[2]/div[2]/ul/li[8]/div/div/div/div[2]/div[3]/div[1]/div[4]/div[2]/a/span[1]");
output.add("body/div[2]/div[3]/div/div[1]/div/div[2]/div[2]/ul/li[9]/div/div/div/div[2]/div[3]/div[1]/div[8]/a/span");
output.add("body/div[2]/div[3]/div/div[2]/div/div[2]/div/div/ul[4]/li[4]/a/span[1]");
for(int a=0; a&lt;output.size(); a++)
        {
            for(int b=0; b&lt;output.size(); b++) {
                String chemin = output.get(a);
                if(a != b)
                {
                    String chemin_comparé = output.get(b);
                    while(true) {
                        if(chemin_comparé.indexOf(chemin) != -1 &amp;&amp; !output.contains(chemin))
                            hh.add(chemin);
                        if(chemin.contains("/")) {
                            chemin = chemin.substring(0, chemin.lastIndexOf("/"));
                        } else {
                            break;
                        }
                    }
                }
            }
        }

        final ArrayList&lt;String&gt; filteredOutputList = new ArrayList&lt;&gt;();
for (String string1 : hh) {
    boolean stringIsContainedInOtherString = false;
    for (String string2 : hh) {
        if(!string2.equals(string1) &amp;&amp; string2.contains(string1)) {
                stringIsContainedInOtherString = true;
                break;
        }
    }
    if(!stringIsContainedInOtherString &amp;&amp; !filteredOutputList.contains(string1)) {
        filteredOutputList.add(string1);
    }
}

for(String filteredOutput: filteredOutputList)
    System.out.println(filteredOutput);
</code></pre>

<p>and it give me a result like this :</p>

<pre><code>body/div[2]/div[3]/div/div[1]/div/div[2]/div[2]/ul/li[8]/div/div/div/div[2]/div[2]
body/div[2]/div[3]/div/div[1]/div/div[2]/div[2]/ul/li[8]/div/div/div/div[2]/div[3]/div[1]
</code></pre>

<p>which is good but , in my case i want repeat again this function until i'got only one common path by the end result should be like this :</p>

<pre><code>body/div[2]/div[3]/div/div[1]/div/div[2]/div[2]/ul/li[8]/div/div/div/div[2]
</code></pre>