There are three ways to cycle over a collection:
These are examples of views that cycle over the keys and the values of a map:
Map<String, String> map = new HashMap<String, String>(); map.put("Name", "Mike"); map.put("Surname", "Sullivan"); for (String key : map.keySet()) System.out.println("Key = " + key); for (String value : map.values()) System.out.println("Value = " + value);
This is an example returning a set of Map.Entry (where a Map.Entry is a key-value pair):
Map<String, String> map = new HashMap<String, String>(); map.put("Name", "Mike"); map.put("Surname", "Sullivan"); Set<Map.Entry<String, String>> entries = map.entrySet(); for (Map.Entry<String, String> entry : entries) System.out.println(entry.getKey() + " = " + entry.getValue());
A map can have three types of views:
Copyright © 2013 Welcome to the website of Davis Fiore. All Rights Reserved.