liveBook · Manning

Uri

As explained in 100 Java Mistakes and How to Avoid Them

A URI is a Uniform Resource Identifier that identifies a resource using a standardized syntax and provides component-based equality semantics.

Use java.net.URI when identifying resources without relying on URL.equals() or URL.hashCode(). URL comparison may resolve host names through a network request, causing delays and treating unrelated virtual-hosted sites as equal. section 7.3 section 7.15

Avoid URL-based collection identity

Prefer URI values when resource identifiers will be compared or stored in sets and maps. Do not invoke equals() or hashCode() directly on URL objects. section 7.3

List<URL> urlList = List.of(…);
URL urlToFind = new URL("https://example.com");
if (urlList.contains(urlToFind)) {
  System.out.println("Found");
}

Check list membership carefully: contains() can invoke URL.equals() indirectly even when creating the list does not. section 7.3

How 100 Java Mistakes and How to Avoid Them covers it

How deep it goes
Explains the URL comparison hazard and recommends URI, but does not detail URI syntax or equality implementation.
Examples
Uses URL lists and contains() to expose indirect equality calls, alongside map and set storage cautions.
What it assumes
Assumes familiarity with Java object equality, hashing, and collections; distinguishes direct from indirect equals() usage.
What you can do after
Apply java.net.URI instead of URL when comparing identifiers or storing them in equality-based collections.
How it connects
Connects URI with URL, equals(), hashCode(), collections, virtual hosting, and host-name resolution.

Also explained in

Compare how 9 books explain uri →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of 100 Java Mistakes and How to Avoid Them. The section links point to the parts of the book it draws on.