JavaAntiPatterns

Collection of bad coding practices

Posts Tagged ‘url

Comparing URLs with ‘URL.equals()’

leave a comment »

Implementation of equals() in java.net.URL is based on a fancy rule saying that URLs of two hosts are equal as long as they are resolving to the same IP address. It is known to be incompatible with virtual hosting and should not be used.

For instance, URL.equals() would consider the URL of this blog (https://javaantipatterns.wordpress.com) to be equal to an URL of any random blog hosted on WordPress – that is obviously not true.

It is recommended to use java.net.URI objects instead.

P.S.
java.net.URL class is an absolute champion in a number of implemented antipatterns of equals() (and hashCode() as well):

Advertisements

Written by Alex

November 24, 2007 at 6:53 am

Posted in Networking, Objects

Tagged with ,

Using URLs in Collections

with one comment

java.net.URL objects used as Map keys or Set items can result in a big performance issue. Internal implementation of equals() and hashCode() methods of java.net.URL class performs domain name resolution, so the time of their execution relies on an internet connection speed. If a computer is off-line, the time is nearly equal to a connection timeout (tens of seconds).

The same problem arises every time when URLs equality checking or hash code computation is performed.

Do not use java.net.URL to keep URL values. Use java.net.URI objects or the simple Strings instead.

Read the rest of this entry »

Written by Alex

November 22, 2007 at 12:25 pm

Posted in Collections

Tagged with , , , , , ,