基于链表的映射的实现

基于链表的映射的实现

映射接口

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
package SetBasicsAndBSTSet;

/**
* @ Description: 映射接口
* @ Date: Created in 21:54 20/07/2018
* @ Author: Anthony_Duan
*/
public interface Map<K, V> {
void add(K key, V value);

V remove(K key);

boolean contains(K key);

V get(K key);

void set(K key, V newValue);

int getSize();

boolean isEmpty();
}

实现接口即可

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package SetBasicsAndBSTSet;

import java.util.ArrayList;

/**
* @ Description: 利用链表实现映射
* @ Date: Created in 21:57 20/07/2018
* @ Author: Anthony_Duan
*/
public class LinkedListMap<K, V> implements Map<K, V> {

private class Node {

public K key;
public V value;
public Node next;


public Node(K key, V value, Node next) {
this.key = key;
this.value = value;
this.next = next;
}


public Node(K key, V value) {
this(key, value, null);
}

public Node() {
this(null, null, null);
}


@Override
public String toString() {
return key.toString() + ":" + value.toString();
}
}


private Node dummyHead;
private int size;

public LinkedListMap() {
dummyHead = new Node();
size = 0;
}


private Node getNode(K key) {

Node cur = dummyHead.next;
while (cur != null) {
if (cur.key.equals(key)) {
return cur;
}
}
return null;
}


@Override
public void add(K key, V value) {
Node node = getNode(key);
if (node == null) {
dummyHead.next = new Node(key, value, dummyHead.next);
} else {
node.value = value;
}
}


@Override
public V remove(K key) {

Node prev = dummyHead;
while (prev.next != null) {
if (prev.next.key.equals(key)) {
break;
}
prev = prev.next;
}

//这个if判断必须有
if (prev.next != null) {
Node delNode = prev.next;
prev.next = delNode.next;
delNode.next = null;
size--;
return delNode.value;
}

return null;
}

@Override
public boolean contains(K key) {
return getNode(key) != null;
}

@Override
public V get(K key) {
Node node = getNode(key);
return node == null ? null : node.value;
}

@Override
public void set(K key, V newValue) {

Node node = getNode(key);
if (node == null) {
throw new IllegalArgumentException(key + " doesn't exist!");
}
node.value = newValue;
}

@Override
public int getSize() {
return size;
}

@Override
public boolean isEmpty() {
return size == 0;
}


public static void main(String[] args) {

System.out.println("Pride and Prejudice");

ArrayList<String> words = new ArrayList<>();
if (FileOperation.readFile("/Users/duanjiaxing/IdeaProjects/Data-Structure/data-structure/src/SetBasicsAndBSTSet/pride-and-prejudice.txt", words)) {
System.out.println("Total words: " + words.size());

LinkedListMap<String, Integer> map = new LinkedListMap<>();
for (String word : words) {
if (map.contains(word)) {
map.set(word, map.get(word) + 1);
} else {
map.add(word, 1);
}
}

System.out.println("Total different words: " + map.getSize());
System.out.println("Frequency of PRIDE: " + map.get("pride"));
System.out.println("Frequency of PREJUDICE: " + map.get("prejudice"));
}

System.out.println();
}
}

-------------End Of This ArticleThank You For Reading-------------