Showing posts with label Hashmap. Show all posts
Showing posts with label Hashmap. Show all posts

Jul 12, 2020

Hashmaps in JAVA | Java Language | Coding Winds

HASHMAP

Hey guys, today we are back with another blog and we are going to talk about Hashmaps in java in this one. So hope you’ll like this blog and do let us know your reviews in the comments below.

We have already learnt about arraylist  which can be used to store desired number of values inside it and  hashmaps are also quite similar to arraylists but unlike arraylists ,hashmaps can store values inside it with a key value, i.e., hashmaps can store values of same or different types and the first value can be the reference value or the key value for the second value. Now let’s see the syntax of how to create a hashmap object in java and its implementation.

Firstly we have to import the hashmaps class inside our code:

import java.util.HashMap;

After importing our class we will create an hashmap object.

Syntax:

HashMap <Type, Type> objectName = new HashMap<Type,Type>();

And after creating an object we can use the put(), method to insert values inside our Hashmap for each pair of values.

Ex:

import java.util.HashMap;

 

public class Coding_Winds {

            public static void main(String[] args) {

                       

                         HashMap <String, Integer> marks = new HashMap<>();

                         marks.put("Paul", 98);

                         marks.put("Alicia", 89);

                         marks.put("Morty", 85);

                         marks.put("Anna", 79);

                         

                         System.out.println(marks);

            }

}

 Output: {Alicia=89, Morty=85, Paul=98, Anna=79}

 

Note that, like arraylist in hashmaps also we have to write the full name of the types (starting with the capital letter) of our pair values when we declare them inside the angular brackets i.e., ‘Character’ for char types, ‘Double’ for double, ‘Boolean’ for boolean, etc.

Now like arraylists, hashmaps also have a lot of functions to manipulate a hashmap.

i) keyset() & values():

 These two methods are used to access the key value( or the first value) and the main value (or the second value) of a pair respectively.

Ex:

import java.util.HashMap;

 

public class Coding_Winds {

            public static void main(String[] args) {

                       

                         HashMap <String, Integer> marks = new HashMap<>();

                         marks.put("Paul", 98);

                         marks.put("Alicia", 89);

                         marks.put("Morty", 85);

                         marks.put("Anna", 79);

                         

                         System.out.println(marks.keySet());

                         System.out.println(marks.values());

            }

}

 

 Output: [Alicia, Morty, Paul, Anna]

            [89, 85, 98, 79]

 

ii) get():

This method simply gets us the main value if we pass the key value of any pair through the get() method.

Ex:

import java.util.HashMap;

 

public class Coding_Winds {

            public static void main(String[] args) {

                       

                         HashMap <String, Integer> marks = new HashMap<>();

                         marks.put("Paul", 98);

                         marks.put("Alicia", 89);

                         marks.put("Morty", 85);

                         marks.put("Anna", 79);

                       

           

                         System.out.println(marks.get("Paul"));

            }

}

Output : 98

Some of the other methods for hashmaps (remove(), size(), clear()) have exact same functionalities as it is for an arraylist, but here both the values are affected if any method is implemented.

 


Hope you are clear on this  topic do read our more articles on JAVA LANGUAGE.

If you still have any doubt on this topic then do come to us via email "sophomoretechs@gmail.com" or via Instagram "@coding.winds".


This article is SUBMITTED By : Pranjal Rai


Do subscribe to our daily blog update by clicking here.


Thank You!