ClickHouse的物化檢視及MySQL表引擎
阿新 • • 發佈:2020-08-26
Collections 工具類提供的幾個常用的方法:addAll ,shuffle,sort
下面是例項:
package com.collectiondo;
import java.util.ArrayList;
import java.util.Collections;
//測試Collections 工具類
public class TestCollectionUtil {
public static void main(String[] args) {
//測試addAll
ArrayList<String> alist = new ArrayList<>();
Collections.addAll(alist, "a","b","e","g","c","d");
System.out.println("alist:"+alist);
//打亂順序
Collections.shuffle(alist);
System.out.println("打亂順序後的alist:"+alist);
//sort排序
ArrayList<Integer> blist = new ArrayList<>();
blist.add(1);
blist.add(2);
blist.add( 3);
blist.add(4);
blist.add(5);
Collections.sort(blist);
Collections.sort(alist);
System.out.println("排序後的alist:"+alist);
System.out.println("排序後的blist:"+blist);
}
}
輸出的結果是:
alist:[a, b, e, g, c, d]
打亂順序後的alist:[b, g, a, c, e, d]
排序後的alist:[a, b, c, d, e, g]
排序後的blist:[1, 2, 3, 4, 5]
排序的方法的擴充套件:
一般資料的排序:
package com.collectiondo;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
//測試一般型別的排序和物件型別的排序
public class TestCollectionSort {
public static void main(String[] args) {
ArrayList<Integer> ilist=new ArrayList<>();
ilist.add(1);
ilist.add(12);
ilist.add(221);
ilist.add(4);
ilist.add(9);
ilist.add(543);
System.out.println("排序前ilist:"+ilist);
Collections.sort(ilist, new Comparator<Integer>() {
//重寫比較較規則
@Override
public int compare(Integer o1, Integer o2) {
// return o1-o2; 升序
return o2-o1; //降序
}
});
System.out.println("排序後ilist:"+ilist);
}
}
輸出的結果是:
排序前ilist:[1, 12, 221, 4, 9, 543]
排序後ilist:[543, 221, 12, 9, 4, 1]
物件型別的排序:
package com.collectiondo;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
//物件型集合的排序
class Student{
private Integer age;
private String name;
public Integer getAge() {
return age;
}
public String getName() {
return name;
}
public Student(Integer age, String name) {
this.age = age;
this.name = name;
}
@Override
public String toString() {
return "Student{" +
"age=" + age +
", name='" + name + '\'' +
'}';
}
}
public class TestPojoSort {
public static void main(String[] args) {
ArrayList<Student> stuList=new ArrayList();
Student stu1=new Student(19,"路飛");
Student stu2=new Student(17,"索隆");
Student stu3=new Student(21,"娜美");
Student stu4=new Student(1,"喬巴");
Collections.addAll(stuList,stu1,stu2,stu3,stu4);
System.out.println("排序前:"+stuList);
//現在要讓集合按照物件中的年齡來排序
Collections.sort(stuList, new Comparator<Student>() {
@Override
public int compare(Student o1, Student o2) {
return o1.getAge()-o2.getAge();
}
});
//排序後的學生列表
System.out.println("排序後:"+stuList);
}
}
輸出結果:
排序前:[Student{age=19, name='路飛'}, Student{age=17, name='索隆'}, Student{age=21, name='娜美'}, Student{age=1, name='喬巴'}]
排序後:[Student{age=1, name='喬巴'}, Student{age=17, name='索隆'}, Student{age=19, name='路飛'}, Student{age=21, name='娜美'}]
package com.collectiondo;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
//物件型集合的排序
class Student{
private Integer age;
private String name;
public Integer getAge() {
return age;
}
public String getName() {
return name;
}
public Student(Integer age, String name) {
this.age = age;
this.name = name;
}
@Override
public String toString() {
return "Student{" +
"age=" + age +
", name='" + name + '\'' +
'}';
}
}
public class TestPojoSort {
public static void main(String[] args) {
ArrayList<Student> stuList=new ArrayList();
Student stu1=new Student(19,"路飛");
Student stu2=new Student(17,"索隆");
Student stu3=new Student(21,"娜美");
Student stu4=new Student(1,"喬巴");
Collections.addAll(stuList,stu1,stu2,stu3,stu4);
System.out.println("排序前:"+stuList);
//現在要讓集合按照物件中的年齡來排序
Collections.sort(stuList, new Comparator<Student>() {
@Override
public int compare(Student o1, Student o2) {
return o1.getAge()-o2.getAge();
}
});
//排序後的學生列表
System.out.println("排序後:"+stuList);
}
}