print map in java

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

3.33
3
Thom 130 points

                                    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.33 (3 Votes)
0
4
1

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

4 (1 Votes)
0
4.1
10
Stillfoolish 115 points

                                    import java.util.HashMap;
import java.util.Map;
import java.util.Iterator;
import java.util.Set;
public class Details {

   public static void main(String args[]) {

      /* This is how to declare HashMap */
      HashMap<Integer, String> hmap = new HashMap<Integer, String>();

      /*Adding elements to HashMap*/
      hmap.put(12, "Chaitanya");
      hmap.put(2, "Rahul");
      hmap.put(7, "Singh");
      hmap.put(49, "Ajeet");
      hmap.put(3, "Anuj");

      /* Display content using Iterator*/
      Set set = hmap.entrySet();
      Iterator iterator = set.iterator();
      while(iterator.hasNext()) {
         Map.Entry mentry = (Map.Entry)iterator.next();
         System.out.print("key is: "+ mentry.getKey() + " & Value is: ");
         System.out.println(mentry.getValue());
      }

      /* Get values based on key*/
      String var= hmap.get(2);
      System.out.println("Value at index 2 is: "+var);

      /* Remove values based on key*/
      hmap.remove(3);
      System.out.println("Map key and values after removal:");
      Set set2 = hmap.entrySet();
      Iterator iterator2 = set2.iterator();
      while(iterator2.hasNext()) {
          Map.Entry mentry2 = (Map.Entry)iterator2.next();
          System.out.print("Key is: "+mentry2.getKey() + " & Value is: ");
          System.out.println(mentry2.getValue());
       }

   }
}

4.1 (10 Votes)
0
4.45
10
LJ Writer 95 points

                                    Map<String, Integer> map = new HashMap<>();
map.put("a", 1);
map.put("b", 2);
System.out.println(Arrays.asList(map)); // method 1
System.out.println(Collections.singletonList(map)); // method 2

4.45 (22 Votes)
0
0
9
Forsvunnet 105 points

                                    // 1. for-each with Entry
Map<Integer, Integer> map = new HashMap<Integer, Integer>();
for (Map.Entry<Integer, Integer> entry : map.entrySet()) {
    System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue());
}


// 2. for-each with key or value each (faster)
Map<Integer, Integer> map = new HashMap<Integer, Integer>();
//iterating over keys only
for (Integer key : map.keySet()) {
    System.out.println("Key = " + key);
}
 
//iterating over values only
for (Integer value : map.values()) {
    System.out.println("Value = " + value);
}


// 3. Using Iterator
Map<Integer, Integer> map = new HashMap<Integer, Integer>();
// With Generic
Iterator<Map.Entry<Integer, Integer>> entries = map.entrySet().iterator();
while (entries.hasNext()) {
    Map.Entry<Integer, Integer> entry = entries.next();
    System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue());
}

// No Generic
Map map = new HashMap();
Iterator entries = map.entrySet().iterator();
while (entries.hasNext()) {
    Map.Entry entry = (Map.Entry) entries.next();
    Integer key = (Integer)entry.getKey();
    Integer value = (Integer)entry.getValue();
    System.out.println("Key = " + key + ", Value = " + value);
}

0
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 map println map in java print java print map key value java print map how to print map key and value in java print a hashmap print java map<String,Object> 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 printing a hashmap in java print map values in java java print map\ print values of hashmap can you print a hashmap map print keys and values java print keys of map in java print map key and values in java printing of hashmap print map java from arrays print values map java 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<> elements in java how to print a Map<> in java java print elements in hashmap print map entry java Print a map on Java swing hash map printing printing hash Map print mapping java print map java with key print out map java with key print out map java java how to print each elemenmt in map printing from a hashmap function print map elements in java system out println map java how to print out a string, map java print a map in java hashmap print elementsa print data map java print map java with proper value print java map print hashmap values print map java] how to print values from map in java how to print contents of a map in java how to store and print things in a map java print a map in java 8 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 print a hashmap of strings 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 map values in java 8 print map in java8 printing a map in java print values from a key map java print an hashmap 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 print content of map in java java print key as readable string how to print out map in java 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 all values in map print the key value pair of a hashmap java print the elements of a hashmap java display elements of hashmap java print hashmap in java how to view hashmap in java print key = value format in java print map java8 how to print the map values in java java view all contents of map how to print key value in map print key and value in map java how to print map values java hashmap print printf for map java hashmap in java print how to print certain entry of hashmap map print key value java printing the values of map java how to print 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 put map into map java hashmap print how to print hashmap that has s certin value print key and value hashmap java how to print key and values java print hashmap key value pair hashmap output in java print map print a hashmap in java how to use stream to print key and value hashmap i want to print map key along with value in java display hashmap values in java java show key and values print str value and not maped value java map print key and value how to print out hashmap printing value from hashmap print out a hashmap print hashmap key with certain value java print hashmap key 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 hashmap print key and value print map values display all values in a map print entire map java how to print out the values associated with a given key in java how to print key name in java using for loop java map print display hash maps in single line java print list map java console print map java console 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 map key values in java how to print key value of hashmap print a hash map how to print values from hashmap in java print a hashmap java best way to print map in java print hashmap how to print map values in java using foreach why is my hashmap printing only "}" java why is my hashmap printing just "}" java how to print by value in a map 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 hashmap print value java java hashmap print values print hashmap items java print keyvalue pairs java map print hashmaps java print all values hashmap print all values from all keys hashmap how to print hash map in java printing keys of specific values in hashmaps hashmap print keys on each line print the keys of hashmap with given value print each key and value in hashmap java display key and value java how to display all values from a map how to print kety and value in map print hahsmap java how to display the values of all hashmap how to print map values in java syso hashmap using if loop to find key in hasmap and printing the value of that key print hasmap java hashmap outputted printing elemnts in map java pjava print hsh map how to print the elements in hashmap how to print the elements on hash map 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 printing values in a hashmap print key and value of hashmap in java print whole hashmap in java print map key value java print hashmap value pritning hashmap displaying hashmap in java 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 out map in 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 out the contents of a map java 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 hashmap values print in java print map key with value java 8 print hashmap elements in java display hashmap java java map out print print hash values java log map to console java print a map + java Map<String, Object> how to print print hasgmap java how to print hashmap object in java map values are not printing properly in java print map in java log print out hashmap java java print hashmap java get all keys and values in a map to string print all values in Map using stream in java java system out print ln to map print a map java print all values of hashmap java hashmap printing in java how to print all elements in a hashmap 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 java sout map 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