简体   繁体   中英

compare path in java

i have those paths stored in arraylist like this :

html/body/div/header/div/div/a/span 
html/body/div/div/div/div/div/div/div/div/div/h2 
html/body/div/div/div/table/tbody/tr/td/ul/li/a/h2
html/body/div/div/div/table/tbody/tr/td/ul/li/a/h1 
html/body/div/div/div/div/table/tbody/tr/td/a/span 
html/body/div/div/div/div/table/tbody/tr/td/a/span/h1
html/body/div/div/div/div/table/tbody/tr/td/a/a/span/h1

i want compare each path with other, and keep at the end only the similar path , result that i want is like this :

html/body/div/div/div/table/tbody/tr/td/ul/li/a
html/body/div/div/div/div/table/tbody/tr/td/a/span

the java code is :

ArrayList<String> list_input = new ArrayList();
    ArrayList<String> list_output = new ArrayList() ;


        list_input.add("");

         list_input.add("html/body/div/header/div/div/a/span");
    list_input.add("html/body/div/div/div/div/div/div/div/div/div/h2");
    list_input.add("html/body/div/div/div/table/tbody/tr/td/ul/li/a/h2");
    list_input.add("html/body/div/div/div/table/tbody/tr/td/ul/li/a/h1");
    list_input.add("html/body/div/div/div/div/table/tbody/tr/td/a/span");
    list_input.add("html/body/div/div/div/div/table/tbody/tr/td/a/span/h1");
    list_input.add("html/body/div/div/div/div/table/tbody/tr/td/a/a/span/h1");

        for(int a=0; a<list_input.size(); a++)
        {
            String chemin = list_input.get(a);
            for(int b=0; b<list_input.size(); b++)
                if(a != b)
                {
                    String chemin_comparé = list_input.get(b);
                    if(chemin_comparé.indexOf(chemin) != -1 && !list_output.contains(chemin))
                        list_output.add(chemin);
                }
        }

        for(String chemin_disp: list_output)
            System.out.println(chemin_disp);

but this code give only this result :

html/body/div/div/div/div/table/tbody/tr/td/a/span

in fact that also

html/body/div/div/div/table/tbody/tr/td/ul/li/a

must be returned but it dosnt ! please help me , and thanks in advance .

Okay, now that you edited here another answer:

First, to get all shared path you have to compare in a loop, cutting of the part after the last slash until there is no slash left:

 for(int a=0; a<list_input.size(); a++)
        {
            for(int b=0; b<list_input.size(); b++) {
                String chemin = list_input.get(a);
                if(a != b)
                {
                    String chemin_comparé = list_input.get(b);
                    while(true) {
                        if(chemin_comparé.indexOf(chemin) != -1 && !list_output.contains(chemin))
                            list_output.add(chemin);
                        if(chemin.contains("/")) {
                            chemin = chemin.substring(0, chemin.lastIndexOf("/"));
                        } else {
                            break;
                        }
                    };
                }
            }
        }

So basicly when comparing the following:

  • html/body/div/div/div/table/tbody/tr/td/ul/li/a/h2
  • html/body/div/div/div/table/tbody/tr/td/ul/li/a/h1

You have to check:

  1. Does

    • html/body/div/div/div/table/tbody/tr/td/ul/li/a/h2 contain
    • html/body/div/div/div/table/tbody/tr/td/ul/li/a/h1
  2. Does

    • html/body/div/div/div/table/tbody/tr/td/ul/li/a/h2 contain
    • html/body/div/div/div/table/tbody/tr/td/ul/li/a
  3. Does

    • html/body/div/div/div/table/tbody/tr/td/ul/li/a/h2 contain
    • html/body/div/div/div/table/tbody/tr/td/ul/li

....

  1. Does
    • html/body/div/div/div/table/tbody/tr/td/ul/li/a/h2 contain
    • html

The Code I posted above does exactly that, allways cutting of the last slash until there is none left.

However, now you actually have ALL path that are equal, including just "html"

since you only want the "longest" equal path, you can simply filter the output list again, removing elements that are substrings / contained in other list-elements:

final ArrayList<String> filteredOutputList = new ArrayList<>();
for (String string1 : list_output) {
    boolean stringIsContainedInOtherString = false;
    for (String string2 : list_output) {
        if(!string2.equals(string1) && string2.contains(string1)) {
                stringIsContainedInOtherString = true;
                break;
        }
    }
    if(!stringIsContainedInOtherString && !filteredOutputList.contains(string1)) {
        filteredOutputList.add(string1);
    }
}

for(String filteredOutput: filteredOutputList)
    System.out.println(filteredOutput);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM