## 数组和集合排序总结 原

韩毅Evan

1：数组排序

`````` String[] str = new String[]{“china”,”guangzhou”,”beijing”};
System.out.println(Arrays.toString(str));
Arrays.sort(str);
System.out.println(Arrays.toString(str)); ``````

2：数组对象里属性排序
2.1 使用Comparable接口

``````public classs Student implements Comparable<Student>{
private int id;
private int score;
@Override
public int compareTo(Student stu) {
return Integer.valueOf(stu.score).compareTo(Integer.valueOf(this.score));
}
}
Student[] stus = new Student[]{
new Student("1001",90),
new Student("1002",70),
new Student("1003",100),
new Student("1004",50),
}
Arrays.sort(stus);
``````

2.2 使用Comparator接口

``````public classs Student{
private int id;
private int score;
}
Student[] stus = new Student[]{
new Student("1001",90),
new Student("1002",70),
new Student("1003",100),
new Student("1004",50),
}
Arrays.sort(stus, new Comparator<Student>() {
@Override
public int compare(Student stu1, Student stu2) {
return Integer.valueOf(stu1.score).compareTo(Integer.valueOf(stu2.score));
}
});``````

================分隔线========================

1：集合排序
Collections.sort():

``````List<String> list = new ArrayList<String>();
System.out.println(list.toString());
Collections.sort(list);
System.out.println(list);``````

1.1：使用Comparator接口排序，同1

``````List<String> list = new ArrayList<String>();
System.out.println("原順序："+list);
Collections.sort(list, new Comparator<String>() {
@Override
public int compare(String str1, String str2) {
return str1.compareTo(str2);//-1、0、1
}
});
System.out.println("自定義順序："+list);
Collections.reverse(list);
System.out.println("然後再反序："+list);
``````

2：集合里的对象属性排序
2.1 使用Comparator接口

``````public classs Student{
private int id;
private int score;
}
List<Student> list = new ArrayList<Student>();
System.out.println("原順序："+list);
Collections.sort(list, new Comparator<Student>() {
@Override
public int compare(Student stu1, Student stu2) {
return Integer.valueOf(stu1.score).compareTo(Integer.valueOf(stu2.score));
}
});
System.out.println("自定義順序："+list);
Collections.reverse(list);
System.out.println("然後再反序："+list);``````

2.2 Comparable

``````public classs Student implements Comparable<Student>{
private int id;
private int score;
@Override
public int compareTo(Student stu) {
return Integer.valueOf(stu.score).compareTo(Integer.valueOf(this.score));
}
}
List<Student> list = new ArrayList<Student>();
System.out.println("原順序："+list);
Collections.sort(list);
System.out.println("自定義順序："+list);
``````

``````public classs Student implements Comparable<Student>{
private int id;
private int score;
@Override
public int compareTo(Student stu) {
return Integer.valueOf(stu.score).compareTo(Integer.valueOf(this.score));
}
}

TreeSet ts = new TreeSet<Student>();

compareTo方法原理

if(A<B){
ruturn -1;
}else if(A==B){
return 0;
}else{
return 1;
}

A<B?-1:(A>B?1:0)

A.compareTo(B)

Comparator对代码没有任何污染，耦合性低
Comparable对代码有污染，耦合性高

### 韩毅Evan

Java Collection、Map集合总结

1.Java中的Collection集合总结 Collection 　　|---List（存储有序，可重复） 　　　　|---ArrayList 　　　　　　底层数据结构是数组，查询快，增删慢。 　　　　　　线程不安全、效率高 　...

hafiz.zhang
2016/05/02
0
0
JavaScript实现flatten多种方法

asyncnode
09/12
0
0

2017/12/07
0
0
Java面试相关---集合

2016/08/08
0
0
【JAVA】集合框架 学习笔记

qq_37792173
2017/09/11
0
0

20分钟前
2
0

33分钟前
2
0
JS其他类型值转化为Boolean类型规则

44分钟前
4
0
EurekaClient自动装配及启动流程解析

Java学习录
50分钟前
9
0

p517 在C++中，基类指针可以指向一个派生类的对象。如果基类的析构函数不是虚函数，当需要delete这个指向派生类的基类指针时，就只会调用基类的析构函数，而派生类的析构函数无法被调用。容易...

51分钟前
5
0