Nisalon
Nisalon

Reputation: 343

Add to hashmap takes a long time

I've stuck by a few lines in my java program, which take too much time (about 20s), and it seems weird to me.

Here are the lines

 Map<URL, Integer> res2 = new HashMap<>();
 for (URL url : res) {
     res2.put(url, null);
 }

Which res defined as following :

List<URL> res = new ArrayList<>();

In my program, res.size() ~= 1500

Do you have any idea of where my problem could come from ?

Thanks !

Upvotes: 7

Views: 645

Answers (1)

Barend
Barend

Reputation: 17444

The hashCode() method of java.net.URL performs DNS resolution. The URL class is unsuitable for use in a HashSet or as keys in a HashMap. Use either Strings or java.net.URI.

Here's some background:

Upvotes: 15

Related Questions