Skip to content

Commit 84fb2ff

Browse files
author
deleiguo
committed
update
1 parent 0bb45d9 commit 84fb2ff

File tree

5 files changed

+132
-5
lines changed

5 files changed

+132
-5
lines changed

doc/jdk/src/java/util/LinkedList.md

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -173,16 +173,14 @@ public E getLast() {
173173

174174
/** 查找相应位置的 Node 对象 */
175175
Node<E> node(int index) {
176-
// size >> 1 每次最坏情况下遍历链表中一半的元素
176+
// size >> 1 每次最好的情况下只需遍历链表中一半的元素
177177
if (index < (size >> 1)) {
178-
// 如果下标小于元素数量的一半
179178
Node<E> x = first;
180179
// 从first开始遍历
181180
for (int i = 0; i < index; i++)
182181
x = x.next;
183182
return x;
184183
} else {
185-
// 如果小标不小于元素数量的一半
186184
Node<E> x = last;
187185
// 从last开始遍历
188186
for (int i = size - 1; i > index; i--)
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package cn.delei.java.util;
2+
3+
import cn.delei.pojo.Person;
4+
import cn.delei.util.PrintUtil;
5+
6+
import java.util.HashSet;
7+
8+
/**
9+
* HashSet Demo
10+
*
11+
* @author deleiguo
12+
*/
13+
public class HashSetDemo {
14+
public static void main(String[] args) {
15+
add();
16+
}
17+
18+
static void add() {
19+
HashSet<Integer> hashSet = new HashSet<>();
20+
hashSet.add(null);
21+
hashSet.add(1);
22+
hashSet.add(2);
23+
hashSet.add(1);
24+
hashSet.add(null);
25+
hashSet.add(10);
26+
hashSet.forEach(System.out::println);
27+
PrintUtil.printDivider();
28+
HashSet<Person> personHashSet = new HashSet<>();
29+
30+
personHashSet.add(new Person("deleiguo", 28, "111111"));
31+
personHashSet.add(new Person("deleiguo", 28, "222222"));
32+
personHashSet.add(new Person("delei", 28, "222222"));
33+
personHashSet.add(new Person("guo", 28, "111111"));
34+
personHashSet.forEach(System.out::println);
35+
}
36+
}

source/java/interview/src/main/java/cn/delei/java/util/LinkedListDemo.java

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,56 @@
88

99
/**
1010
* java.util.LinkedList
11+
*
1112
* @author deleiguo
1213
*/
1314
public class LinkedListDemo {
1415

1516
public static LinkedList<Integer> dataList = new LinkedList<>();
17+
1618
public static void main(String[] args) {
17-
opera();
19+
// add();
20+
get();
21+
// opera();
22+
}
23+
24+
25+
static void add() {
26+
StopWatch stopWatch = new StopWatch();
27+
stopWatch.start("add");
28+
final int size = 10;
29+
dataList = new LinkedList<>();
30+
// 头插
31+
for (int i = 0; i < size; i++) {
32+
dataList.addFirst(i);
33+
}
34+
// 尾插
35+
for (int i = 0; i < size; i++) {
36+
dataList.add(i + 10);
37+
}
38+
System.out.println(dataList);
39+
40+
stopWatch.stop();
41+
System.out.println(stopWatch.prettyPrint());
42+
}
43+
44+
static void get() {
45+
StopWatch stopWatch = new StopWatch();
46+
47+
final int size = 100000;
48+
dataList = new LinkedList<>();
49+
// 头插
50+
for (int i = 0; i < size; i++) {
51+
dataList.add(i);
52+
}
53+
stopWatch.start("index < (size>>1)");
54+
System.out.println(dataList.get(size / 2 - 50));
55+
stopWatch.stop();
56+
57+
stopWatch.start("index > (size>>1)");
58+
System.out.println(dataList.get(size / 2 + 50));
59+
stopWatch.stop();
60+
System.out.println(stopWatch.prettyPrint());
1861
}
1962

2063
/**

source/java/interview/src/main/java/cn/delei/pojo/Person.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package cn.delei.pojo;
22

33
import java.io.Serializable;
4+
import java.util.Objects;
5+
import java.util.Optional;
46

57
/**
68
* POJO 类
@@ -60,4 +62,28 @@ public String toString() {
6062
", password='" + password + '\'' +
6163
'}';
6264
}
65+
66+
@Override
67+
public int hashCode() {
68+
int nameHash = name.toUpperCase().hashCode();
69+
return nameHash ^ age;
70+
}
71+
72+
@Override
73+
public boolean equals(Object obj) {
74+
if (Objects.isNull(obj)) {
75+
return false;
76+
}
77+
if (this == obj) {
78+
return true;
79+
}
80+
81+
if (obj instanceof Person) {
82+
final Person p = (Person) obj;
83+
return this.getName().equals(p.getName())
84+
&& this.getAge() == p.getAge();
85+
}
86+
87+
return false;
88+
}
6389
}

source/java/interview/src/main/java/cn/delei/pojo/Teacher.java

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package cn.delei.pojo;
22

33
import java.io.Serializable;
4+
import java.util.Objects;
45

56
/**
67
* POJO 类:老师
@@ -17,7 +18,8 @@ public class Teacher implements Serializable {
1718
*/
1819
private int age;
1920

20-
public Teacher(){}
21+
public Teacher() {
22+
}
2123

2224
public Teacher(String name, int age) {
2325
this.name = name;
@@ -47,4 +49,26 @@ public String toString() {
4749
", age=" + age +
4850
'}';
4951
}
52+
53+
@Override
54+
public int hashCode() {
55+
int nameHash = name.toUpperCase().hashCode();
56+
return nameHash ^ age;
57+
}
58+
59+
@Override
60+
public boolean equals(Object obj) {
61+
if (Objects.isNull(obj)) {
62+
return false;
63+
}
64+
if (this == obj) {
65+
return true;
66+
}
67+
if (obj instanceof Teacher) {
68+
final Teacher p = (Teacher) obj;
69+
return p.getName().equals(this.getName())
70+
&& p.getAge() == this.getAge();
71+
}
72+
return false;
73+
}
5074
}

0 commit comments

Comments
 (0)