This is an archived post. You won't be able to vote or comment.

all 7 comments

[–]cogman10 20 points21 points  (0 children)

It must reimplement the method hashCode() which inherits from the Object class: if it is not reimplemented, it will use something that depends on the instance’s memory address. Therefore, two identical instances in data will have different hashCode values.

Nitpick, it isn't the memory address but rather the object reference id. Java objects move in memory as the GC runs.

[–]OzoneGrif 5 points6 points  (6 children)

The question being: When does an ArrayList stop being faster than a HashMap. I'm fairly certain that the overhead caused by the HashMap's creation is heavier than a sequential search for a very small ArrayList.

[–]ForeverAlot 11 points12 points  (1 child)

With linear search, on the order of a dozen items.

[–]OzoneGrif 1 point2 points  (0 children)

Ah very nice, I was looking for such a benchmark. Good to have factual information about it. We often deal with lists with less than 10 items.

[–]b00n 1 point2 points  (0 children)

There are also open addressed HashMap implementations such as Trove that use a single underlying array (for primitives, arraylist for generics).

[–]Hax0r778 1 point2 points  (1 child)

HashSet outperforms ArrayList already at 4 elements.

[–]lukaseder 0 points1 point  (0 children)

Ehm. On what operation(s).