Skip to content

Commit 9883173

Browse files
committed
修改格式
1 parent 614cfb8 commit 9883173

22 files changed

Lines changed: 554 additions & 356 deletions

src/main/java/basic/collection/TestHashCode.java

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -54,21 +54,24 @@ public int hashCode() {
5454
*/
5555
@Override
5656
public boolean equals(Object obj) {
57-
if (this == obj)
57+
if (this == obj) {
5858
return true;
59-
if (obj == null)
59+
}
60+
if (obj == null) {
6061
return false;
61-
if (getClass() != obj.getClass())
62+
}
63+
if (getClass() != obj.getClass()) {
6264
return false;
65+
}
6366
Car other = (Car) obj;
64-
if (cost != other.cost)
67+
if (cost != other.cost) {
6568
return false;
69+
}
6670
if (name == null) {
67-
if (other.name != null)
68-
return false;
69-
} else if (!name.equals(other.name))
70-
return false;
71-
return true;
71+
return other.name == null;
72+
} else {
73+
return name.equals(other.name);
74+
}
7275
}
7376

7477
}

src/main/java/basic/collection/comparable/Apple.java

Lines changed: 26 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,34 +2,35 @@
22

33
public class Apple implements Comparable<Apple> {
44

5-
private int count;
5+
private int count;
66

7-
public Apple(int count) {
8-
this.count = count;
9-
}
7+
public Apple(int count) {
8+
this.count = count;
9+
}
1010

11-
@Override
12-
public int compareTo(Apple apple) {
13-
if (apple.getCount() < count) {
14-
return -1;
15-
} else if (apple.getCount() > count) {
16-
return 1;
17-
} else {
18-
return 0;
19-
}
20-
}
11+
@Override
12+
public int compareTo(Apple apple) {
13+
if (apple.getCount() < count) {
14+
return -1;
15+
} else if (apple.getCount() > count) {
16+
return 1;
17+
} else {
18+
return 0;
19+
}
20+
}
2121

22-
public int getCount() {
23-
return count;
24-
}
22+
public int getCount() {
23+
return count;
24+
}
2525

26-
public String toString() {
27-
return new Integer(getCount()).toString();
28-
}
26+
@Override
27+
public String toString() {
28+
return new Integer(getCount()).toString();
29+
}
2930

30-
public static void main(String[] args) {
31-
Apple apple1 = new Apple(1);
32-
Apple apple2 = new Apple(2);
33-
System.out.println(apple1.compareTo(apple2));
34-
}
31+
public static void main(String[] args) {
32+
Apple apple1 = new Apple(1);
33+
Apple apple2 = new Apple(2);
34+
System.out.println(apple1.compareTo(apple2));
35+
}
3536
}
Lines changed: 27 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,35 @@
11
package basic.collection.comparable;
22

3-
import java.util.ArrayList;
4-
import java.util.Arrays;
5-
import java.util.Collections;
6-
import java.util.Comparator;
7-
import java.util.List;
3+
import java.util.*;
84

5+
/**
6+
* 比较器
7+
*
8+
* @author yangqc
9+
*/
910
public class MyComparator implements Comparator<Apple> {
1011

11-
@Override
12-
public int compare(Apple apple1, Apple apple2) {
13-
if (apple1.getCount() < apple2.getCount()) {
14-
return -1;
15-
} else if (apple1.getCount() > apple2.getCount()) {
16-
return 1;
17-
}
18-
return 0;
19-
}
12+
@Override
13+
public int compare(Apple apple1, Apple apple2) {
14+
if (apple1.getCount() < apple2.getCount()) {
15+
return -1;
16+
} else if (apple1.getCount() > apple2.getCount()) {
17+
return 1;
18+
}
19+
return 0;
20+
}
2021

21-
public static void main(String[] args) {
22-
Apple apple1 = new Apple(1);
23-
Apple apple2 = new Apple(2);
24-
MyComparator myComparator = new MyComparator();
25-
System.out.println(myComparator.compare(apple1, apple2));
26-
List<Apple> appleList = new ArrayList<>();
27-
for (int i = 0; i < 10; i++) {
28-
appleList.add(new Apple(10 - i));
29-
}
30-
Collections.sort(appleList, myComparator);
31-
System.out.println(Arrays.toString(appleList.toArray()));
32-
}
22+
public static void main(String[] args) {
23+
Apple apple1 = new Apple(1);
24+
Apple apple2 = new Apple(2);
25+
MyComparator myComparator = new MyComparator();
26+
System.out.println(myComparator.compare(apple1, apple2));
27+
List<Apple> appleList = new ArrayList<>();
28+
for (int i = 0; i < 10; i++) {
29+
appleList.add(new Apple(10 - i));
30+
}
31+
Collections.sort(appleList, myComparator);
32+
System.out.println(Arrays.toString(appleList.toArray()));
33+
}
3334

3435
}

src/main/java/basic/collection/map/AssociativeArray.java

Lines changed: 51 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -2,65 +2,64 @@
22

33
/**
44
* ��ά����ʵ��Map,���dz��ȹ̶������Ҳ���Ч�ʲ���
5-
*
6-
* @author yangqc
7-
*
5+
*
86
* @param <K>
97
* @param <V>
8+
* @author yangqc
109
*/
1110
public class AssociativeArray<K, V> {
12-
private Object[][] pairs;
13-
private int index;
11+
private Object[][] pairs;
12+
private int index;
1413

15-
public AssociativeArray(int length) {
16-
pairs = new Object[length][2];
17-
}
14+
public AssociativeArray(int length) {
15+
pairs = new Object[length][2];
16+
}
1817

19-
public void put(K key, V value) {
20-
if (index >= pairs.length) {
21-
throw new ArrayIndexOutOfBoundsException();
22-
}
23-
pairs[index++] = new Object[] { key, value };
24-
}
18+
public void put(K key, V value) {
19+
if (index >= pairs.length) {
20+
throw new ArrayIndexOutOfBoundsException();
21+
}
22+
pairs[index++] = new Object[]{key, value};
23+
}
2524

26-
@SuppressWarnings("unchecked")
27-
public V get(K key) {
28-
for (int i = 0; i < index; i++) {
29-
if (key.equals(pairs[i][0])) {
30-
return (V) pairs[i][1];
31-
}
32-
}
33-
return null;
34-
}
25+
@SuppressWarnings("unchecked")
26+
public V get(K key) {
27+
for (int i = 0; i < index; i++) {
28+
if (key.equals(pairs[i][0])) {
29+
return (V) pairs[i][1];
30+
}
31+
}
32+
return null;
33+
}
3534

36-
public String toString() {
37-
StringBuilder result = new StringBuilder();
38-
for (int i = 0; i < index; i++) {
39-
result.append(pairs[i][0].toString());
40-
result.append(" : ");
41-
result.append(pairs[i][1].toString());
42-
if (i < index - 1) {
43-
result.append("\n");
44-
}
45-
}
46-
return result.toString();
47-
}
35+
@Override
36+
public String toString() {
37+
StringBuilder result = new StringBuilder();
38+
for (int i = 0; i < index; i++) {
39+
result.append(pairs[i][0].toString());
40+
result.append(" : ");
41+
result.append(pairs[i][1].toString());
42+
if (i < index - 1) {
43+
result.append("\n");
44+
}
45+
}
46+
return result.toString();
47+
}
4848

49-
public static void main(String[] args) {
50-
AssociativeArray<String, String> map = new AssociativeArray<String, String>(
51-
6);
52-
map.put("sky", "bule");
53-
map.put("grass", "green");
54-
map.put("ocean", "dancing");
55-
map.put("tree", "tail");
56-
map.put("earth", "brown");
57-
map.put("sun", "warm");
58-
try {
59-
map.put("extra", "object");
60-
} catch (ArrayIndexOutOfBoundsException e) {
61-
System.out.println("Too mmany objects!");
62-
}
63-
System.out.println(map);
64-
System.out.println(map.get("ocean"));
65-
}
49+
public static void main(String[] args) {
50+
AssociativeArray<String, String> map = new AssociativeArray<>(6);
51+
map.put("sky", "bule");
52+
map.put("grass", "green");
53+
map.put("ocean", "dancing");
54+
map.put("tree", "tail");
55+
map.put("earth", "brown");
56+
map.put("sun", "warm");
57+
try {
58+
map.put("extra", "object");
59+
} catch (ArrayIndexOutOfBoundsException e) {
60+
System.out.println("Too mmany objects!");
61+
}
62+
System.out.println(map);
63+
System.out.println(map.get("ocean"));
64+
}
6665
}

src/main/java/basic/collection/map/FailFast.java

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,26 +4,27 @@
44
import java.util.ConcurrentModificationException;
55
import java.util.Iterator;
66
import java.util.List;
7+
78
/**
8-
*
99
* ����:����IJ�ͬ�����޸�����ʱ�����ܻᵼ������״̬��һ�£����Իᱨ��
10-
* @author yangqc
1110
*
11+
* @author yangqc
12+
* <p>
1213
* 2016-6-15
1314
*/
1415
public class FailFast {
15-
public static void main(String[] args) {
16-
List<String> c=new ArrayList<>();
17-
c.add("An Object");
18-
System.out.println(c.size());
19-
Iterator<String> it=c.iterator();
20-
c.remove(0);
21-
c.add(0, "123");
22-
System.out.println(c.size());
23-
try{
24-
String s=it.next();
25-
}catch(ConcurrentModificationException e){
26-
System.out.println(e);
27-
}
28-
}
16+
public static void main(String[] args) {
17+
List<String> c = new ArrayList<>();
18+
c.add("An Object");
19+
System.out.println(c.size());
20+
Iterator<String> it = c.iterator();
21+
c.remove(0);
22+
c.add(0, "123");
23+
System.out.println(c.size());
24+
try {
25+
String s = it.next();
26+
} catch (ConcurrentModificationException e) {
27+
System.out.println(e);
28+
}
29+
}
2930
}
Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,20 @@
11
package basic.collection.map;
2+
23
/**
34
* Object��ɢ�����ǰ���ַ���ɵ�
5+
*
46
* @author yangqc
57
* 2016.6.14
68
*/
79
public class Groundhog {
8-
protected int number;
9-
public Groundhog(int n){
10-
number=n;
11-
}
12-
public String toString(){
13-
return "Groundhog #"+number;
14-
}
10+
protected int number;
11+
12+
public Groundhog(int n) {
13+
number = n;
14+
}
15+
16+
@Override
17+
public String toString() {
18+
return "Groundhog #" + number;
19+
}
1520
}
Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,25 @@
11
package basic.collection.map;
2+
23
/**
3-
*
44
* ����: ��Ҫͬʱ����hashCode�Լ�equals����
5-
* @author yangqc
65
*
6+
* @author yangqc
7+
* <p>
78
* 2016-6-14
89
*/
9-
public class Groundhog2 extends Groundhog{
10+
public class Groundhog2 extends Groundhog {
11+
12+
public Groundhog2(int n) {
13+
super(n);
14+
}
1015

11-
public Groundhog2(int n) {
12-
super(n);
13-
}
16+
@Override
17+
public int hashCode() {
18+
return number;
19+
}
1420

15-
public int hashCode(){
16-
return number;
17-
}
18-
19-
public boolean equals(Object obj){
20-
return obj instanceof Groundhog2 && (number==((Groundhog2)obj).number);
21-
}
21+
@Override
22+
public boolean equals(Object obj) {
23+
return obj instanceof Groundhog2 && (number == ((Groundhog2) obj).number);
24+
}
2225
}

0 commit comments

Comments
 (0)