Skip to content

Commit 5dcaaff

Browse files
update list and map
1 parent 236e68d commit 5dcaaff

File tree

9 files changed

+315
-17
lines changed

9 files changed

+315
-17
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
package com.examplehub.basics.collection;
2+
3+
public class CollectionsExample {
4+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
package com.examplehub.basics.collection;
2+
3+
public class ListExample {
4+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
package com.examplehub.basics.collection;
2+
3+
public class StackExample {
4+
}

src/test/java/com/examplehub/basics/HashMapExampleTest.java

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,30 +4,34 @@
44

55
import java.util.HashMap;
66
import java.util.Map;
7+
78
import org.junit.jupiter.api.Test;
89

910
class HashMapExampleTest {
1011

1112
@Test
1213
void testPut() {
1314
HashMap<String, String> hashMap = new HashMap<>();
14-
hashMap.put("username", "admin");
15-
hashMap.put("password", "abc123");
15+
assertNull(hashMap.put("username", "admin"));
16+
assertNull(hashMap.put("password", "abc123"));
1617
assertEquals("{password=abc123, username=admin}", hashMap.toString());
1718

1819
assertNull(hashMap.put("bio", "Github"));
1920
assertEquals("Github", hashMap.put("bio", "I love coding"));
2021

21-
hashMap.put("username", "admin_username");
22+
assertEquals("admin", hashMap.put("username", "admin_username"));
2223
assertEquals("admin_username", hashMap.get("username"));
24+
25+
assertEquals("{password=abc123, bio=I love coding, username=admin_username}",
26+
hashMap.toString());
2327
}
2428

2529
@Test
2630
void testPutAll() {
2731
Map<String, String> map = Map.of("username", "admin", "password", "abc123");
2832
HashMap<String, String> hashMap = new HashMap<>();
2933
hashMap.putAll(map);
30-
System.out.println(hashMap);
34+
assertEquals("{password=abc123, username=admin}", hashMap.toString());
3135
}
3236

3337
@Test
@@ -118,10 +122,10 @@ void testKeySet() {
118122
assertEquals("[password, username]", hashMap.keySet().toString());
119123

120124
String[][] keyValues =
121-
new String[][] {
122-
{"password", "abc123"},
123-
{"username", "admin"}
124-
};
125+
new String[][]{
126+
{"password", "abc123"},
127+
{"username", "admin"}
128+
};
125129
int index = 0;
126130
for (String key : hashMap.keySet()) {
127131
assertEquals(keyValues[index][0], key);
@@ -153,10 +157,10 @@ void testEntry() {
153157
hashMap.put("username", "admin");
154158
hashMap.put("password", "abc123");
155159
String[][] keyValues =
156-
new String[][] {
157-
{"password", "abc123"},
158-
{"username", "admin"}
159-
};
160+
new String[][]{
161+
{"password", "abc123"},
162+
{"username", "admin"}
163+
};
160164
int index = 0;
161165
for (Map.Entry<String, String> entry : hashMap.entrySet()) {
162166
assertEquals(keyValues[index][0], entry.getKey());
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package com.examplehub.basics.collection;
2+
3+
import org.junit.jupiter.api.Test;
4+
5+
import java.util.ArrayList;
6+
import java.util.Arrays;
7+
import java.util.Collections;
8+
import java.util.List;
9+
10+
import static org.junit.jupiter.api.Assertions.*;
11+
12+
class CollectionsExampleTest {
13+
@Test
14+
void testMax() {
15+
List<Integer> list = Arrays.asList(1, 3, 5, 2, 4, 6);
16+
assertEquals(6, Collections.max(list));
17+
}
18+
19+
@Test
20+
void testMin() {
21+
List<Integer> list = Arrays.asList(1, 3, 5, 2, 4, 6);
22+
assertEquals(1, Collections.min(list));
23+
}
24+
25+
@Test
26+
void testReverse() {
27+
List<Integer> list = Arrays.asList(1, 3, 5, 2, 4, 6);
28+
Collections.reverse(list);
29+
assertEquals("[6, 4, 2, 5, 3, 1]", list.toString());
30+
}
31+
32+
@Test
33+
void testSwap() {
34+
List<Integer> list = Arrays.asList(1, 3, 5, 2, 4, 6);
35+
Collections.swap(list, 0, list.size() - 1);
36+
assertTrue(list.get(0) == 6 && list.get(list.size() - 1) == 1);
37+
}
38+
39+
@Test
40+
void testSort() {
41+
List<Integer> list = Arrays.asList(1, 3, 5, 2, 4, 6);
42+
Collections.sort(list);
43+
assertEquals("[1, 2, 3, 4, 5, 6]", list.toString());
44+
}
45+
46+
@Test
47+
void testShuffle() {
48+
List<Integer> list = Arrays.asList(1, 2, 3, 4, 5, 6);
49+
Collections.shuffle(list);
50+
System.out.println(list);
51+
}
52+
53+
@Test
54+
void testCopy() {
55+
List<Integer> src = Arrays.asList(1, 2, 3, 4, 5, 6);
56+
List<Integer> dist = Arrays.asList(new Integer[src.size()]);
57+
Collections.copy(dist, src);
58+
assertEquals("[1, 2, 3, 4, 5, 6]", dist.toString());
59+
}
60+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package com.examplehub.basics.collection;
2+
3+
import org.junit.jupiter.api.Test;
4+
5+
import java.util.LinkedList;
6+
import java.util.List;
7+
8+
import static org.junit.jupiter.api.Assertions.*;
9+
10+
public class ListExampleTest {
11+
@Test
12+
void testAdd() {
13+
List<String> list = new LinkedList<>();
14+
list.add("1");
15+
list.add("2");
16+
list.add("3");
17+
assertEquals("[1, 2, 3]", list.toString());
18+
}
19+
20+
@Test
21+
void testAddAll() {
22+
List<String> list = new LinkedList<>();
23+
list.add("1");
24+
list.add("2");
25+
list.add("3");
26+
27+
List<String> list2 = new LinkedList<>();
28+
list2.add("4");
29+
list2.add("5");
30+
list2.add("6");
31+
32+
list.addAll(list2);
33+
assertEquals("[1, 2, 3, 4, 5, 6]", list.toString());
34+
}
35+
36+
@Test
37+
void testGet() {
38+
List<String> list = new LinkedList<>();
39+
list.add("1");
40+
list.add("2");
41+
list.add("3");
42+
for (int i = 0; i < 3; ++i) {
43+
assertEquals(i + 1 + "", list.get(i));
44+
}
45+
}
46+
47+
@Test
48+
void testSet() {
49+
List<String> list = new LinkedList<>();
50+
list.add("1");
51+
list.add("2");
52+
list.add("3");
53+
54+
assertEquals("2", list.set(1, "22"));
55+
assertEquals("22", list.get(1));
56+
}
57+
58+
@Test
59+
void testRemove() {
60+
List<String> list = new LinkedList<>();
61+
list.add("1");
62+
list.add("2");
63+
list.add("3");
64+
65+
for (int i = 1; i <= 3; ++i) {
66+
assertEquals(i + "", list.remove(0));
67+
}
68+
assertEquals("[]", list.toString());
69+
}
70+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package com.examplehub.basics.set;
2+
3+
import org.junit.jupiter.api.Test;
4+
5+
import java.util.HashSet;
6+
import java.util.Set;
7+
8+
import static org.junit.jupiter.api.Assertions.*;
9+
10+
class HashSetExampleTest {
11+
@Test
12+
void testAdd() {
13+
Set<Integer> set = new HashSet<>();
14+
assertTrue(set.add(2));
15+
assertFalse(set.add(2));
16+
assertTrue(set.add(3));
17+
assertTrue(set.add(1));
18+
assertEquals("[1, 2, 3]", set.toString());
19+
}
20+
21+
@Test
22+
void testRemove() {
23+
Set<Integer> set = new HashSet<>();
24+
set.add(2);
25+
set.add(3);
26+
set.add(1);
27+
assertTrue(set.remove(2));
28+
assertTrue(set.remove(1));
29+
assertFalse(set.remove(1));
30+
assertEquals("[3]", set.toString());
31+
}
32+
33+
@Test
34+
void testClear() {
35+
Set<Integer> set = new HashSet<>();
36+
set.add(2);
37+
set.add(3);
38+
set.add(1);
39+
assertEquals("[1, 2, 3]", set.toString());
40+
41+
set.clear();
42+
assertEquals("[]", set.toString());
43+
}
44+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.examplehub.basics.set;
2+
3+
import org.junit.jupiter.api.Test;
4+
5+
import java.util.LinkedHashSet;
6+
import java.util.Set;
7+
8+
import static org.junit.jupiter.api.Assertions.*;
9+
10+
class LinkedListExampleTest {
11+
@Test
12+
void testAdd() {
13+
Set<Integer> set = new LinkedHashSet<>();
14+
assertTrue(set.add(2));
15+
assertFalse(set.add(2));
16+
assertTrue(set.add(3));
17+
assertTrue(set.add(1));
18+
assertEquals("[2, 3, 1]", set.toString());
19+
}
20+
}

src/test/java/com/examplehub/basics/set/TreeSetExampleTest.java

Lines changed: 93 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,106 @@
22

33
import static org.junit.jupiter.api.Assertions.*;
44

5+
import java.util.Comparator;
56
import java.util.Set;
67
import java.util.TreeSet;
8+
79
import org.junit.jupiter.api.Test;
810

911
class TreeSetExampleTest {
1012
@Test
11-
void testTreeSet() {
13+
void testAdd() {
1214
Set<String> set = new TreeSet<>();
13-
set.add("B");
14-
set.add("A");
15-
set.add("C");
16-
set.add("D");
15+
assertTrue(set.add("B"));
16+
assertTrue(set.add("A"));
17+
assertTrue(set.add("C"));
18+
assertTrue(set.add("D"));
1719
assertEquals("[A, B, C, D]", set.toString());
1820
}
21+
22+
@Test
23+
void testComparable() {
24+
class Student implements Comparable<Student> {
25+
private final String name;
26+
private final int age;
27+
28+
public Student(String name, int age) {
29+
this.name = name;
30+
this.age = age;
31+
}
32+
33+
public String getName() {
34+
return name;
35+
}
36+
37+
public int getAge() {
38+
return age;
39+
}
40+
41+
@Override
42+
public String toString() {
43+
return "Student{" +
44+
"name='" + name + '\'' +
45+
", age=" + age +
46+
'}';
47+
}
48+
49+
@Override
50+
public int compareTo(Student o) {
51+
return this.getAge() - o.getAge();
52+
}
53+
}
54+
55+
Student s1 = new Student("Jack", 23);
56+
Student s2 = new Student("Tom", 22);
57+
Student s3 = new Student("Zara", 21);
58+
59+
Set<Student> set = new TreeSet<>();
60+
set.add(s1);
61+
set.add(s2);
62+
set.add(s3);
63+
64+
assertEquals("[Student{name='Zara', age=21}, Student{name='Tom', age=22}, Student{name='Jack', age=23}]"
65+
, set.toString());
66+
}
67+
68+
@Test
69+
void testComparator() {
70+
class Student{
71+
private final String name;
72+
private final int age;
73+
74+
public Student(String name, int age) {
75+
this.name = name;
76+
this.age = age;
77+
}
78+
79+
public String getName() {
80+
return name;
81+
}
82+
83+
public int getAge() {
84+
return age;
85+
}
86+
@Override
87+
public String toString() {
88+
return "Student{" +
89+
"name='" + name + '\'' +
90+
", age=" + age +
91+
'}';
92+
}
93+
}
94+
95+
Student s1 = new Student("Jack", 23);
96+
Student s2 = new Student("Tom", 22);
97+
Student s3 = new Student("Zara", 21);
98+
99+
Set<Student> set = new TreeSet<>((o1, o2) -> o1.getAge() - o2.getAge());
100+
set.add(s1);
101+
set.add(s2);
102+
set.add(s3);
103+
104+
assertEquals("[Student{name='Zara', age=21}, Student{name='Tom', age=22}, Student{name='Jack', age=23}]"
105+
, set.toString());
106+
}
19107
}

0 commit comments

Comments
 (0)