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:

  • Why Webdriver driver = new FirefoxDriver();        WebDriver driver =  new FirefoxDriver();                   (vs)        FirefoxDriver driver =  new Firef… 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
  • 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
  • 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
  • Selenium Example: How to verify whether the table column is sorted or not? Question: How do you check whether a table data(values) is sorted or not? Sorting happens when clicking on Table Header(Ex: Name, which is a link). Name Venu Avinash Dharma Answer: We c… 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