how to print the map in java

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.stream.Stream;
 
class MapUtil
{
    // Program to print all keys present in the Map using keySet() in Java
    public static void main (String[] args)
    {
        Map<Integer, String> map = new HashMap<>();
        map.put(1, "One");
        map.put(2, "Two");
 
        // 1. using Iterator
        Iterator<Integer> itr = map.keySet().iterator();
        while (itr.hasNext()) {
            System.out.println(itr.next());
        }
 
        // 2. For-each Loop
        for (Integer key : map.keySet()) {
            System.out.println(key);
        }
 
        // 3. Java 8 - Collection.iterator() + Iterator.forEachRemaining()
        map.keySet().iterator()
                .forEachRemaining(System.out::println);
 
        // 4. Java 8 - Collection.stream() + Stream.forEach()
        map.keySet().stream()
                .forEach(System.out::println);
 
        // Java 8 - Stream.of() + Collection.toArray() + Stream.forEach()
        Stream.of(map.keySet().toArray())
                .forEach(System.out::println);
 
        // 5. Convert to String
        System.out.println(map.keySet().toString());
 
        // Java 8
        Stream.of(map.keySet().toString())
                .forEach(System.out::println);
    }
}

3.75
8
Awgiedawgie 440215 points

                                    // Java 8 - Collection.iterator() + Iterator.forEachRemaining()
        map.keySet().iterator()
                .forEachRemaining(System.out::println);

3.75 (8 Votes)
0
Are there any code examples left?
Create a Free Account
Unlock the power of data and AI by diving into Python, ChatGPT, SQL, Power BI, and beyond.
Sign up
Develop soft skills on BrainApps
Complete the IQ Test
Relative searches
how to print map java how to print values of map in java java print map key value java print map how to print map key and value in java print all entries in map java how to print a java map how to print data for get mapping in java print an Array map keys java print all elements of map java print all the values in a map java print map values in java map print keys and values java print keys of map in java print map key and values in java print map java from arrays print keys of a map java print key and value of a map in java Print all values from a map java java print map as string print value of a key from map in java how to access map in java and print values how to print Map&lt;&gt; elements in java print map entry java Print a map on Java swing print out map java with key print out map java java how to print each elemenmt in map how to print out a string, map java print a map in java print map java with proper value print java map how to print contents of a map in java how to store and print things in a map java how print hashmap java how to print a map entry java how to print a map that inside a method java java print the contents of a map java print map in java java 8 print a map how to print map data in java how to print key and value of map in java how to print all values for map java print a value from a map in java print items from a map in java print values from a key map java how to print values of map in java shortly how to display map key and value in java java output hashmap java print out map java print map example java print key as readable string java 8 collections print map key and value how to display elements of map in java println hashmap java printing hashmap in java how to print a hashmap of string and list in java iterate and print key values of map in java print only value of hashmap in java print key value of hashmap in java print the key value pair of a hashmap java print the elements of a hashmap java print hashmap in java how to view hashmap in java print key = value format in java java view all contents of map how to print key value in map print key and value in map java hashmap print hashmap in java print how to print certain entry of hashmap map print key value java printing the values of map how to all the keys in string from a map in java print map key and value java map print in java7 print out keyset values how to print hashmap that has s certin value how to print key and values java print hashmap key value pair hashmap output in java print a hashmap in java i want to print map key along with value in java display hashmap values in java java map print key and value printing value from hashmap print out a hashmap print hashmap key with certain value java prin hashmap key java print key values of map in java java hasmap show all how to make value from print of hashmap in java how to print a hashmap in java Map show values java print out a key java hashmap print key of a certain value hashmap print key at given value print map values display all values in a map how to print key name in java using for loop java map print hashmap print values java how to print the hashmap in java java print all key values in map print all elements in a map java how to print key value of hashmap print a hash map how to print values from hashmap in java print a hashmap java print hashmap how to print map values in java using foreach why is my hashmap printing only &quot;}&quot; java why is my hashmap printing just &quot;}&quot; java print all items in a map java print map key and value how to pirnt value of key in hashmap in java how to print whole hashmap in java how to print values in hashmap in java java print values from map java hashmap print values print keyvalue pairs java map print all values from all keys hashmap how to print hash map in java print the keys of hashmap with given value print each key and value in hashmap java how to display all values from a map how to print kety and value in map print hahsmap java how to print map values in java print hasmap printing elemnts in map java pjava print hsh map how to print the elements in hashmap print all the key value in a map print map data print only values in map print map using for each in java printing map in java print the contents of a hashmap\ show hashmap values how to print a map in java java map print key value pairs how to print all values in hasmap hashmap print every value stream how to print out all keys iand values in hashmap how to print out all keys in a map java pritn map java how to show key and value in hashmap java java how to print elements from hashmap print key and value of hashmap in java print whole hashmap in java print map key value java print hashmap value pritning hashmap print a map java wont print values from map how to print ivalues from map in java how to print out a map java how to print map in java how to print a hashmap print all values in map java print all values from hashmap java print the value of a key hashmap java howto print key values from map in java how to print hashmao hwo to print values of a map print hashamp java java print contents of hashmap how to print the contents of a map in java java print hashmap values jabva code to print key value pair of map in seperate lines how to print elements in hashmap in java java print all elements of map map print java how to print all map values in java print the map key and value in java 8 print the map in java 8 print map in java with out foreach loop print hasmap in java print all map on java 8 print key value of map in java print map key with value java 8 print hashmap elements in java display hashmap java print hasgmap java java print hashmap java get all keys and values in a map to string java system out print ln to map print a map java print all values of hashmap java hashmap printing in java print particular value of hashmap in java java print all elements of hashmap show hashmap values java get all values from a java map print Map in console java how to print java map by value print hashmap values in java print key value in hashmap java print hashmap java how to print hashmap key and values in java how to print values in hashmap java java print out hashmap java printout map print all key from hashmap java output map key value java print values of the Map java print map string object print key and value map java hashmap print key and value java hashmap print in java code to print each enrty in hashmap print key values of java map print map key in java print result map and keys how to print the key of a hashmap in java java print all map values print elemtns of a hashmap how to print map values how to print hashmap in java enter elements in array in java how to print chart area in c# js for loop print on the same lin java print all elements in map print keyset java print hashmap as string java java print content of map how to print all elements in hashmap in java print elements in map java printing values of map in java without key printing values of map in java without keys program to print key and value of hashmap in java seperately print map in java print key and value from hashmap java print contents of hashmap in java print map java how to print key and value in hashmap in java print values from map in java java print values of Map how to print a map java java how to display all the values of hashmap how to print the map in java
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source