Proper way to check for URL equality

Viewed 14009

I have the following scenario:

URL u1 = new URL("http://www.yahoo.com/");
URL u2 = new URL("http://www.yahoo.com");

if (u1.equals(u2)) {
    System.out.println("yes");
}
if (u1.toURI().equals(u2.toURI())) {
    System.out.println("uri equality");
}
if (u1.toExternalForm().equals(u2.toExternalForm())) {
    System.out.println("external form equality");
}
if (u1.toURI().normalize().equals(u2.toURI().normalize())) {
    System.out.println("uri normalized equality");
}

None of these checks are succeeding. Only the path differs: u1 has a path of "/" while u2 has a path of "". Are these URLs pointing to the same resource and is there a way for me to check such a thing without opening a connection? Am I misunderstanding something fundamental about URLs?

EDIT I should state that a non hacky check is desired. Is it reasonable to say that empty path == / ? I was hoping to not have this kind of code

3 Answers
Related