/** * @author lautturi.com * Java example: how to return all the keys of a java hashmap */ import java.util.*; import java.util.Map.Entry; public class Lautturi { public static void main(String[] args) { Map<Integer, String> map = new HashMap<>(); map.put(5,"Apple"); map.put(7,"Banana"); map.put(12,"Cherry"); map.put(33,"Orange"); // return all the keys of hashmap Set<Integer> key = map.keySet(); for(Integer keys: key){ System.out.println(keys+ " : " +map.get(keys)); } } }
output:
33 : Orange 5 : Apple 7 : Banana 12 : Cherry