-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontact.java
More file actions
110 lines (93 loc) · 3.26 KB
/
Copy pathcontact.java
File metadata and controls
110 lines (93 loc) · 3.26 KB
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
import java.util.*;
class Student implements Comparable<Student>{
private String name;
private int age;
Student(String name, int age){
this.name = name;
this.age = age;
}
public String getName(){
return name; // do not set print function here.
}
public int getAge(){
return age;
}
@Override
public int hashCode(){ // privilege should larger/equal than father class
return name.hashCode() + 20 * age;// so, public should be save.
}
@Override
public boolean equals(Object o){
if(!(o instanceof Student)){//check if o the object of Student
throw new ClassCastException("Exception: non compatible type.");
}// this RunTimeException is this exception's father calss.
Student s = (Student) o;//if safe, then cast.
return this.name.equals(s.name) && this.age == s.age;
}
public int compareTo(Student stu){
int num;
num = new Integer(this.age).compareTo(new Integer(stu.age));
//use Integer.compareTo can return the specific value we need.
//simply use "=" compare should write more codes.
if(num == 0)//check in different level.
return this.name.compareTo(stu.name);
return num;
}
/**
* to String is defined in Object class,
* just like hashCode and equals.
* So we can override directly.
*/
@Override
public String toString(){
return name + ":" + age;
}
}
/*
*if we cannot modify compare function in Student class inside,
*we can use the Comparator interface
* */
class newComparator implements Comparator<Student>{
public int compare(Student A, Student B){
int num = A.getName().compareTo(B.getName());
if(num == 0){
return new Integer(A.getAge()).compareTo(new Integer(B.getAge()));
}
return num;
}
}
public class contact{
public static void main(String[] args){
/*
HashMap<Student, String> map = new HashMap<Student, String>();
map.put(new Student("aaaa",12), "Beijing");
map.put(new Student("bbbb",12), "Guangzhou");
map.put(new Student("dddd",16), "Zhuzhou");
map.put(new Student("cccc",11), "Shanghai");
map.put(new Student("aaaa",19), "Hongkong");
Set<Student> ks = map.keySet();
Iterator<Student> it = ks.iterator();
*/
TreeMap<Student, String> tmap = new TreeMap<>(new newComparator());
tmap.put(new Student("aaaa",12), "Beijing");
tmap.put(new Student("bbbb",12), "Guangzhou");
tmap.put(new Student("dddd",16), "Zhuzhou");
tmap.put(new Student("cccc",11), "Shanghai");
tmap.put(new Student("aaaa",19), "Hongkong");
Set<Map.Entry<Student, String>> es = tmap.entrySet();
Iterator<Map.Entry<Student, String>> it = es.iterator();
/*
while(it.hasNext()){
Student stu = it.next();
String location = map.get(stu);
System.out.println(stu +" --------- "+location);
}
*/
while(it.hasNext()){
Map.Entry<Student, String> me = it.next();
Student stu = me.getKey();
String addr = me.getValue();
System.out.println(stu+" ::: "+ addr);
}
}
}