Kumar Vaibhav
Kumar Vaibhav

Reputation: 2652

Why can I not create a HashMap with 'long' types in Java?

Any reason that the following is not allowed?

HashMap<long, long> x = new HashMap<>();

Upvotes: 11

Views: 30653

Answers (3)

bedrin
bedrin

Reputation: 4584

Using standard collections for primitive types like long is not really effective

If you need to minimize memory footprint and get better performance you should consider 3rd-party collection libraries like Trove

Upvotes: 5

KeyboardDrummer
KeyboardDrummer

Reputation: 587

In Java, types with generic type parameters, such as HashMap, only accept types that inherit from Object. long does not inherit from Object, so you can't use it with HashMap. You can however use Long, which is a boxed version of long.

Upvotes: 8

Master Slave
Master Slave

Reputation: 28559

you're using primitives rewrite to HashMap<Long,Long> x = new HashMap<>()

Upvotes: 14

Related Questions