![]() ![]() V put(K key, V value) : adds the mapping of key-value pair to the map.V get(Object key): returns the value mapped to the given key, if no mapping found then returns null.boolean containsValue(Object value): returns true if there are at least one key mapped to the specified value, otherwise false.boolean isEmpty() : returns true if there are no mappings present, otherwise false.int size() : returns the number of key-value mappings in this Map.Let’s have a look at some of the important Map methods. TreeMap : It is the implementation of Map and SortedMap. LinkedHashMap : It is the implementation of Map. HashMap : It is the implementation of Map, but it doesn't maintain any order. HashMap and LinkedHashMap allow null keys and values, but TreeMap doesn't allow any null key or value.Ī Map can't be traversed, so you need to convert it into Set using keySet() or entrySet() method. There are two interfaces for implementing Map in java: Map and SortedMap, and three classes: HashMap, LinkedHashMap, and TreeMap.Ī Map doesn't allow duplicate keys, but you can have duplicate values. Each class (key) is associated with a list of students (value). Each manager (key) is associated with a list of employees (value) he manages. A map of error codes and their descriptions.The maps are used to perform lookups by keys or when someone wants to retrieve and update elements by keys. Maps are perfect to use for key-value association mapping such as dictionaries. An UnsupportedOperationException is thrown when an attempt is made to change an unmodifiable map.A NullPointerException is thrown if an attempt is made to use a null object and null is not allowed in the map.A ClassCastException is thrown when an object is incompatible with the elements in a map.Several methods throw a NoSuchElementException when no items exist in the invoking map.After the value is stored, you can retrieve it by using its key. Given a key and a value, you can store the value in a Map object.A key is an object that you use to retrieve a value at a later date. The Map interface maps unique keys to values. There are two interfaces for implementing Map in java: Map and SortedMap, and three classes: HashMap, TreeMap and LinkedHashMap. The order of a map depends on specific implementations, e.g TreeMap and LinkedHashMap have predictable order, while HashMap does not. Some implementations allow null key and null value like the HashMap and LinkedHashMap, but some do not like the TreeMap. Therefore it behaves a bit different from the rest of the collection types.įew characteristics of the Map Interface are:Ī Map cannot contain duplicate keys and each key can map to at most one value. The Map interface is not a subtype of the Collection interface. The interface represents a mapping between a key and a value. A Map contains unique keys.Ī Map is useful if you have to search, update or delete elements on the basis of a key. Each key and value pair is known as an entry. A map contains values on the basis of key, i.e. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |