Traversing the map

Why traversing the map is tough ...?

Oooh....
Is it really tough...?
I am gonna tell you a trick...
Just remember...😁

Map can't be traversed directly...
First map will be converted to the set ...
Then set can be traversed using 
Foreach ,for ,while....etc

So the thing is how will we convert it to set...

And which set...



Ways...
-----------------
Source :

https://www.google.com/amp/s/www.geeksforgeeks.org/iterate-map-java/amp/




Using foreach and entrySet()
-------------------

For(Map.Entry< Integer,Integer>  entry : map.entrySet())
{
System.out.println("Key = " +entry.getKey() +", Value= "+ entry.getValue());
}


Explanation :

Here...
In line one... In map.entrySet()...
EntrySet() is a method returns the set view of the map...when we are applying it on map  

I.e.  map.entrySet()

The syntax of entrySet() is ...
Public Set<Map.Entry<K,V>> entrySet()

Now entrySet return a set of type...
Set<Map.Entry<K,V>>

So in the first line the map.entrySet() returns 
Map.Entry< Integer,Integer>
Which is a set view of the Map and stored in the variable "entry" which is a set of type <Map.Entry< Integer,Integer>>


In second line..from the set variable "entry"...
We are getting each key one by one by calling getKey() on "entry" then...
We are printing the keys one by one...

Same procedure for values...



Examples

(Copied from https://www.geeksforgeeks.org/iterate-map-java/  )
----------------------------------------

Iterating Map In Java :


There are generally five ways of iterating over a Map in Java. In this article, we will discuss all of them and also look at their advantages and disadvantages.
First of all, we cannot iterate a Map directly using iterators, because Map are not Collection. Also before going further, you must know a little-bit about Map.Entry<K, V> interface.
Since all maps in Java implement Map interface, following techniques will work for any map implementation (HashMapTreeMapLinkedHashMapHashtable, etc.)
  1. Iterating over Map.entrySet() using For-Each loop :
    Map.entrySet() method returns a collection-view(Set<Map.Entry<K, V>>) of the mappings contained in this map. So we can iterate over key-value pair using getKey() and getValue() methods of Map.Entry<K, V>. This method is most common and should be used if you need both map keys and values in the loop. Below is the java program to demonstrate it.
    filter_none
    edit
    play_arrow
    brightness_4
    // Java program to demonstrate iteration over 
    // Map.entrySet() entries using for-each loop
      
    import java.util.Map;
    import java.util.HashMap;
      
    class IterationDemo 
    {
        public static void main(String[] arg)
        {
            Map<String,String> gfg = new HashMap<String,String>();
          
            // enter name/url pair
            gfg.put("GFG", "geeksforgeeks.org");
            gfg.put("Practice", "practice.geeksforgeeks.org");
            gfg.put("Code", "code.geeksforgeeks.org");
            gfg.put("Quiz", "quiz.geeksforgeeks.org");
              
            // using for-each loop for iteration over Map.entrySet()
            for (Map.Entry<String,String> entry : gfg.entrySet()) 
                System.out.println("Key = " + entry.getKey() +
                                 ", Value = " + entry.getValue());
        }
    }
    
    


// Java program to demonstrate iteration over 
// Map using keySet() and values() methods 

import java.util.Map; 
import java.util.HashMap; 

class IterationDemo 
public static void main(String[] arg) 
Map<String,String> gfg = new HashMap<String,String>(); 
// enter name/url pair 
gfg.put("GFG", "geeksforgeeks.org"); 
gfg.put("Practice", "practice.geeksforgeeks.org"); 
gfg.put("Code", "code.geeksforgeeks.org"); 
gfg.put("Quiz", "quiz.geeksforgeeks.org"); 
// using keySet() for iteration over keys 
for (String name : gfg.keySet()) 
System.out.println("key: " + name); 
// using values() for iteration over keys 
for (String url : gfg.values()) 
System.out.println("value: " + url); 












// Java program to demonstrate iteration over // Map using keySet() and values() methods import java.util.Map; import java.util.HashMap; import java.util.Iterator; class IterationDemo { public static void main(String[] arg) { Map<String,String> gfg = new HashMap<String,String>(); // enter name/url pair gfg.put("GFG", "geeksforgeeks.org"); gfg.put("Practice", "practice.geeksforgeeks.org"); gfg.put("Code", "code.geeksforgeeks.org"); gfg.put("Quiz", "quiz.geeksforgeeks.org"); // using iterators Iterator<Map.Entry<String, String>> itr = gfg.entrySet().iterator(); while(itr.hasNext()) { Map.Entry<String, String> entry = itr.next(); System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue()); } } }


---------------------------------------------------------------

// Java code illustrating iteration 
// over map using forEach(action) method 

import java.util.Map; 
import java.util.HashMap; 

class IterationDemo 
public static void main(String[] arg) 
Map<String,String> gfg = new HashMap<String,String>(); 
// enter name/url pair 
gfg.put("GFG", "geeksforgeeks.org"); 
gfg.put("Practice", "practice.geeksforgeeks.org"); 
gfg.put("Code", "code.geeksforgeeks.org"); 
gfg.put("Quiz", "quiz.geeksforgeeks.org"); 
// forEach(action) method to iterate map 
gfg.forEach((k,v) -> System.out.println("Key = "
+ k + ", Value = " + v)); 




// Java program to demonstrate iteration 
// over keys and searching for values 

import java.util.Map; 
import java.util.HashMap; 

class IterationDemo 
public static void main(String[] arg) 
Map<String,String> gfg = new HashMap<String,String>(); 
// enter name/url pair 
gfg.put("GFG", "geeksforgeeks.org"); 
gfg.put("Practice", "practice.geeksforgeeks.org"); 
gfg.put("Code", "code.geeksforgeeks.org"); 
gfg.put("Quiz", "quiz.geeksforgeeks.org"); 
// looping over keys 
for (String name : gfg.keySet()) 
// search for value 
String url = gfg.get(name); 
System.out.println("Key = " + name + ", Value = " + url); 


Explanation :

*** In the above example gfg is the reference of the map.Now by doing gfg.keySet() we are converting it into set and we are taking strings from the set one  by one and storing it in the variable 

in next step inside loop ,from gfg the string is fetched using the method get and stored in the variable




// Java program to demonstrate iteration 
// over keys and searching for values 

import java.util.Map; 
import java.util.HashMap; 

class IterationDemo 
public static void main(String[] arg) 
Map<String,String> gfg = new HashMap<String,String>(); 
// enter name/url pair 
gfg.put("GFG", "geeksforgeeks.org"); 
gfg.put("Practice", "practice.geeksforgeeks.org"); 
gfg.put("Code", "code.geeksforgeeks.org"); 
gfg.put("Quiz", "quiz.geeksforgeeks.org"); 
// looping over keys 
for (String name : gfg.keySet()) 
// search for value 
String url = gfg.get(name); 
System.out.println("Key = " + name + ", Value = " + url); 

Comments

Popular posts from this blog

Git Commands With Output Log

Java Interview Preparation (conceptual)

Java 8 Function Interface