Answers for "for each map java"

4

java map foreach

// 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);
}
Posted by: Guest on May-14-2020
5

java for map

Map<String, String> map = new HashMap<>();

for(Entry<String, String> entry:map.entrySet()) {
  System.out.println("key: "+entry.getKey()+" value: "+entry.getValue());
}
Posted by: Guest on July-07-2020
3

java foreach map

map.forEach((k, v) -> System.out.printf( " %s : %d \n" , k,v) );
Posted by: Guest on March-16-2020
1

foreach map java

Map<String, String> map = ...
for (Map.Entry<String, String> entry : map.entrySet()) {
    System.out.println(entry.getKey() + "/" + entry.getValue());
}
Posted by: Guest on June-01-2020
0

java 8 map foreach

public void iterateUsingStreamAPI(Map<String, Integer> map) {
    map.entrySet().stream()
      // ...
      .forEach(e -> System.out.println(e.getKey() + ":" + e.getValue()));
}
Posted by: Guest on March-10-2020
0

java 8 map foreach

public void iterateUsingEntrySet(Map<String, Integer> map) {
    for (Map.Entry<String, Integer> entry : map.entrySet()) {
        System.out.println(entry.getKey() + ":" + entry.getValue());
    }
}
Posted by: Guest on May-29-2020
0

java 8 map foreach

        //4.  Iterating Map through forEach
        Map<Integer, String> countriesMap = new HashMap<>();
        countriesMap.put(1, "India");
        countriesMap.put(2, "USA");
        countriesMap.put(3, "Australia");
        countriesMap.forEach((k, v) -> System.out.println("No = " + k + ", Country = " + v));
Posted by: docsconsole on February-06-2023
0

for each map java

        // 2 Iterating over entrySet of map
        Map<String, Integer> cityPINsMap = new HashMap<>();
        cityPINsMap.put("NewDelhi", 110001);
        cityPINsMap.put("NewYork", 10001);
        cityPINsMap.put("Sydney", 2000);
        for (Map.Entry<String, Integer> entry : cityPINsMap.entrySet()) {
            System.out.println("City = " + entry.getKey() + ", PIN = " + entry.getValue());
        }
Posted by: docsconsole on February-06-2023

Code answers related to "Java"

Java Answers by Framework

Browse Popular Code Answers by Language