Topic 2: More fun about sets.
Yes, this topic is also on the surface of sets ...
Program
import java.net.*;
public class UrlSet {
private static final String[] URL_NAMES = {
"http://javapuzzlers.com",
"http://apache2-snort.skybar.dreamhost.com",
"http://www.google.com",
"http://javapuzzlers.com",
"http://findbugs.sourceforge.net",
"http://www.bianceng.cn"
};
public static void main (string[] args)
throws MalformedURLException {
Set<URL> favorites = new HashSet<URL>();
for (String urlName : URL_NAMES)
favorites.add(new URL(urlName));
System.out.println(favorites.size());
}
}
Answer
(a) 4
(b) 5
(c) 6
(d) None of the above
Translator Preface:
Because recently busy, the second episode of the answer is too late, I hope you forgive me.
Another: A line in the program forgets the parentheses,set<url> favorites = new hashset<url> (); I have added, thanks to the reader's careful.
Answers to puzzles, analytical processes, and lessons learned
Answer
(d) None of the above
Analysis process
If the machine running this program is connected to the Internet, the answer will be (a) 4. Why, then? Because the URL interface of the Equals and Hashcode method is completely mistaken.
"Http://javapuzzlers.com" and "http://apache2-snort.skybar.dreamhost.com", these two completely different URLs, resolved into the IP address is exactly the same! (Translator Note: The Web server hosting these two different sites is obviously only one, with a name based shared IP network hosting technology, which is clearly the topic of interest, that is, the so-called virtual Host)
According to the technical documentation for the URL class, two URL objects will be considered equal if the following conditions are met:
Using the same communication protocol, refer to a comparable hosting server, using the same port, same file or part of the same file.
Two hosting servers are considered equal if two names are resolved to the same IP address, or two names cannot be resolved, or two names are null (NULL).
Therefore, if the machine running this program is not connected to the Internet, the result will be 1, all names should not be resolved. The answer to this question therefore is (d) None of the above. Under the different network environment operation, has the different result, this is quite bad.
The Equals method of the URL class does not support virtual Host (VM) functionality. The 93 URL class was added to the Java platform, the basic is not virtual hosting this technology.
Lessons learned
URL class has bugs, do not use URLs, you should use the URI class.
The Equals method of the URI class only makes a comparison of characters. The program is changed to set<uri> favorites = new hashset<uri> (); The result of printing is 5, because there are two "http://javapuzzlers.com".
At the same time, Favorites.add (new URL (urlname)) This line of procedures to be changed to Favorites.add (Uri.create (uriname));
Note that this is a better static factory model than a generic creation, which is also different from the URL of the URI.