Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the <K, V> thing in a hashmap, and how do I use it in my own class? [duplicate]

I have been trying to simply get a name for what the <> part of the declaration is called, to no luck. Can anyone tell me what it's called, how I can use it in my own class? For instance I may want to try and make my own kind of collection, and use new MyThing<String> for instance. Any help is appreciated, thanks!

like image 281
user2507230 Avatar asked Jul 21 '13 00:07

user2507230


People also ask

What is K and V in HashMap?

Type Parameters: K - the type of keys maintained by this map V - the type of mapped values All Implemented Interfaces: Serializable, Cloneable, Map<K,V> Direct Known Subclasses: LinkedHashMap, PrinterStateReasons.

Can we have duplicate key in HashMap?

HashMap stores key, value pairs and it does not allow duplicate keys. If the key is duplicate then the old key is replaced with the new value.

How do I find duplicates in a HashMap?

How To Find Duplicate Elements In Array In Java Using HashMap? In this method, We use HashMap to find duplicates in array in java. We store the elements of input array as keys of the HashMap and their occurrences as values of the HashMap. If the value of any key is more than one (>1) then that key is duplicate element.

How HashMap print key and value separately?

You can use the “values()” and “keySet()” methods to print the values and keys of HashMap separately. The values() method returns the entire set of values, whereas the keySet() method returns the entire set of HashMap keys.


1 Answers

That's the generic type declaration of the class. For example, to specify that a map is going to use strings as keys and integers as values:

Map<String, Integer> mymap = new HashMap<String, Integer>();
like image 181
Óscar López Avatar answered Sep 23 '22 10:09

Óscar López