Java Puzzle Selection-II

Source: Internet
Author: User
Tags hosting server hosting

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.