import java.util.HashMap; import java.util.Map.Entry; public class Example1 { public static void main(String[] args) { HashMapmap = new HashMap<>(); map.put("key1","value1"); map.put("key2","value2"); map.put("key3","value3"); for(Entry entry : map.entrySet()){ System.out.println(entry.getKey()+" : "+entry.getValue()); } } }
import java.util.TreeMap; import java.util.Map.Entry; public class Example2 { public static void main(String[] args) { TreeMapIn this example, we first create a TreeMap and insert some key-value pairs into it. We then use the entrySet() method to get a Set view of the mappings in the map. Finally, we iterate through this Set to print all the keys whose values are greater than 500. The Map.entrySet() method is part of the java.util package.map = new TreeMap<>(); map.put(100,"value1"); map.put(200,"value2"); map.put(300,"value3"); map.put(400,"value4"); map.put(500,"value5"); map.put(600,"value6"); for(Entry entry : map.entrySet()){ if(entry.getKey()>500){ System.out.println(entry.getKey()); } } } }