JavaBeat

  • Home
  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)
  • Privacy

HashMap in Java

January 21, 2014 by Krishna Srinivasan Leave a Comment

HashMap uses the hash table to implement the Map interface. Map is key-value pair where key is the reference and value is the actual data. HashMap is very extensively used in the projects when key-value type data is stored. This doesn’t ensure the order of insertion. This class extends the AbstractMap and implements the Map interface.

HaspMap supports the four types of constructors:

  • HashMap( )
  • HashMap(Map m)
  • HashMap(int capacity)
  • HashMap(int capacity, float fillRatio)

HashMap Methods

  • void clear() –> Clears all entries in this map.
  • Object clone() –> Returns a clone of the map object
  • boolean containsKey(Object key) –> Will return true if the map contains the key
  • boolean containsValue(Object value) –> Will return true if this map maps one or more keys to the specified value.
  • Set entrySet() –> Will return true if a collection of the mappings found this map.
  • Object get(Object key) –> Will return the value to which the passed key is mapped in this identity hash map, or null if the map contains no mapping for the passed key.
  • boolean isEmpty() –> Will return true if this map contains no entries
  • Set keySet() –> Will return the set of keys in the map object
  • Object put(Object key, Object value) –> Will link the key and value passed
  • putAll(Map m) –> Copy all of the amppings to the another map
  • Object remove(Object key) –> Will remove the passed key-value from the map
  • int size() –> Will return the size of the map
  • Collection values() –> Will retrun the collection view of the map values. It may be a list of values

HashMap Example

[code lang=”java”]
package javabeat.net.core;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

public class HashMapExample {
public static void main(String args[]) {

// Create a default hash map
HashMap<String,String> hashmap = new HashMap<String,String>();
// Add elements to the map
hashmap.put("1", "Entry 1");
hashmap.put("2", "Entry 2");
hashmap.put("3", "Entry 3");

// Get a set of the entries
Set<Entry<String, String>> set = hashmap.entrySet();

// Get an iterator
Iterator<Entry<String, String>> i = set.iterator();

// Display elements
while (i.hasNext()) {
Map.Entry<String,String> mapEntry = i.next();
System.out.print(mapEntry.getKey() + ": ");
System.out.println(mapEntry.getValue());
}
}
}

[/code]

The output for the above example will be:

[code]
3: Entry 3
2: Entry 2
1: Entry 1
[/code]

Filed Under: Java Tagged With: Java Basics, Java Collections

About Krishna Srinivasan

He is Founder and Chief Editor of JavaBeat. He has more than 8+ years of experience on developing Web applications. He writes about Spring, DOJO, JSF, Hibernate and many other emerging technologies in this blog.

Leave a Reply Cancel reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Follow Us

  • Facebook
  • Pinterest

As a participant in the Amazon Services LLC Associates Program, this site may earn from qualifying purchases. We may also earn commissions on purchases from other retail websites.

JavaBeat

FEATURED TUTORIALS

Answered: Using Java to Convert Int to String

What is new in Java 6.0 Collections API?

The Java 6.0 Compiler API

Copyright © by JavaBeat · All rights reserved