1Map<String, String> map = new HashMap<>();
2
3for(Entry<String, String> entry:map.entrySet()) {
4 System.out.println("key: "+entry.getKey()+" value: "+entry.getValue());
5}
1Map <Integer, Point2D.Double> hm = new HashMap<Integer, Point2D>();
2hm.put(1, new Point2D.Double(50, 50));
1Map< String,Integer> hm =
2 new HashMap< String,Integer>();
3 hm.put("a", new Integer(100));
4 hm.put("b", new Integer(200));
5 hm.put("c", new Integer(300));
6 hm.put("d", new Integer(400));
1/**
2 * @author Jack Waller <jw309@st-andrews.ac.uk>
3 * @version 1.1
4 * @since 2020-03-28
5 * A implementation of Map.Entry.
6 */
7public final class Pair<K, V> implements Map.Entry<K, V> {
8
9 //variables
10 private final K key;
11 private V value;
12
13 //constructor
14 public Pair(K key, V value) {
15 this.key = key;
16 this.value = value;
17 }
18
19 //methods
20
21 /**
22 * Returns the key.
23 * @return K
24 */
25 @Override
26 public K getKey() {
27 return key;
28 }
29
30 /**
31 * Returns the value.
32 * @return V
33 */
34 @Override
35 public V getValue() {
36 return value;
37 }
38
39 /**
40 * Sets the value, returns the old value
41 * @param v value to set
42 * @return V
43 */
44 @Override
45 public V setValue(V v) {
46 V old = this.value;
47 this.value = v;
48 return old;
49 }
50}