public
interface
Comparable<T> {
public
int
compareTo(T o);
}
|
类
|
排序
|
BigDecimal,BigInteger,Byte,Double, Float,Integer,Long,Short
|
按数字大小排序
|
Character
|
按
Unicode
值的数字大小排序
|
String
|
按字符串中字符
Unicode
值排序
|
package
com.zj.sort.comparable;
public
class
Person
implements
Comparable<Person> {
private
int
age
;
private
String
name
;
public
Person(
int
age, String name) {
this
.
age
= age;
this
.
name
= name;
}
public
int
compareTo(Person person) {
int
cop =
age
- person.getAge();
if
(cop != 0)
return
cop;
else
return
name
.compareTo(person.
name
);
}
public
int
getAge() {
return
age
;
}
public
String getName() {
return
name
;
}
public
int
hashCode() {
int
result = 17;
result = 37 * result +
age
;
result = 37 * result +
name
.hashCode();
return
result;
}
public
boolean
equals(Object o) {
if
(!(o
instanceof
Person))
return
false
;
Person person = (Person) o;
return
(
age
== person.
age
) && (
name
.equals(person.
name
));
}
public
String toString() {
return
(
age
+
"{"
+
name
+
"}"
);
}
}
|
package
com.zj.sort.comparable;
import
java.util.Arrays;
import
com.zj.compare.Person;
public
class
ArraysSortUnit {
public
static
void
main(String[] args) {
Person[] ps = {
new
Person(20,
"Tom"
),
new
Person(20,
"Jeff"
),
new
Person(30,
"Mary"
),
new
Person(20,
"Ada"
),
new
Person(40,
"Walton"
),
new
Person(61,
"Peter"
),
new
Person(20,
"Bush"
) };
System.
out
.println(Arrays.toString (ps));
Arrays.sort (ps);
System.
out
.println(Arrays.toString (ps));
}
}
|
package
com.zj.sort.comparable;
import
java.util.Arrays;
import
java.util.Collections;
import
com.zj.compare.Person;
public
class
CollctionsSortUnit {
public
static
void
main(String[] args) {
Person[] ps = {
new
Person(20,
"Tom"
),
new
Person(20,
"Jeff"
),
new
Person(30,
"Mary"
),
new
Person(20,
"Ada"
),
new
Person(40,
"Walton"
),
new
Person(61,
"Peter"
),
new
Person(20,
"Bush"
) };
System.
out
.println(Arrays.toString (ps));
Collections.sort (Arrays.asList (ps));
System.
out
.println(Arrays.toString (ps));
}
}
|
package
com.zj.sort.comparable;
import
java.util.TreeSet;
import
com.zj.compare.Person;
public
class
TreeSetUnit {
public
static
void
main(String[] args) {
TreeSet<Person> set =
new
TreeSet<Person>();
set.add(
new
Person(20,
"Tom"
));
set.add(
new
Person(20,
"Jeff"
));
set.add(
new
Person(30,
"Mary"
));
set.add(
new
Person(20,
"Ada"
));
set.add(
new
Person(40,
"Walton"
));
set.add(
new
Person(61,
"Peter"
));
set.add(
new
Person(20,
"Bush"
));
System.
out
.println(set);
}
}
|
package
com.zj.sort.comparable;
import
java.util.TreeMap;
import
com.zj.compare.Person;
public
class
TreeMapUnit {
public
static
void
main(String[] args) {
TreeMap<Person, String> map =
new
TreeMap<Person, String>();
map.put(
new
Person(20,
"Tom"
),
"Tom"
);
map.put(
new
Person(20,
"Jeff"
),
"Jeff"
);
map.put(
new
Person(30,
"Mary"
),
"Mary"
);
map.put(
new
Person(20,
"Ada"
),
"Ada"
);
map.put(
new
Person(40,
"Walton"
),
"Walton"
);
map.put(
new
Person(61,
"Peter"
),
"Peter"
);
map.put(
new
Person(20,
"Bush"
),
"Bush"
);
System.
out
.println(map);
}
}
|
public
interface
Comparator<T> {
int
compare(T o1, T o2);
boolean
|