Sunday, January 27, 2019

we cannot iterate a Map directly using iterators, because Map are not Collection.
There are different methods to iterate through MAP elements:
Method 1: Using Iterator and keySet().
package seleniumrepo;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
public class TestIterator {
public static void main(String[] args) {
              Map<String,String> mp=new HashMap<String,String>();
              mp.put("1", "avinash");
              mp.put("2", "venu");
              mp.put("3", "dharma");
              mp.put("4", "hari");
              Iterator<String> it=mp.keySet().iterator();
              while(it.hasNext()){
                    String temp=it.next();
                    System.out.println(temp + " " +mp.get(temp));
              }
}
}

Method 2: Using foreach() method. It is defined as default method in the Map interface.

package seleniumrepo;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
public class TestIterator {
public static void main(String[] args) {
                Map<String,String> map=new HashMap<String,String>();
                map.put("1", "avinash");
                map.put("2", "venugopal");
                map.put("3", "dharma");
                map.put("4", "hari");
                map.forEach((k,v)->System.out.println("Key: "+k+" Value: "+v));
}

Method 3: Using entrySet() and Map.Entry(K, V).
package javaprograms;

import java.util.HashMap;

import java.util.Map;
public class TestIterator {
public static void main(String[] args) {
                    Map<String,String> map=new HashMap<String,String>();
map.put("1", "avinash");
map.put("70", "venugopal");
map.put("30", "dharma");
map.put("4", "hari");
System.out.println(map.values());
for(Map.Entry<String, String> entry: map.entrySet()) {
 System.out.println("Key: "+entry.getKey()+"\t"+ "Value: "+entry.getValue());
}
}
}

Output:
[avinash, hari, venugopal, dharma]
Key: 1 Value: avinash
Key: 4 Value: hari
Key: 70 Value: venugopal
Key: 30 Value: dharma

Related Posts:

  • Manual Testing QuestionsWhat is Smoke Testing?Smoke Testing: This is build acceptance Testing performed after build is released to the Testing. This Testing is done to check the major functionalities are working fine or not.What is Sanity Testing?Sa… Read More
  • Selenium Interview Questions and Answers I have attended some interviews during my career. I am sharing with you questions i was asked in the interviews. Hope these questions are helpful to the readers.    A Java Program to retrieve the data fro… Read More
  • Variables and Methods Variables: Variable is a name given to a memory location in which, we can store some value which can be used in a program. Variable Declaration: It is process of specifying what type of data to be stored into the memory … Read More
  • Type Casting Type Casting: The process of converting value of one Datatype to another Datatype. Two ways of Casting: Widening:  Converting a smaller datatype to higher datatype is called widening. byte-> short -> char … Read More
  • Data Types in Java Based on the type of data stored, data types are classifying as 3 types in Java. Primitive Datatype (Fundamental Datatype): Derived Datatype User Defined Datatype Primitive Datatype (Fundamental Datatype):  The da… Read More

0 comments:

Post a Comment

Selenium Training in Realtime

Blog helps to a student or IT employee to develop or improve skills in Software Testing.

Followers

About Me

My photo
Hyderabad, Andhra Pradesh, India
I am Automation Testing Professional. I have completed my graduation in B.Tech (Computers) from JNTU Hyderabad and started my career in Software Testing accidentally since then, I passionate on learning new technologies

Contact Form

Name

Email *

Message *

Popular Posts