print map in java

Solutions on MaxInterview for print map in java by the best coders in the world

showing results for - "print map in java"
Fabio
20 Nov 2017
1map.forEach((key, value) -> System.out.println(key + ":" + value));
Niko
20 Apr 2018
1// 1. for-each with Entry
2Map<Integer, Integer> map = new HashMap<Integer, Integer>();
3for (Map.Entry<Integer, Integer> entry : map.entrySet()) {
4    System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue());
5}
6
7
8// 2. for-each with key or value each (faster)
9Map<Integer, Integer> map = new HashMap<Integer, Integer>();
10//iterating over keys only
11for (Integer key : map.keySet()) {
12    System.out.println("Key = " + key);
13}
14 
15//iterating over values only
16for (Integer value : map.values()) {
17    System.out.println("Value = " + value);
18}
19
20
21// 3. Using Iterator
22Map<Integer, Integer> map = new HashMap<Integer, Integer>();
23// With Generic
24Iterator<Map.Entry<Integer, Integer>> entries = map.entrySet().iterator();
25while (entries.hasNext()) {
26    Map.Entry<Integer, Integer> entry = entries.next();
27    System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue());
28}
29
30// No Generic
31Map map = new HashMap();
32Iterator entries = map.entrySet().iterator();
33while (entries.hasNext()) {
34    Map.Entry entry = (Map.Entry) entries.next();
35    Integer key = (Integer)entry.getKey();
36    Integer value = (Integer)entry.getValue();
37    System.out.println("Key = " + key + ", Value = " + value);
38}
39
Liam
03 Jul 2018
1import java.util.HashMap;
2import java.util.Iterator;
3import java.util.Map;
4import java.util.stream.Stream;
5 
6class MapUtil
7{
8    // Program to print all keys present in the Map using keySet() in Java
9    public static void main (String[] args)
10    {
11        Map<Integer, String> map = new HashMap<>();
12        map.put(1, "One");
13        map.put(2, "Two");
14 
15        // 1. using Iterator
16        Iterator<Integer> itr = map.keySet().iterator();
17        while (itr.hasNext()) {
18            System.out.println(itr.next());
19        }
20 
21        // 2. For-each Loop
22        for (Integer key : map.keySet()) {
23            System.out.println(key);
24        }
25 
26        // 3. Java 8 - Collection.iterator() + Iterator.forEachRemaining()
27        map.keySet().iterator()
28                .forEachRemaining(System.out::println);
29 
30        // 4. Java 8 - Collection.stream() + Stream.forEach()
31        map.keySet().stream()
32                .forEach(System.out::println);
33 
34        // Java 8 - Stream.of() + Collection.toArray() + Stream.forEach()
35        Stream.of(map.keySet().toArray())
36                .forEach(System.out::println);
37 
38        // 5. Convert to String
39        System.out.println(map.keySet().toString());
40 
41        // Java 8
42        Stream.of(map.keySet().toString())
43                .forEach(System.out::println);
44    }
45}
46
Luigi
05 May 2016
1// Java 8 - Collection.iterator() + Iterator.forEachRemaining()
2        map.keySet().iterator()
3                .forEachRemaining(System.out::println);
Ari
03 Nov 2019
1Map<String, Integer> map = new HashMap<>();
2map.put("a", 1);
3map.put("b", 2);
4System.out.println(Arrays.asList(map)); // method 1
5System.out.println(Collections.singletonList(map)); // method 2
6
Nicolás
24 Mar 2017
1import java.util.HashMap;
2import java.util.Map;
3import java.util.Iterator;
4import java.util.Set;
5public class Details {
6
7   public static void main(String args[]) {
8
9      /* This is how to declare HashMap */
10      HashMap<Integer, String> hmap = new HashMap<Integer, String>();
11
12      /*Adding elements to HashMap*/
13      hmap.put(12, "Chaitanya");
14      hmap.put(2, "Rahul");
15      hmap.put(7, "Singh");
16      hmap.put(49, "Ajeet");
17      hmap.put(3, "Anuj");
18
19      /* Display content using Iterator*/
20      Set set = hmap.entrySet();
21      Iterator iterator = set.iterator();
22      while(iterator.hasNext()) {
23         Map.Entry mentry = (Map.Entry)iterator.next();
24         System.out.print("key is: "+ mentry.getKey() + " & Value is: ");
25         System.out.println(mentry.getValue());
26      }
27
28      /* Get values based on key*/
29      String var= hmap.get(2);
30      System.out.println("Value at index 2 is: "+var);
31
32      /* Remove values based on key*/
33      hmap.remove(3);
34      System.out.println("Map key and values after removal:");
35      Set set2 = hmap.entrySet();
36      Iterator iterator2 = set2.iterator();
37      while(iterator2.hasNext()) {
38          Map.Entry mentry2 = (Map.Entry)iterator2.next();
39          System.out.print("Key is: "+mentry2.getKey() + " & Value is: ");
40          System.out.println(mentry2.getValue());
41       }
42
43   }
44}
queries leading to this page
print elemtns of a hashmapprint hashmap key javahow to traverse map in javajava map print key value pairshashmap java foreachprint values from a key map javaiterate over map javajava output hashmapprogram to print key and value of hashmap in java seperatelyprint key and value in maphashmap printloop map javahow to loop through a map in javajava 8 map foreach exampleforeach element in map javajava 8 map foreachfor each and mapfor each mapiterate a map in javajava hashmap foreachprint a hash mapwhy is my hashmap printing only 22 7d 22 javaprint a map in java 8print hashmap values in javahashmap print every value streamdisplaying hashmap in javaoutput map key value javaprint hahsmap javaprint values of hashmapforeach on map javapritning hashmapjava print all elements of hashmaphashmap print key of a certain valueget map with for each javaprint all items in a mapmap foreach javajava map foreach exampleprint map key javahashmap print key and value javaprint a map on java swinghow to print map valueshow to print map values in javalog map to console javafor each map javafor each loop in map javaprint entire map javahowto print key values from map in javafor each loop map javaprint hasgmap javaenter elements in array in javaprint a hashmap in javaiterate through map javai want to print map key along with value in javaprint map java with proper valuesystem out println map javalooping map in javajabva code to print key value pair of map in seperate lines iterate over a map javahow to print hashmap that has s certin valuejava map printprint map java from arraysmap iteration in javaprinting hashmap in javaprint map key and values in javaarraylist map foreach javaprint values of the map javafor each key map javausing if loop to find key in hasmap and printing the value of that keyforeach method in map javascripthow to show key and value in hashmap javahow to print values from map in javahow to make value from print of hashmap in javaprint only values in mapjava print hashmap valuesjava map foreachmap foreach javajava printmapjava loop a maphow to print the key of a hashmap in javaprint out a hashmapmap printing using java 8hashmap printing in javaprint all values of hashmap javamap in java for each use map iterator javaprint map in java with out foreach loopiterate in map javaprint all values in hashmap javaprint the elements of a hashmap javaforeach java hashmapmap foreach java exmaplehow to traverse a map in javajava print elements in hashmapprint key value of map in javahashmap foreach javahow to print out the values associated with a given key in javaprint map javahow to iterate map in java 8java how to print elements from hashmapget all values from a java maphow to print a map that inside a method javajava print map in javaiterate map javahow to iterate through map in javaprinting a hashmap in javause map foreach javahow to display map key and value in javaprint a hashmapprint all the values in a map javawhat ways to iterate map javaprint all values hashmapmap implementation using eachprint key java mapjava map foreach consumerhow to print a map 3c 3e in javadoes map do a for eachjava map for eachforeach key value in map java 5chow to print key value of hashmapprint contents of hashmap in javaiterate and print key values of map in javaprint map entry javahow to loop through map in javajava foreach on mapforeach with map javaloop through map javahow to put map into map javastring map print java forjavascript map foreachmap java foreachhow to print out a map javaprint list map java consoleiterating a map in javaprint map java get keyjava iterate over map with foreachprint hashmap valuesprint map valuesprint values map javahwo to print values of a mapforeach for map in javahow to print values in hashmap javamap get 28 29 for each javahow to print out the contents of a map javahow to print a map entry 28 29 javahashmap print keys on each lineforeach in map javaprint items from a map in javashow hashmap valuesprint a hashmap javaprinting values of map in java without keyprint mapping javaiterate through map values javahow to iterrate over a map in javaiterating through a map javahow to print by value in a map javahow to use stream to print key and value hashmapjava print map key valueprint key value of hashmap in javaprint key 3d value format in javahow to print whole hashmap in javajava sout mapprint map dataprint map in java logjava print the contents of a maphow to iterate a map in javaiteration of map in javajava hashmap outputtediterator over map in javaprint the map in java 8traverse map in javahow to print all values for map javaprint the value of a key hashmap javaprint map key and value javajava foreach over mappjava print hsh mapjava loop array and set values with mapjava system out print ln to maphow to print values from hashmap in javaprint result map and keysprint every element of map in javajava foreach for mapmap values are not printing properly in javaprint key and value from hashmap javahow to print the elements in hashmapjava iterate map with foreachprint all values in mapprint all the key value in a mapeach maptraverse a map in javadisplay hash maps in single line javajava get all keys and values in a map to stringprint map java8how to print all values in hasmapprinting map in javafor each in map javaprint an array map keys javahow to print the map in javaforeach and map java 8print hashmap key value pairjava foreach map entryjava print map keysprint whole hashmap in javadisplay hashmap javajava iterate map and return stringhow to print ivalues from map in javaprint hashmap as string javajava iterate over mapprint map key value javaprint map java 5djava 8 foreach mapwhy is my hashmap printing just 22 7d 22 javaforeach value in map javaiterate map foreach in javatraverse on map in javaprint each key and value in hashmap javaprint hashmap in javahow to print key and value in hashmap in javajava 3a foreach in maphow to print map data in javaforeach maphow to print elements in hashmap in javaprint all elements of map javaprinting elemnts in map javafor each for maps javaiterate map in javaprint all key from hashmap javafor each through mapdisplay key and value javaprint hashmaphow to print hashmap in javahow to print all elements in hashmap in javahow to print a hashmap of string and list in javaprint data map javaprint whole hashmapjava show key and valueshow to iterate through a map in javashow hashmap values javaapply foreach on map in javaprintln hashmap javaloop through map in javaprint only value of hashmap in javatraverse a map of map in javahashmap output in javaprint all values from all keys hashmaphow to print out hashmaphow to print all map values in javahow to print the hashmap in javajava print key as readable stringhashmap print value javajava map foreach functionhow to print kety and value in mapmap for loop javahow to print a map entry javaprintf for map javahow to display elements of map in javaprinting map javahow to print a map in javahow to pirnt value of key in hashmap in javafor each map entry javajava iterate mapcycle through map javaprint key values of java maphow to print a java mapprint all values in map using stream in javaprint keys of a map javajava best practice to iterate over maphow to print map key values in javajava view all contents of maphow to access map in java and print valuesmap iteration in java 8how to print out all keys in a map javaiterate the map in java 8map each itemjava for each in a map java map foreach key valueforeach map javafor each java string mapiterate through map in javaprinting from a hashmap functionprinting a map in javaforeach on map in javajava print map as stringprint hashmap javajava print out mapprint the contents of a hashmap 5chow to print a map javajava print contents of hashmapprint map in javaforeach a map javamap 3cstring 2c object 3e how to printprint a hashmap of stringshow print hashmap javamap for each javaprint elements in map javajava traverse map valuestraverse a map javaforeach in map javaprint all values from a map javaprint hashmaps javamap print javahow to print key and value of map in javaprint a map 2b javaforeach for map javajava iterate in maphow to display all values from a mapjava for each over mapprint map key with value java 8java print hashmapmap in java printjava hashmap printhow to print out all keys iand values in hashmapprint the key value pair of a hashmap javaprint particular value of hashmap in javahow to print map javatraverse through map javahow to print key and values javahashmap print key and valueprint value of a key from map in javaprinting values of map in java without keysprint an hashmapmap print in java7traverse map javaforeach loop java map 3cstring 2cstring 5b 5d 3ejava map iteratemaphash foreach javahow to print value of a particular key in java from a mapjava map each element in arrayjava print all map valuesjava map foreach variableprinting the values of mapjava map iteratormap iterate javaprint mapprint all entries in map javajava map eachforeach map java 8java foreach mapiterate java mapmap and for eachprint keyvalue pairs java mapdisplay hashmap values in javaprint out map javajava iterate a mapforeach for map in java 8for each entry in map javahow to print values of map in javajava foreahc maphow to print hash map in javaprint map values in java 8foreach map entry in javaforeach map object javajava map foreachprint keys of map in javamap show values javajava iterate map foreachwhat ways to loop map in javahow to use foreach for map in java 8string map print javahashmap in java printprint java map 3cstring 2cobject 3e how to iterate on map in javajava map foreach valuejava for each map valuehow to print out map in javahow to print key name in java using for loopprint hasmap in javaprint java maphow to do a foreach loop with a map in javahashmap print elementsaprin hashmap key javafor each in mapfor each java of a mapiterate on map javaprint the keys of hashmap with given valueprint key values of map in javaprint map elements in javahow to use foreach in map javajs for loop print on the same linjava print out hashmapmaps foreach javajava loop through a mapjava map out printprinting value from hashmapjava print all key values in maphow to print contents of a map in javajava hasmap show allprint map in java 8loop through a map javaprint elements in hashmap javahow to print a hashmap in javaprint map java with keyhow to print data for get mapping in javaprint all values in map javahow to print hashmap object in javajava itterate in mapjava 8 map iterationjava map foreach loopforeach java maphow to view hashmap in javajava print map 5chow to print hashmaoprint out hashmap javaprint key and value of a map in javamap foreach in java 8map or foreach in java 3fprint str value and not maped valuesyso hashmapwhat is the difference between map and foreachhow to print values of map in java shortlyprint hashmap key with certain value javaprint a mapforeach key value map javahashmap print key at given valueprint all values from hashmap javaprint the map in javajava map loopjava 8 collections print map key and valueprint key value in hashmap javajava iterate through mapmap keys foreach javahow to print map keys in javahash map printing how to print out a string 2c map javajava how to print map valuesprint key and value hashmap javaprint values from map in javaprint the map key and value in java 8print a value from a map in javadisplay all values in a mapprinting keys of specific values in hashmapshow to print map values in java using foreachjava print all elements of mapfor each with mappritn map javaiterate over a map in javaprint hashmap items javaprint map values in javajava how to print mapmap iterator javaprint key and value map javaprint hashmap elements in javaprint out map java with keyprint content of map in javaprint a map in javaprinting elements of an hashmap in javajava print values of mapprint map in java8iterating through map in javaprint out keyset valueshow to use a for each loop on a map javaprinting of hashmapjava map print key and valueprint map in console javajava iterate map valuesitirate map javamap for eachhow to print the elements on hash mapjava loop thorugh mapjava map loopingprint map key in javacode to print each enrty in hashmapjava foreach hashmapjava map loop valuesjava iterate map key valueprint hashmap valuehashmap print values javajava map for loopjava printout mapjava how to display all the values of hashmapcan you print a hashmapfor each on maphow to print map key and value in javajava return foreach from a maphow to print values in hashmap in javaprint a map javaprint out a key javaprint map using for each in javaiterate through a map javahow to print hashmap key and values in javaiterate over map keys in javajava print maphow to print map in javaprint hash values javahashmap print in javahow to print the map values in javaprint all map on java 8how to all the keys in string from a map in javahow to print all elements in a hashmapjava print all elements in mapjava how to print each elemenmt in mapprint map string objectmap print key value javahow to print java map by valuehow to iterate over map in javajava print map exampleprint out map in javajava loop over mapiterate in map in javajava hashmap print valuesjava loop mapjava print values from mapfor each with maps javamap and foreach javahow to iterate map in javadisplay elements of hashmap javamap foreach java exampleprint hasmapprinting hash mapjava for each key of mapjava for each entry in maphow to print map 3c 3e elements in javaprint keyset javamap or foreach in java stream 3fprint key and value of hashmap in javahow to print key value in mapprint all elements in a map javalooping through a map in javahow to print certain entry of hashmapprint map java consolejava iterate in map of mapsjava print content of mapmap print keys and values javajava 8 print a maphow to print a hashmapforeach hashmap javajava map foreach with key and valueforeach map entry javabest way to print map in javahow to display the values of all hashmapforeach loop for map in java 8printing values in a hashmapiterating over map in javaforeach in map java 8how to store and print things in a map javahow to print chart area in c 23print hashamp javajava loop through mapmap for each loophashmap values print in javatraverse throuh map javajava map for each keyjava wont print values from mapmap iterator in javausing for each on a mapjava to for each on mapjavascript map foreach examplejava print map key and valuehow to print the contents of a map in javajava foreach key in mapprint map in java