Java Collections

Map interface in Collection

The Map interface is not an extension of Collection interface. Instead the interface starts of it’s own interface hierarchy, for maintaining key-value associations. The interface describes a mapping from keys to values, without duplicate keys, by definition.

The Map interface provides three collection views, which allow a map’s contents to be viewed as a set of keys, collection of values, or set of key-value mappings. The order of a map is defined as the order in which the iterators on the map’s collection views return their elements. Some map implementations, like the TreeMap class, make specific guarantees as to their order; others, like the HashMap class, do not. 
 

 void
clear()
Removes all mappings from this map (optional operation).
 boolean
containsKey(Object key)
Returns true if this map contains a mapping for the specified key.
 boolean
containsValue(Object value)
Returns true if this map maps one or more keys to the specified value.
 Set
entrySet()
Returns a set view of the mappings contained in this map.
 boolean
equals(Object o)
Compares the specified object with this map for equality.
 Object
get(Object key)
Returns the value to which this map maps the specified key.
 int
hashCode()
Returns the hash code value for this map.
 boolean
isEmpty()
Returns true if this map contains no key-value mappings.
 Set
keySet()
Returns a set view of the keys contained in this map.
 Object
put(Object key, Object value)
Associates the specified value with the specified key in this map (optional operation).
 void
putAll(Map t)
Copies all of the mappings from the specified map to this map (optional operation).
 Object
remove(Object key)
Removes the mapping for this key from this map if it is present (optional operation).
 int
size()
Returns the number of key-value mappings in this map.
 Collection
values()
Returns a collection view of the values contained in this map.

The interface methods can be broken down into three sets of operations: altering, querying and providing alternative views

The alteration operation allows you to add and remove key-value pairs from the map. Both the key and value can be null. However you should not add a Map to itself as a key or value.
Object put(Object key, Object value)
Object remove(Object key)
void putAll(Map t)
void clear()

The query operations allow you to check on the contents of the map
Object get(Object key)
boolean containsKey(Object key)
boolean containsValue(Object value)
int size()
boolean isEmpty()

The set methods allow you to work with the group ofkeys or values as a collection
Set keySet()
Collection values()
Set entrySet()

<<Previous <<   || Index ||   >>Next >>

Previous
Next
Dinesh Rajput

Dinesh Rajput is the chief editor of a website Dineshonjava, a technical blog dedicated to the Spring and Java technologies. It has a series of articles related to Java technologies. Dinesh has been a Spring enthusiast since 2008 and is a Pivotal Certified Spring Professional, an author of a book Spring 5 Design Pattern, and a blogger. He has more than 10 years of experience with different aspects of Spring and Java design and development. His core expertise lies in the latest version of Spring Framework, Spring Boot, Spring Security, creating REST APIs, Microservice Architecture, Reactive Pattern, Spring AOP, Design Patterns, Struts, Hibernate, Web Services, Spring Batch, Cassandra, MongoDB, and Web Application Design and Architecture. He is currently working as a technology manager at a leading product and web development company. He worked as a developer and tech lead at the Bennett, Coleman & Co. Ltd and was the first developer in his previous company, Paytm. Dinesh is passionate about the latest Java technologies and loves to write technical blogs related to it. He is a very active member of the Java and Spring community on different forums. When it comes to the Spring Framework and Java, Dinesh tops the list!

Share
Published by
Dinesh Rajput

Recent Posts

Strategy Design Patterns using Lambda

Strategy Design Patterns We can easily create a strategy design pattern using lambda. To implement…

2 years ago

Decorator Pattern using Lambda

Decorator Pattern A decorator pattern allows a user to add new functionality to an existing…

2 years ago

Delegating pattern using lambda

Delegating pattern In software engineering, the delegation pattern is an object-oriented design pattern that allows…

2 years ago

Spring Vs Django- Know The Difference Between The Two

Technology has emerged a lot in the last decade, and now we have artificial intelligence;…

2 years ago

TOP 20 MongoDB INTERVIEW QUESTIONS 2022

Managing a database is becoming increasingly complex now due to the vast amount of data…

2 years ago

Scheduler @Scheduled Annotation Spring Boot

Overview In this article, we will explore Spring Scheduler how we could use it by…

2 years ago