Friday, 27 April 2018

java - Iterating over hashmap











I've written the following piece of code and am stuck on iterating over the hashmap.



import java.util.ArrayList;
import java.util.HashMap;

import java.util.Iterator;
import java.util.List;

class demo
{
public static void main(String v[]) {
ArrayList contactIds = new ArrayList();
contactIds.add("2");
contactIds.add("3");


HashMap names = new HashMap();
names = getNames(contactIds);

// I want to get the total size of the hashmap - names
// for ex now there are 6 elements inside hashmap.
// How can I get that count?

}



private static HashMap getNames(ArrayList contactIds) {
HashMap names = new HashMap();
String params = null;
List list = new ArrayList();
for(int i=0; i params = contactIds.get(i).toString();

list.add(0,"aer-1");
list.add(1,"aer-2");
list.add(2,"aer-3");


names.put(params,list) ;
}

return names;
}
}


In this code, there are six elments inside the map, now in the main method how can I iterate over the map and get the total count?




Thank you.


Answer



Your question is asked - and answered - here:



How to efficiently iterate over each Entry in a Map?



for (Map.Entry entry : map.entrySet()) {
System.out.println(entry.getKey() + "/" + entry.getValue());
}


No comments:

Post a Comment

casting - Why wasn't Tobey Maguire in The Amazing Spider-Man? - Movies & TV

In the Spider-Man franchise, Tobey Maguire is an outstanding performer as a Spider-Man and also reprised his role in the sequels Spider-Man...