how to iterate a map in java using for each

Ads
 

how to iterate a map in java using for each

Hi,

I have a HashMap in Java and there is need to iterate all the items.

how to iterate a map in java using for each?

Thanks

View Answers

February 13, 2020 at 3:39 AM

Hi,

This example is using the stream of Java 8:

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

public class MapIterate {

    public static void main(String[] args) {
        Map<String, String> map = new HashMap<String, String>();
        map.put("1", "One");
        map.put("2", "Two");
        map.put("3", "Three");
        map.put("4", "Four");
        map.put("5", "Five");
        map.put("6", "Six");
        map.put("7", "Seven");
        map.put("8", "Eight");
        map.put("9", "Nine");
        map.put("10", "Ten");

        map.forEach((key, value) -> System.out.println("Key:" + key + ", Value:" + value));


    }

}

and output will be:

Key:1, Value:One
Key:2, Value:Two
Key:3, Value:Three
Key:4, Value:Four
Key:5, Value:Five
Key:6, Value:Six
Key:7, Value:Seven
Key:8, Value:Eight
Key:9, Value:Nine
Key:10, Value:Ten

Thanks


February 13, 2020 at 5:32 AM

Hi,

It can be also iterated like this:

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

public class MapIterate {

    public static void main(String[] args) {
        Map<String, String> map = new HashMap<String, String>();
        map.put("1", "One");
        map.put("2", "Two");
        map.put("3", "Three");
        map.put("4", "Four");
        map.put("5", "Five");
        map.put("6", "Six");
        map.put("7", "Seven");
        map.put("8", "Eight");
        map.put("9", "Nine");
        map.put("10", "Ten");

        map.forEach((key, value) -> {
            System.out.println("Key: " + key + ", Value : " + value);
        });

    }

}

Thanks

Ads









Related Tutorials/Questions & Answers:
how to iterate a map in java using for each
how to iterate a map in java using for each  Hi, I have a HashMap in Java and there is need to iterate all the items. how to iterate a map in java using for each? Thanks   Hi, This example is using the stream
how to iterate a map in java using for each
how to iterate a map in java using for each  Hi, I have a HashMap in Java and there is need to iterate all the items. how to iterate a map in java using for each? Thanks   Hi, This example is using the stream
Advertisements
how to iterate a map in java using for each
how to iterate a map in java using for each  Hi, I have a HashMap in Java and there is need to iterate all the items. how to iterate a map in java using for each? Thanks   Hi, This example is using the stream
java code for PartialSearch using Map????
java code for PartialSearch using Map????  java code for Partial Search using Map
Iterate a List in Java
Iterate a List in Java In this tutorial I will show you how you can iterate... With the help of while loop Java 5 for-each loop Following example code shows how... of java 5 for-each loop System.out.println("Iterate with the help of java 5
how to print the average of each column of 2d n*m array using java
how to print the average of each column of 2d n*m array using java  here is my code: import java.io.File; import java.io.IOException; import... the content of file.now i want to calculate mean(average) of each column present
How does the Java for each loop work?
How does the Java for each loop work?  How does the Java for each loop work
How to convert map to json in Java
How to convert map to json in Java  Hi, I my program I have an object of HashMap and I have to output this into json string. How to convert map to json in Java? Thanks   Hi, You have to use the Google Gson library
How to convert map to json in Java
How to convert map to json in Java  Hi, I my program I have an object of HashMap and I have to output this into json string. How to convert map to json in Java? Thanks   Hi, You have to use the Google Gson library
how to make authentication for each user using jsp/servlet?
how to make authentication for each user using jsp/servlet?  i have 10 jsp pages and 5 users.I use 10 buttons for open the page.Admin assign some jsp pages to each user.If admin assign page1 and page2 for user1 then make other
How to iterate over a checkbox group
How to iterate over a checkbox group  Hi. I am new to using JQuery... resides. I've tried all manner of techniques, including "$.each" and ".map... checkbox groups. Each of these groups consist of multiple checkboxes. The first
how to get data from list to map with out using a loop?
how to get data from list to map with out using a loop?  List list...("15"); how to get this data into map with out using a loop. i want this data like this--map.put(name,id); Is it possible to do without using loop
iterate through map and write the content in a separate file
iterate through map and write the content in a separate file  I am trying to save the data from file to a map, then iterate through map and write...; map: a d2rq:PropertyBridge; The last step would be to iterate through
iterate through map and write the content in a separate file
iterate through map and write the content in a separate file  I am trying to save the data from file to a map, then iterate through map and write...; map: a d2rq:PropertyBridge; The last step would be to iterate through
iterate through map and write the content in a separate file
iterate through map and write the content in a separate file  I am trying to save the data from file to a map, then iterate through map and write...; map: a d2rq:PropertyBridge; The last step would be to iterate through
How do you map Java Objects with Database tables?
How do you map Java Objects with Database tables?  Hi, How do you map Java Objects with Database tables? thanks
How to design a form using java?
How to design a form using java?  Please help in designing of form using java for my project
ApplicationDevelopment Using Google Map
ApplicationDevelopment Using Google Map  I want application using google map please guide me how can i develop
how to get description for each file
how to get description for each file  hi, how to get description for each file using php or java script..... condtion:two text boxes for each... for each file without description dont upload..... u have any idea......code
How to implement FTP using java
How to implement FTP using java  Hi, I am a B.tech student and I want to implement FTP using Java to transfer files and exchange files between FTP client and FTP server. Could anyone help me for How to implement FTP using java
for-each loop in java
for-each loop in java In this section you will learn about for-each loop in java. for-each is a control statement which is used to traverse an array or object. for-each loop is used to iterate over array or collection object as well
how to open notepad using java
how to open notepad using java  Hi, how to open notepad using java? I want to open windows notepad from java program. Thanks   Hi, Use following code: import java.util.*; import java.io.*; class Notepad { public
how to refresh google map using ajax by fetching lat long from database
how to refresh google map using ajax by fetching lat long from database ... the route travelled by it will be displayed on the google map by fetching the lat long from db. Now, i need to refresh the map using ajax.Pls help me how to do
how to get harddisk info using S.M.A.R.T using java
how to get harddisk info using S.M.A.R.T using java   how to get harddisk info using S.M.A.R.T using java
How to transfer a file in java using SSH & Telnet
How to transfer a file in java using SSH & Telnet  How to transfer a file in java using SSH & Telnet
How to open a file in java using JFileChooser
How to open a file in java using JFileChooser  How to open a file in java using JFileChooser
how to extract css properties using java?
how to extract css properties using java?  how to extract css properties using java
How to merge two word document using java
How to merge two word document using java   how to merge two word document using java
How to merge two word document using java
How to merge two word document using java   how to merge two word document using java
how to create users in LDAP using java?
how to create users in LDAP using java?  how to create users in LDAP using java
Map java Object to database.
Map java Object to database.  How do you map Java Objects with Database tables

Ads