Coding Raptor

Programmers' Nest

You are here: Home / java / core-java / Java Map

January 23, 2017

Java Map

A Java Map is an aggregation of values, each of which is associated with exactly one key. The distinguishing feature of a Java Map is that keys are unique and every key is associated with a value. The same value may be associated with two keys but two values can’t have the same key. A key maps to exactly one object. This object may itself be a composite such as a Collection or a String. In Java lingo a pair of key and value is commonly referred to as Entry.

When to Use Java Maps

Java Maps specialize in searching. Anytime you have to implement/use a data structure that is optimized for searching, you should go for Maps.

Java Maps are based on the Table abstract data type (ADT). A Table data structure is designed to optimally store and retrieve a key value pair. Since searching/retrieving is such a fundamental task in computer science, almost all computer languages (except for minimalist languages such as C) provide some equivalent of Maps. Maps are sometimes also referred to as Table or Dictionary in common parlance.

Java Maps in Relation to Collection

Java Maps are not strictly a Collection in the sense that they don’t implement the Collection interface. Still, Map interface provides three collection-views of itself. These collection-views allow you to view the keys, values and entries as Collections.

The three collection-view methods of Map are keySet, entrySet and values.  These methods are called collection-view methods because they –

  1. Return a Collection of either key, value or entry.
  2. Are backed by the Map, meaning any change made to the underlying Map is reflected in the collections returned by these methods. If the Map is changed while the Collection returned by these methods is being iterated, the results are JDK dependent (or in other words undefined /unspecified in the specs).

These methods are discussed in more details in the next section.

Important Methods of Java Maps

The below list summarizes important methods in Java Maps –

  1. V get(Object key) -> is the most important method of Map interface. Is used to retrieve/search the Object key in the Map. Returns the value associated with key.
  2. boolean containsKey(Object key) –> another search method of Map interface. Returns true or false based on whether key exists in the Map or not.
  3. V put(K key, V value) -> This method inserts a key, value pair in a Map.
  4. void putAll(Map map) -> Is used to put all key, value pairs of one Map into another.
  5. Object remove(Object key) -> Deletes an entry (both key and value) identified by key from a Map. 
  6. Set<K> keySet() -> Returns all keys in a Map stored as a Set.
  7. Set<Map.Entry<K,V>> entrySet() -> Returns all entries (i.e. key value pairs) of a Map as a Set
  8. Collection<V> values() -> Returns a Collection of all the values stored in the Map.

Constructors in Java Map

A constructor is not part of an interface. Therefore, Java Map interface does not impose any restriction on the constructors a concrete implementation must have. Nevertheless, by convention all concrete implementations of Map must have two constructors –

  1. No argument constructor – creates an empty Map
  2. Copy constructor – takes Map as an argument and creates another Map that has all the elements contained in the passed argument.

Note that there are no compile-time checks to enforce the existences of these constructors but all concrete implementations follow this convention. So, if you roll out your own custom implementation be sure to provide these two constructors. Old-timers expect these constructors to be present.

Map.Entry Interface

If you plan to work with Java Maps for any significant length of time you should also familiarize yourself with a static sub-interface of Map – Entry. The static Entry interface is defined inside the Map interface and represents a key value pair.

Map.Entry has three important methods –

  1. K getKey()-> Retrieves the key of a particular entry
  2. V getValue()-> Retrieves the value of a particular entry
  3. V setValue(V v) -> Replaces the value of this particular entry with the new value.

Having looked at the Map interface, we will now turn to the popular concrete implementations such as HashMap, TreeMap etc.

We are social

Spread the word
Facebooktwittergoogle_plusredditpinterestlinkedintumblrmailFacebooktwittergoogle_plusredditpinterestlinkedintumblrmail

Follow CodingRaptor
Facebooktwittergoogle_plusrssFacebooktwittergoogle_plusrss
Post Views: 51

Share with your friends on other avenues:

  • Twitter
  • Facebook
  • Google
  • Reddit
  • Pinterest
  • Tumblr
  • Print
  • LinkedIn
  • Email
  • More
  • Pocket
  • Skype

Related

Article by P T / core-java, java / collections, Dictionary, HashMap, Hashtable, Map, Map Interface Leave a Comment

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Also Check These Out!

  • Factors in R
  • Basic Data Structures V: Arrays in R
  • Basic Data Structures IV: Lists in R
  • Basic Data Structures III: Data Frames in R
  • Basic Data Structures II: Matrices in R
  • Basic Data Structures I: Vectors in R
  • Atomic or Basic Data Types and Scalars in R
  • Getting Started with R: Packages, Help & Workspace (Part 2)
  • Getting Started with R: Installing and Running R (Part 1)
  • Floating-Point in Java: Representation, Comparison, Equality & A Few Shockers
  • Java Ternary Operator a.k.a Conditional Operator
  • Arrays In JavaScript
  • Coding Raptor Turns 1
  • Java EnumMap with Example
  • LinkedHashMap Example: LRU Cache Implementation
  • LinkedHashMap with Example
  • Java TreeMap with Example
  • Java Collections: Java HashMap Internals
  • Java HashMap with Example
  • Java Map
  • Java Collections: EnumSet with Example
  • Java Collections: Problem Solving with Java Sets
  • Java Collections: Differences between TreeSet, LinkedHashSet and HashSet
  • Java Collections: Sets
  • Using Lists in Java
  • Java Collections: Using Lists
  • Java Collections: Interfaces
  • Java Collections: General Overview
  • Debugging in Netbeans
  • Learn Basics of Core Java in 49 Lessons
  • Varargs in Java
  • Exceptions in Java VIII: Creating Custom Exceptions
  • Exceptions in Java VII: throw and throws in Java
  • Exceptions in Java VI: Types and Hierarchy
  • Exceptions in Java V: try with Resources
  • Exceptions in Java IV: Summary of Rules for try-catch-finally
  • Exceptions in Java iii: Control Flow and Multiple try, catch, finally
  • Exceptions in Java II: try, catch, finally
  • Exceptions in Java I: Introduction
  • Strings in Java VI: More String Methods
  • String in Java V: The intern Method
  • String in Java IV: Important String Methods
  • Strings in Java III: StringBuilder and StringBuffer
  • Strings in Java II: Immutability
  • Strings in Java I: What is a String?
  • Arrays in Java V: Iteration
  • Arrays in Java IV: Manipulating Individual Elements
  • Arrays in Java III: Multidimensional Arrays
  • Arrays in Java II: Declaring, Initializing, Instantiating Arrays
  • Arrays in Java I: What is an Array?
  • Using Environment Variables as Inputs
  • Command Line Arguments in Java
  • static import in Java
  • The import Statement in Java
  • Packages in Java
  • Non Access Modifiers in Java III: abstract, transient, volatile, native
  • Non Access Modifiers in Java II: final, synchronized
  • Non Access Modifiers in Java I: static and strictfp
  • Access Modifiers in Java
  • Looping Construct: for Loop
  • Looping Construct: while and do-while
  • Statements, Expressions and Code Blocks
  • Operators in Java
  • Conditional Statements in Java
  • Reference Data Types
  • Literals in Java
  • Primitive Data Types in Java
  • Variables in Java
  • Compiling and Running Java from Command Line
  • First Java Program: Hello World!
  • Java IDEs and Editors III: Sublime Text and Editplus
  • Java IDEs and Editors II: Eclipse IDE
  • Java IDEs and Editors I: Netbeans
  • Setting up Java Development Environment
  • Java’s Program Execution Model and WORA: Compilation & Interpretation
  • Inside JVM 101: What does JVM do and Memory Areas
  • JVM, JRE, JDK and JIT explained
  • Features of Java and White Paper Buzzwords
  • Beginning Java: History in Brief
Facebooktwittergoogle_plusrssFacebooktwittergoogle_plusrss

Copyright © 2017 · Coding Raptor

loading Cancel
Post was not sent - check your email addresses!
Email check failed, please try again
Sorry, your blog cannot share posts by email.