-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathLinkedHashSetDemo1.java
50 lines (45 loc) · 1.39 KB
/
LinkedHashSetDemo1.java
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
package Advances.CollectionDemo;
// https://www.youtube.com/watch?v=z8nlH3W1sBU&list=PLmOn9nNkQxJH0qBIrtV6otI0Ep4o2q67A&index=538
/** LinkedHashSet demo 1 */
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.Set;
import org.junit.jupiter.api.Test;
public class LinkedHashSetDemo1 {
@Test
public void test1() {
/**
* LinkedHashSet demo 1
*
* <p>1) LinkedHashSet is HashSet's sub class
*
* <p>2) maintain 2 references when adding new element : pre element and next element
*
* <p>3) pros : good performance in looping, adding, deletion
*
* <p>4) cons :
*/
Set set = new LinkedHashSet();
set.add(456);
set.add(123);
set.add(123); // only storage one "123", since HashSet is non-duplicated
set.add("aa");
set.add("bb");
set.add(new Person("tim", 11));
set.add(
new Person(
"tim",
11)); // NOTE : will storage 2 "Person("tim",11)" since we haven't overwrote "equals",
// "hashCode" methods in Person class
set.add(new User("ann", 20));
set.add(
new User(
"ann",
20)); // NOTE : only storage 1 "User("ann",20)" if we overwrite "equals", "hashCode"
// methods in User class
Iterator iterator = set.iterator();
while (iterator.hasNext()) {
System.out.println(iterator.next());
}
}
}