Skip to content

Commit 9b416de

Browse files
committed
toString modified
1 parent 3d4bcb3 commit 9b416de

File tree

7 files changed

+48
-47
lines changed

7 files changed

+48
-47
lines changed
176 Bytes
Binary file not shown.
260 Bytes
Binary file not shown.

build/classes/Maps/Main.class

-277 Bytes
Binary file not shown.
164 Bytes
Binary file not shown.

src/Maps/HashMapCustom.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -122,16 +122,17 @@ public void clear() {
122122

123123
@Override
124124
public String toString() {
125-
String str = "{ ";
125+
StringBuilder builder = new StringBuilder("{");
126126
for (int i = 0; i < bucket.length; i++) {
127127
Entry temp = bucket[i];
128128
while (temp != null) {
129-
str += temp.key + "=" + temp.val + " ";
129+
builder.append(temp.key + "=" + temp.val + ",");
130130
temp = temp.next;
131131
}
132132
}
133-
str += "}";
134-
return str;
133+
if(builder.length()==1) builder.append(" ");
134+
String t = builder.substring(0, builder.length() - 1);
135+
return (new StringBuilder(t)).append("}").toString();
135136
}
136137

137138
public int size() {

src/Maps/LinkedHashMapCustom.java

Lines changed: 30 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -27,46 +27,41 @@ public Entry(K key, V val, Entry next, Entry after, Entry before) {
2727
}
2828

2929
private int hashCode(K key) {
30-
return key.hashCode()%bucket.length;
30+
return key.hashCode() % bucket.length;
3131
}
3232

3333
public boolean put(K key, V val) {
34-
3534
int hash = hashCode(key);
36-
Entry t = new Entry(key, val, null, null, previous);
37-
if (bucket[hash] == null) {
38-
bucket[hash] = t;
35+
if (root == null) {
36+
root = new Entry<>(key, val, null, null, null);
37+
previous = root;
38+
bucket[hash] = root;
3939
size++;
40-
} else {
41-
Entry temp = bucket[hash];
42-
if (temp.next == null) {
43-
if (temp.key.equals(key)) {
44-
temp.val = val;
45-
return true;
46-
}
47-
}
48-
while (temp.next != null) {
49-
if (temp.key.equals(key)) {
50-
temp.val = val;
51-
return true;
52-
}
53-
temp = temp.next;
54-
}
40+
return true;
41+
}
42+
Entry<K, V> temp = bucket[hash];
43+
if (temp == null) {
44+
bucket[hash] = new Entry<>(key, val, null, null, previous);
45+
previous.after = bucket[hash];
46+
previous = bucket[hash];
47+
size++;
48+
return true;
49+
}
50+
while (temp.next != null) {
5551
if (temp.key.equals(key)) {
5652
temp.val = val;
5753
return true;
5854
}
59-
temp.next = t;
60-
t.before = previous;
61-
size++;
62-
}
63-
if (previous != null) {
64-
previous.after = t;
55+
temp = temp.next;
6556
}
66-
previous = t;
67-
if (root == null) {
68-
root = t;
57+
if (temp.key.equals(key)) {
58+
temp.val = val;
59+
return true;
6960
}
61+
temp.next = new Entry<>(key, val, null, null, previous);
62+
previous.after = temp.next;
63+
previous = temp.next;
64+
size++;
7065
return true;
7166
}
7267

@@ -165,14 +160,15 @@ public void clear() {
165160

166161
@Override
167162
public String toString() {
168-
String ret = "{ ";
169-
Entry temp = root;
163+
Entry<K, V> temp = root;
164+
StringBuilder builder = new StringBuilder("{");
170165
while (temp != null) {
171-
ret += temp.key + "=" + temp.val + " ";
166+
builder.append(temp.key + "=" + temp.val + ",");
172167
temp = temp.after;
173168
}
174-
ret += "}";
175-
return ret;
169+
if(builder.length()==1) builder.append(" ");
170+
String t = builder.substring(0, builder.length() - 1);
171+
return new StringBuilder(t).append("}").toString();
176172
}
177173

178174
public int size() {

src/Maps/TreeMapCustom.java

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -84,15 +84,18 @@ private V get(K key, Node node) {
8484
return get(key, node.left);
8585
}
8686

87-
boolean that=false;
87+
boolean that = false;
88+
8889
public V remove(K key) {
8990
V val = get(key);
9091
if (val == null) {
9192
return null;
9293
}
93-
that=false;
94+
that = false;
9495
root = remove(key, root);
95-
if(that ) size++;
96+
if (that) {
97+
size++;
98+
}
9699
return val;
97100
}
98101

@@ -115,8 +118,8 @@ private Node remove(K key, Node node) {
115118
size--;
116119
return node.left;
117120
} else {
118-
size--;
119-
that=true;
121+
size--;
122+
that = true;
120123
Node minKey = getMin(node.right);
121124
node.right = remove((K) minKey.key, node.right);
122125
node.key = minKey.key;
@@ -143,18 +146,19 @@ private void display(Node node, StringBuilder helper) {
143146
return;
144147
}
145148
display(node.left, helper);
146-
helper.append(node.key + "=" + node.val + " ");
149+
helper.append(node.key + "=" + node.val + ",");
147150
display(node.right, helper);
148151
}
149152

150153
@Override
151154
public String toString() {
152-
StringBuilder builder = new StringBuilder("[ ");
155+
StringBuilder builder = new StringBuilder("{");
153156
StringBuilder temp = new StringBuilder();
154157
display(root, temp);
155158
builder.append(temp);
156-
builder.append("]");
157-
return builder.toString();
159+
if(builder.length()==1) builder.append(" ");
160+
String t = builder.substring(0, builder.length() - 1);
161+
return (new StringBuilder(t)).append("}").toString();
158162
}
159163

160164
public ArrayList<K> toKeyArray() {

0 commit comments

Comments
 (0)