一. sort
对集合进行排序
小结:
1.String本身含有compareTo方法,可以直接调用sort方法,按自然顺序排序
2.compareTo方法使用,升序排序(参数左比右),降序排序(参数右比左);另外compareTo方法里面传入参数类型是Integer类型的比较内容
3.使用sort方法进行规则比较,都需要实现Comparator,只是方式不同
实践代码:
private void test() {
//排序字符串集合
List<String> listS = new ArrayList<>();
listS.add("2");
listS.add("5");
listS.add("3");
listS.add("7");
listS.add("4");
//listS中的对象String本身含有compareTo方法,所以可以直接调用sort方法,按自然顺序排序
Collections.sort(listS);
Log.d("TAG", "listS:" + listS);//[2, 3, 4, 5, 7]
//排序对象元素
List<Employer1> list1 = new ArrayList<Employer1>();
Employer1 a1 = new Employer1();
Employer1 b1 = new Employer1();
Employer1 c1 = new Employer1();
a1.setName("a1");
a1.setAge(44);
b1.setName("b1");
b1.setAge(55);
c1.setName("b1");
c1.setAge(33);
list1.add(a1);
list1.add(b1);
list1.add(c1);
Collections.sort(list1, new MyCompare());
//[name is b1 age is 33, name is a1 age is 44, name is b1 age is 55]
Log.d("TAG", "list1:" + list1);
List<Employer2> list2 = new ArrayList<Employer2>();
Employer2 a2 = new Employer2();
Employer2 b2 = new Employer2();
Employer2 c2 = new Employer2();
a2.setName("a2");
a2.setAge(66);
b2.setName("b2");
b2.setAge(33);
c2.setName("b2");
c2.setAge(22);
list2.add(a2);
list2.add(b2);
list2.add(c2);
Collections.sort(list2, new Comparator<Employer2>() {
@Override
public int compare(Employer2 o1, Employer2 o2) {
//比较规则,年龄比较,降序排序(右比左)
//注意:compareTo方法里面传入Integer类型的比较内容
return o2.getOrder().compareTo(o1.getOrder());
}
});
//[name is a2 age is 66, name is b2 age is 33, name is b2 age is 22]
Log.d("TAG", "list2:" + list2);
List<Employer3> list3 = new ArrayList<Employer3>();
Employer3 a3 = new Employer3();
Employer3 b3 = new Employer3();
Employer3 c3 = new Employer3();
a3.setName("a3");
a3.setAge(77);
b3.setName("b3");
b3.setAge(55);
c3.setName("b3");
c3.setAge(99);
list3.add(a3);
list3.add(b3);
list3.add(c3);
Collections.sort(list3);
//[name is b3 age is 55, name is a3 age is 77, name is b3 age is 99]
Log.d("TAG", "list3:" + list3);
}
class Employer1 {
private String name;
private Integer age;
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
//重载了Object类里的toString方法,使之可以按照我们要求的格式打印
@Override
public String toString() {
return "name is " + name + " age is " + age;
}
}
//比较类
class MyCompare implements Comparator<Employer1> {
@Override
public int compare(Employer1 o1, Employer1 o2) {
//比较规则,年龄比较,升序排序(左比右)
return o1.getAge().compareTo(o2.getAge());
}
}
class Employer2 {
private String name;
private Integer age;
public void setName(String name) {
this.name = name;
}
public Integer getOrder() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
//重载了Object类里的toString方法,使之可以按照我们要求的格式打印
@Override
public String toString() {
return "name is " + name + " age is " + age;
}
}
class Employer3 implements Comparable<Employer3> {
private String name;
private Integer age;
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
//重载了Object类里的toString方法,使之可以按照我们要求的格式打印
@Override
public String toString() {
return "name is " + name + " age is " + age;
}
//重载了Comparable接口里的compareTo方法来实现具体的比较
@Override
public int compareTo(Employer3 a) {//第二个参数(右)
return this.age.compareTo(a.getAge());
}
}
补充:关于String的compareTo方法比较使用
compareTo方法返回值是整型,在字符串比较时它会先比较对应字符的大小(ASCII码顺序),直到比较的字符的ASCII码不相同,则结束比较,返回他们之间对应ASCII码的差值(左边减去右边),如果第一个字符和参数的第一个字符相等,则以第二个字符和参数的第二个字符作比较,以此类推,直到比较的字符或被比较的字符有一方全比较完,这时就比较字符的长度,左边字符串长度减去右边字符串长度就是返回值。
实践代码:
String s1 = "abc";
String s2 = "abcd";
String s3 = "abcdfg";
String s4 = "1bcdfg";
String s5 = "cdfg";
String s6 = "abc";
String s7 = "accc";
Log.d("TAG", "s1.compareTo(s2):" + s1.compareTo(s2));// -1 (前面相等,s1长度小1)
Log.d("TAG", "s1.compareTo(s3):" + s1.compareTo(s3));// -3 (前面相等,s1长度小3)
Log.d("TAG", "s1.compareTo(s4):" + s1.compareTo(s4));// 48 ("a"的ASCII码是97,"1"的的ASCII码是49,所以返回48)
Log.d("TAG", "s1.compareTo(s5):" + s1.compareTo(s5));// -2 ("a"的ASCII码是97,"c"的ASCII码是99,所以返回-2)
Log.d("TAG", "s1.compareTo(s6):" + s1.compareTo(s6));// 0
Log.d("TAG", "s1.compareTo(s7):" + s1.compareTo(s7));// -1
二.shuffle
对集合进行随机排序
(有可能排序过的集合元素位置不变)
public void shuffle(){
List c = new ArrayList();
c.add("w");
c.add("o");
c.add("r");
c.add("l");
c.add("d");
Log.d("TAG", "初始集合:"+c);//[w, o, r, l, d]
Collections.shuffle(c);
Log.d("TAG", "随机一集合:"+c);//[l, o, d, w, r]
Collections.shuffle(c);
Log.d("TAG", "随机二集合:"+c);//[r, w, o, l, d]
}
三.binarySearch
查找指定集合中的元素,返回所查找元素的索引
public void practice() {
List c = new ArrayList();
c.add("w");
c.add("o");
c.add("r");
c.add("l");
c.add("d");
Log.d("TAG", "初始集合:"+c);//[w, o, r, l, d]
int m = Collections.binarySearch(c, "l");
Log.d("TAG", "找元素的索引:"+m);//-1
}
四.max
前者采用Collection内含自然比较法,后者采用Comparator进行比较
public static <T extends Object & Comparable<? super T>> T max(Collection<? extends T> coll)
public static <T> T max(Collection<? extends T> coll, Comparator<? super T> comp)
五.min
前者采用Collection内含自然比较法,后者采用Comparator进行比较
public static <T extends Object & Comparable<? super T>> T min(Collection<? extends T> coll)
public static <T> T min(Collection<? extends T> coll, Comparator<? super T> comp)
六.indexOfSubList
查找subList在list中首次出现位置的索引
public void indexOfSubList() {
List list = Arrays.asList("one two three four five six siven".split(" "));
Log.d("TAG", "初始集合:"+list);//[one, two, three, four, five, six, siven]
List subList = Arrays.asList("three four five six".split(" "));
Log.d("TAG", "子集合:"+subList);//[three, four, five, six]
Log.d("TAG", "子集合首次出现位置的索引:"+Collections.indexOfSubList(list, subList));//2
}
六.lastIndexOfSubList
查找subList在list中最后出现位置的索引
public void lastIndexOfSubList() {
List list = Arrays.asList("one two three four five six siven one two three four five six siven".split(" "));
Log.d("TAG", "初始集合:"+list);//[one, two, three, four, five, six, siven, one, two, three, four, five, six, siven]
List subList = Arrays.asList("three four five six".split(" "));
Log.d("TAG", "子集合:"+subList);//[three, four, five, six]
Log.d("TAG", "子集合首次出现位置的索引:"+Collections.indexOfSubList(list, subList));//2
Log.d("TAG", "子集合最后出现位置的索引:"+Collections.lastIndexOfSubList(list, subList));//9
}
八.replaceAll
替换指定元素为某元素,若要替换的值(旧元素)存在刚返回true,反之返回false
public void replaceAll(){
List list = Arrays.asList("old one two three four old five six siven old".split(" "));
Log.d("TAG", "初始集合:"+list);//[old, one, two, three, four, old, five, six, siven, old]
Log.d("TAG", "替换值是否存在:"+Collections.replaceAll(list, "old", "two"));//true
Log.d("TAG", "替换后集合:"+list);//[new, one, two, three, four, new, five, six, siven, new]
}
九.reverse
反转集合中元素的顺序
public void reverse() {
List list = Arrays.asList("one two three four five six siven".split(" "));
Log.d("TAG", "初始集合:" + list);//[one, two, three, four, five, six, siven]
Collections.reverse(list);
Log.d("TAG", "反转后集合:" + list);//[siven, six, five, four, three, two, one]
}
十.rotate
集合中的元素向后移m个位置,在后面被遮盖的元素循环到前面来
public void rotate() {
List list = Arrays.asList("one two three four five six siven".split(" "));
Log.d("TAG", "初始集合:" + list);//[one, two, three, four, five, six, siven]
Collections.rotate(list, 2);
Log.d("TAG", "旋转后集合:" + list);//[six, siven, one, two, three, four, five]
}
十一.copy
将集合n中的元素全部复制到m中,并且覆盖相应索引的元素
(从0开始覆盖,后面的元素向后移)
public void copy() {
List m = Arrays.asList("one two three four five six siven".split(" "));
Log.d("TAG", "初始集合:" + m);//[one, two, three, four, five, six, siven]
List n = Arrays.asList("我是 复制 过来的哈".split(" "));
Log.d("TAG", "待复制集合:" + n);//[我是, 复制, 过来的哈]
Collections.copy(m, n);
Log.d("TAG", "复制后集合:" + m);//[我是, 复制, 过来的哈, four, five, six, siven]
}
十二.swap
交换集合中指定元素索引的位置
public void swap() {
List m = Arrays.asList("one two three four five six siven".split(" "));
Log.d("TAG", "初始集合:" + m);//[one, two, three, four, five, six, siven]
Collections.swap(m, 2, 3);
Log.d("TAG", "交换后集合:" + m);//[one, two, four, three, five, six, siven]
}
十三.fill
用对象o替换集合list中的所有元素
public void fill() {
List m = Arrays.asList("one two three four five six siven".split(" "));
Log.d("TAG", "初始集合:" + m);//[one, two, three, four, five, six, siven]
Collections.fill(m, "haha12345");
Log.d("TAG", "替换后集合:" + m);//[haha12345, haha12345, haha12345, haha12345, haha12345, haha12345, haha12345]
}
十四.nCopies
返回大小为n的List,List不可改变,其中的所有引用都指向o
public void nCopies() {
Log.d("TAG", "" + Collections.nCopies(5, "haha"));//[haha, haha, haha, haha, haha]
}