Java/J2EE and SOA (349 Blogs) Become a Certified Professional

Understanding Java HashMaps

Last updated on May 22,2019 12.5K Views


Table of Content

What are Java HashMaps?

Java HashMap is a class which is used to perform operations such as inserting, deleting and locating elements in a map. We create a map, where we pass two kinds of values which are ‘key’ and ‘value’.

While using HashMaps, values will be put in HashMap and whenever the user retrieves a value, the key will be used in order to use the value.

The map is an interface that maps keys to the elements. Maps are unsorted and unordered. They allow one null key and multiple null values. The values are stored in key and value. One key or multiple values could be null in the entire HashMap. A key can be any object.

There are several methods available in HashMap

  • Object put(Object key, Object value)
  • Enumeration keys() – it will fetch keys
  • Enumeration elements() – it will fetch elements
  • Object get(Object keys) – pass the key and get the value associated with it
  • Boolean contains key(Object key) – used for checking whether a key is present in HashMap or not
  • Boolean contains Value(Object key) – pass the key
  • Object remove(Object key) – pass the key and remove the object
  • Int size() – for using size
  • String to String() – for converting into string

There are corresponding values for each key where values can be null in the HashMap also.

Creation of HashMap.

HashMap hashmap = new HashMap();

Putting elements

hashmap.put(“Ankita”, 9634.58);

hashmap.put(“Vishal”, 1283.48);

hashmap.put(“Gurinder”, 1478.10);

hashmap.put(“Krishna”, 199.11);

Here, we pass key and the value.

Displaying the value – Get an iterator

Iterator iterator = hashmap.entrySet().iterator();

Here, the values are present in the set so we use entrySet.

Along with the line:

While(iterator.hasNext()){

Map.Entry entry=(Map.Entry) iterator.next();

System.out.print(entry.getKey()+”:”);

System.out.printIn(entry.getValue());

}

Got a question for us? Mention them in the comments section and we will get back to you. 

Related Posts:

Introduction to JAVA/J2EE & SOA

JAVA/J2EE & SOA course

Upcoming Batches For Java Course Online
Course NameDateDetails
Java Course Online

Class Starts on 7th December,2024

7th December

SAT&SUN (Weekend Batch)
View Details
Comments
1 Comment
  • Load factor represents at what level HashMap should increase its own size. For example product of capacity and load factor as (default size) 16 * 0.75 = 12. This represents that after storing the 12th key – value pair into the HashMap , its capacity becomes 32 in Java HashMap

Join the discussion

Browse Categories

webinar REGISTER FOR FREE WEBINAR
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP

Subscribe to our Newsletter, and get personalized recommendations.

image not found!
image not found!

Understanding Java HashMaps

edureka.co