Skip to main content

Posts

Showing posts with the label Map

How to loop through a Map?

-- Using Map in java is a convenient way to store objects into a collection and identified by their names.  The Map interface provides API of adding and getting a particular object by the name it’s stored with.  However, there is no simple API to loop through all the items in the Map.  An older fashion to do this is to use the java.util.Iterator, for example: public void test () {         Map testMap = new HashMap();         testMap.put("key1", "value1");         testMap.put("key2", "value2");         Iterator it = testMap.keySet() .iterator () ;          while (  it.hasNext ()   )   {              String key =  (String) it.next () ;          ...