Java

Java

Made by DeepSource

Maps and Sets of URLs can be performance hogs JAVA-S0057

Performance
Critical

This method or field is or uses a Map or Set of URLs. Since both the equals and hashCode method of URL perform domain name resolution, this can result in a big performance hit.

Examples

Problematic Code

HashMap<URL, Integer> hits = new HashMap<>();

// ...

for (HashMap.Entry<URL, Integer> e : hits) {
    // ... This can become very slow for larger hashmaps of URLS.
}

Recommended

Consider using the java.net.URI class to represent URLs. This class does not have the same hashCode behavior, so it is safe to use as a key for map data structures.

HashMap<URI, Integer> hits = new HashMap<>();

// ...

for (HashMap.Entry<URI, Integer> e : hits) {
    // ...
}

References