Name: ssT124754 Date: 04/05/2001
java version "1.3.0_02"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.3.0_02)
Java HotSpot(TM) Client VM (build 1.3.0_02, mixed mode)
Run this code:
import java.io.*;
import java.net.*;
import java.util.*;
public class prova {
private static Set URLSet;
public static void main( String arg[] ) throws InterruptedException {
URLSet = new HashSet();
URL a = null, b = null;
try {
URLSet.add(a = new URL("http://ioi.dsi.unimi.it"));
URLSet.add(b = new URL("http://gongolo.usr.dsi.unimi.it"));
} catch(Exception e) { System.err.println(e); };
System.err.println("Sizeof URLSet :"+URLSet.size());
System.err.println(b.equals(a));
System.err.println(a);
System.err.println(b);
}
}
The two URLs above refer to the same host, but they point to completely
different web pages. This is absolutely common with providers, that have
hundreds of different virtual hosts. According to the W3C specs, an URL
is just the stream of characters, and two URLs are the same URL if and only
if they match character by character. The behaviour of equals() and hashCode()
on URLs should definitely be revised. The host name _IS_ meaningful, as it
can be interpreted, for instance by the HTTP server, to point to completely
different resources. So considering as equal different resources is not
sensible. This is going to give major headaches around.
This report was submitted four months ago, and closed as fixed in JDK 1.3,
but it was not.
So let me be even
more precise. I can do the following:
1) Compile
import java.net.*;
public class Test {
public static void main(String args[]) {
try {
System.out.println((new URL("http://ioi.dsi.unimi.it")).equals(new URL("http://ne.dsi.unimi.it")));
}
catch(Exception e) {}
}
}
2) "java Test" prints "true"
3) "ifdown eth0" (I turn off my ethernet card, and thus my network connection)
4) "java Test" now prints "false"
If I would build now a set with this two elements, it would become incoherent
and all Set methods would work incorrectly as soon as I turned on my ethernet
card (as two equal object would be in a Set).
Does anyone at Sun think this is reasonable? I cannot believe it...
Ciao,
(Review ID: 118791)
======================================================================
- duplicates
-
JDK-4483537 java.net.URL.equals behaviour is wrong for virtual hosts
-
- Closed
-
-
JDK-8366034 URL equality fails for same URL string
-
- Closed
-
-
JDK-4426753 URL insert into MAP as key performance bug
-
- Closed
-
-
JDK-4375334 Takes 20 seconds to get hashCode() of a single URL
-
- Closed
-