|
1 | 1 | package basic.collection.map; |
2 | 2 |
|
3 | | -import java.util.AbstractMap; |
4 | | -import java.util.HashSet; |
5 | | -import java.util.LinkedList; |
6 | | -import java.util.ListIterator; |
7 | | -import java.util.Set; |
| 3 | +import java.util.*; |
8 | 4 |
|
9 | 5 | /** |
10 | 6 | * @author yangqc |
11 | 7 | */ |
12 | 8 | public class SimpleHashMap<K, V> extends AbstractMap<K, V> { |
13 | 9 |
|
14 | | - static final int SIZE = 997; |
| 10 | + static final int SIZE = 997; |
15 | 11 |
|
16 | | - @SuppressWarnings("unchecked") |
17 | | - LinkedList<MapEntry<K, V>>[] buckets = new LinkedList[SIZE]; |
| 12 | + @SuppressWarnings("unchecked") |
| 13 | + LinkedList<MapEntry<K, V>>[] buckets = new LinkedList[SIZE]; |
18 | 14 |
|
19 | | - @Override |
20 | | - public V put(K key, V value) { |
21 | | - V oldValue = null; |
22 | | - int index = Math.abs(key.hashCode()) % SIZE; |
23 | | - if (buckets[index] == null) { |
24 | | - buckets[index] = new LinkedList<>(); |
| 15 | + @Override |
| 16 | + public V put(K key, V value) { |
| 17 | + V oldValue = null; |
| 18 | + int index = Math.abs(key.hashCode()) % SIZE; |
| 19 | + if (buckets[index] == null) { |
| 20 | + buckets[index] = new LinkedList<>(); |
| 21 | + } |
| 22 | + LinkedList<MapEntry<K, V>> bucket = buckets[index]; |
| 23 | + MapEntry<K, V> pair = new MapEntry<>(key, value); |
| 24 | + boolean found = false; |
| 25 | + ListIterator<MapEntry<K, V>> it = bucket.listIterator(); |
| 26 | + while (it.hasNext()) { |
| 27 | + MapEntry<K, V> iPair = it.next(); |
| 28 | + if (iPair.getKey().equals(key)) { |
| 29 | + oldValue = iPair.getValue(); |
| 30 | + it.set(pair); |
| 31 | + found = true; |
| 32 | + break; |
| 33 | + } |
| 34 | + } |
| 35 | + if (!found) { |
| 36 | + buckets[index].add(pair); |
| 37 | + } |
| 38 | + return oldValue; |
25 | 39 | } |
26 | | - LinkedList<MapEntry<K, V>> bucket = buckets[index]; |
27 | | - MapEntry<K, V> pair = new MapEntry<>(key, value); |
28 | | - boolean found = false; |
29 | | - ListIterator<MapEntry<K, V>> it = bucket.listIterator(); |
30 | | - while (it.hasNext()) { |
31 | | - MapEntry<K, V> iPair = it.next(); |
32 | | - if (iPair.getKey().equals(key)) { |
33 | | - oldValue = iPair.getValue(); |
34 | | - it.set(pair); |
35 | | - found = true; |
36 | | - break; |
37 | | - } |
38 | | - } |
39 | | - if (!found) { |
40 | | - buckets[index].add(pair); |
41 | | - } |
42 | | - return oldValue; |
43 | | - } |
44 | 40 |
|
45 | | - public V get(Object key) { |
46 | | - int index = Math.abs(key.hashCode()) % SIZE; |
47 | | - if (buckets[index] == null) { |
48 | | - return null; |
49 | | - } |
50 | | - for (MapEntry<K, V> iPair : buckets[index]) { |
51 | | - if (iPair.getKey().equals(key)) { |
52 | | - return iPair.getValue(); |
53 | | - } |
| 41 | + @Override |
| 42 | + public V get(Object key) { |
| 43 | + int index = Math.abs(key.hashCode()) % SIZE; |
| 44 | + if (buckets[index] == null) { |
| 45 | + return null; |
| 46 | + } |
| 47 | + for (MapEntry<K, V> iPair : buckets[index]) { |
| 48 | + if (iPair.getKey().equals(key)) { |
| 49 | + return iPair.getValue(); |
| 50 | + } |
| 51 | + } |
| 52 | + return null; |
54 | 53 | } |
55 | | - return null; |
56 | | - } |
57 | 54 |
|
58 | | - @Override |
59 | | - public Set<Entry<K, V>> entrySet() { |
60 | | - Set<Entry<K, V>> set = new HashSet<>(); |
61 | | - for (LinkedList<MapEntry<K, V>> bucket : buckets) { |
62 | | - if (bucket == null) { |
63 | | - continue; |
64 | | - } |
65 | | - for (MapEntry<K, V> mpair : bucket) { |
66 | | - set.add(mpair); |
67 | | - } |
| 55 | + @Override |
| 56 | + public Set<Entry<K, V>> entrySet() { |
| 57 | + Set<Entry<K, V>> set = new HashSet<>(); |
| 58 | + for (LinkedList<MapEntry<K, V>> bucket : buckets) { |
| 59 | + if (bucket == null) { |
| 60 | + continue; |
| 61 | + } |
| 62 | + for (MapEntry<K, V> mpair : bucket) { |
| 63 | + set.add(mpair); |
| 64 | + } |
| 65 | + } |
| 66 | + return set; |
68 | 67 | } |
69 | | - return set; |
70 | | - } |
71 | 68 |
|
72 | | - public static void main(String[] args) { |
73 | | - SimpleHashMap<String, String> m = new SimpleHashMap<>(); |
74 | | - m.put("123", "dsadcds"); |
75 | | - m.put("2", "dw"); |
76 | | - Set<Entry<String, String>> set = m.entrySet(); |
77 | | - for (Entry<String, String> entry : set) { |
78 | | - System.out.println(entry.getValue()); |
| 69 | + public static void main(String[] args) { |
| 70 | + SimpleHashMap<String, String> m = new SimpleHashMap<>(); |
| 71 | + m.put("123", "dsadcds"); |
| 72 | + m.put("2", "dw"); |
| 73 | + Set<Entry<String, String>> set = m.entrySet(); |
| 74 | + for (Entry<String, String> entry : set) { |
| 75 | + System.out.println(entry.getValue()); |
| 76 | + } |
| 77 | + System.out.println(m); |
| 78 | + System.out.println(m.get("123")); |
79 | 79 | } |
80 | | - System.out.println(m); |
81 | | - System.out.println(m.get("123")); |
82 | | - } |
83 | 80 | } |
0 commit comments