• HashMap on Java
    Java

    Different ways to traverse a Map in Java

    In this quick article, we’ll have a look at the different ways of iterating through the entries of a Map in Java. Simply put, we can extract the contents of a Map using keySet(), valueSet() or entrySet(). Since those are all sets, similar iteration principles apply to all of them. The Map.entrySet API returns a collection-view of the map, whose elements are from the Map class. The only way to obtain a reference to a single map entry is from the iterator of this collection view. The entry.getKey() returns the key and entry.getValue() returns the corresponding value. Let’s have a look at a few of these. We will use following…