HashMap Iteration

Leave a comment

January 18, 2017 by oneOokay

e.g. Map<String, List<String>> map = new HashMap<String, ArrayList<String>>();

如果只关心key的话就可以用for(T t : map.keySet())

  • for (String s : map.keySet())

如果只关心value的话就可以用for (T t: map.values())

  • for (List<List<String>> list : map.values())

如果要iterate全部,两种方法for 和 Iterator:

  • FOR:

for(Map.Entry<T,T> entry : map.entrySet())

for (Map.Entry<String, List<String>> entry : map.entrySet())

entry.getKey()

entry.getValue()

  • ITERATOR:

Iterator<Map.Entry<T,T>> entries = map.entrySet().iterator()

Iterator<Map.Entry<String, List<String>>> entries = map.entrySet().iterator()

while (entries.hasNext())

Map.Entry<T,T> entry = entries.next()

Map.Entry<String, List<String>> entry = entries.next();

entry.getKey()

entry.getValue()

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: