Skip to content

Custom key classes for HashMap should implement Comparable [SPR-14017] #18589

@spring-projects-issues

Description

@spring-projects-issues

Juergen Hoeller opened SPR-14017 and commented

According to http://www.javaspecialists.eu/archive/Issue235.html...

Since Java 8 it is VERY IMPORTANT to always make your custom keys for HashMap implement Comparable. HashMap now puts your entries into a tree to avoid degradation to O(n) when the entries map to the same bucket. It somehow needs to know the relative order of the keys. If they are strings, then comparing them is trivial. Otherwise, if the key class implements Comparable it uses the compareTo() function. If not, it will eventually use System.identityHashCode() to determine the sort order.


No further details from SPR-14017

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions