File tree Expand file tree Collapse file tree 5 files changed +132
-5
lines changed
source/java/interview/src/main/java/cn/delei Expand file tree Collapse file tree 5 files changed +132
-5
lines changed Original file line number Diff line number Diff line change @@ -173,16 +173,14 @@ public E getLast() {
173173
174174/* * 查找相应位置的 Node 对象 */
175175Node<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-- )
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 88
99/**
1010 * java.util.LinkedList
11+ *
1112 * @author deleiguo
1213 */
1314public 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 /**
Original file line number Diff line number Diff line change 11package cn .delei .pojo ;
22
33import 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}
Original file line number Diff line number Diff line change 11package cn .delei .pojo ;
22
33import 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}
You can’t perform that action at this time.
0 commit comments