一.概念
1.Collection
java.util.Collection 是一个集合接口(集合类的一个顶级接口)。它提供了对集合对象进行基本操作的通用接口方法。Collection接口在Java 类库中有很多具体的实现。Collection接口的意义是为各种具体的集合提供了最大化的统一操作方式,其直接继承接口有List与Set。
2.Collections
java.util.Collections 是一个包装类(工具类/帮助类)。它包含有各种有关集合操作的静态多态方法。此类不能实例化,就像一个工具类,用于对集合中元素进行排序、搜索以及线程安全等各种操作,服务于Java的Collection框架。
二.Collections工具类常用方法
1. void reverse(List list):反转集合
package com.example.mytest;
import android.os.Bundle;
import android.util.Log;
import androidx.appcompat.app.AppCompatActivity;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initCollections();
}
public void initCollections() {
ArrayList<String> arrayList = new ArrayList<>();
arrayList.add("aaa");
arrayList.add("bbb");
arrayList.add("ccc");
Iterator<String> iterator = arrayList.iterator();
while (iterator.hasNext()) {
Log.d("TAG", iterator.next() + "");
}
Log.d("TAG", "********************************* reverse方法反转集合后*********************************");
Collections.reverse(arrayList);
Iterator<String> iterators = arrayList.iterator();
while (iterators.hasNext()) {
Log.d("TAG", iterators.next() + "");
}
}
}
D/TAG: aaa
D/TAG: bbb
D/TAG: ccc
D/TAG: ********************************* reverse方法反转集合后*********************************
D/TAG: ccc
D/TAG: bbb
D/TAG: aaa
2.void shuffle(List list):随机排序集合
package com.example.mytest;
import android.os.Bundle;
import android.util.Log;
import androidx.appcompat.app.AppCompatActivity;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initCollections();
}
public void initCollections() {
ArrayList<String> arrayList = new ArrayList<>();
arrayList.add("aaa");
arrayList.add("bbb");
arrayList.add("张三");
arrayList.add("ccc");
arrayList.add("123");
Iterator<String> iterator = arrayList.iterator();
while (iterator.hasNext()) {
Log.d("TAG", iterator.next() + "");
}
Log.d("TAG", "***************************** 随机排序集合后****************************");
Collections.shuffle(arrayList);
Iterator<String> iterators = arrayList.iterator();
while (iterators.hasNext()) {
Log.d("TAG", iterators.next() + "");
}
}
}
结果1:随机
D/TAG: aaa
D/TAG: bbb
D/TAG: 张三
D/TAG: ccc
D/TAG: 123
D/TAG: ***************************** 随机排序集合后****************************
D/TAG: 张三
D/TAG: ccc
D/TAG: 123
D/TAG: bbb
D/TAG: aaa
结果2:随机
D/TAG: aaa
D/TAG: bbb
D/TAG: 张三
D/TAG: ccc
D/TAG: 123
D/TAG: ***************************** 随机排序集合后****************************
D/TAG: aaa
D/TAG: ccc
D/TAG: bbb
D/TAG: 张三
D/TAG: 123
结果...
3.void sort(List list):按自然排序的升序排序
package com.example.mytest;
import android.os.Bundle;
import android.util.Log;
import androidx.appcompat.app.AppCompatActivity;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initCollections();
}
public void initCollections() {
ArrayList<String> arrayList = new ArrayList<>();
arrayList.add("ccc");
arrayList.add("bbb");
arrayList.add("111");
arrayList.add("aaa");
arrayList.add("56");
Iterator<String> iterator = arrayList.iterator();
while (iterator.hasNext()) {
Log.d("TAG", iterator.next() + "");
}
Log.d("TAG", "********** sort方法自然排序的升序排序集合后*****************");
Collections.sort(arrayList);
Iterator<String> iterators = arrayList.iterator();
while (iterators.hasNext()) {
Log.d("TAG", iterators.next() + "");
}
}
}
D/TAG: ccc
D/TAG: bbb
D/TAG: 111
D/TAG: aaa
D/TAG: 56
D/TAG: ********** sort方法自然排序的升序排序集合后*****************
D/TAG: 111
D/TAG: 56
D/TAG: aaa
D/TAG: bbb
D/TAG: ccc
4.void swap(List list, int i , int j):交换两个索引位置的元素。
package com.example.mytest;
import android.os.Bundle;
import android.util.Log;
import androidx.appcompat.app.AppCompatActivity;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initCollections();
}
public void initCollections() {
ArrayList<String> arrayList = new ArrayList<>();
arrayList.add("aaa");
arrayList.add("bbb");
arrayList.add("ccc");
Iterator<String> iterator = arrayList.iterator();
while (iterator.hasNext()) {
Log.d("TAG", iterator.next() + "");
}
Log.d("TAG", "***************** swap交换两个索引位置后*********************");
Collections.swap(arrayList, 0, 2);
Iterator<String> iterators = arrayList.iterator();
while (iterators.hasNext()) {
Log.d("TAG", iterators.next() + "");
}
}
}
D/TAG: aaa
D/TAG: bbb
D/TAG: ccc
D/TAG: ***************** swap交换两个索引位置后*********************
D/TAG: ccc
D/TAG: bbb
D/TAG: aaa
5.rotate(List list, int distance):旋转。当distance为正数时,将list后distance个元素整体移到前面。当distance为负数时,将 list的前distance个元素整体移到后面。
package com.example.mytest;
import android.os.Bundle;
import android.util.Log;
import androidx.appcompat.app.AppCompatActivity;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initCollections();
}
public void initCollections() {
ArrayList<String> arrayList = new ArrayList<>();
arrayList.add("aaa");
arrayList.add("bbb");
arrayList.add("ccc");
arrayList.add("ddd");
arrayList.add("eee");
Iterator<String> iterator = arrayList.iterator();
while (iterator.hasNext()) {
Log.d("TAG", iterator.next() + "");
}
Log.d("TAG", "****************** rotate方法旋转集合后***********************");
Collections.rotate(arrayList, 2);
Iterator<String> iterators = arrayList.iterator();
while (iterators.hasNext()) {
Log.d("TAG", iterators.next() + "");
}
}
}
D/TAG: aaa
D/TAG: bbb
D/TAG: ccc
D/TAG: ddd
D/TAG: eee
D/TAG: ****************** rotate方法旋转集合后***********************
D/TAG: ddd
D/TAG: eee
D/TAG: aaa
D/TAG: bbb
D/TAG: ccc
如果
Collections.rotate(arrayList,-2);
D/TAG: aaa
D/TAG: bbb
D/TAG: ccc
D/TAG: ddd
D/TAG: eee
D/TAG: ****************** rotate方法旋转集合后***********************
D/TAG: ccc
D/TAG: ddd
D/TAG: eee
D/TAG: aaa
D/TAG: bbb
6.void sort(List list, Comparator c):定制排序,由Comparator控制排序逻辑。
Java Bean
package com.example.mytest;
public class Person {
private String id;
private String name;
private String starttime;
private String age;
private String describe;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDescribe() {
return describe;
}
public void setDescribe(String describe) {
this.describe = describe;
}
public String getStarttime() {
return starttime;
}
public void setStarttime(String starttime) {
this.starttime = starttime;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
}
Comparator实现类
package com.example.mytest;
import java.util.Comparator;
public class MyComparator implements Comparator<Person> {
@Override
public int compare(Person person, Person t1) {
int flag = person.getAge().compareTo(t1.getAge());
/**
* return -1 表示放在红黑树的左边,即逆序输出
* return 1 表示放在红黑树的右边,即顺序输出
* return 0 表示元素相同,仅存放第一个元素
* */
if (flag > 0) {
return -1;
} else if (flag == 0) {
return 0;
} else {
return 1;
}
}
}
Java代码使用
package com.example.mytest;
import android.os.Bundle;
import android.util.Log;
import androidx.appcompat.app.AppCompatActivity;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initCollections();
}
public void initCollections() {
ArrayList<Person> arrayList = new ArrayList<Person>();
Person person1 = new Person();
person1.setName("张三");
person1.setAge("2018-01-02");
Person person2 = new Person();
person2.setName("李四");
person2.setAge("2018-02-02");
Person person3 = new Person();
person3.setName("王五");
person3.setAge("2018-03-02");
Person person4 = new Person();
person4.setName("赵云");
person4.setAge("2017-01-02");
Person person5 = new Person();
person5.setName("关羽");
person5.setAge("2016-01-02");
Person person6 = new Person();
person6.setName("小强");
person6.setAge("2018-08-02");
arrayList.add(person1);
arrayList.add(person2);
arrayList.add(person3);
arrayList.add(person4);
arrayList.add(person5);
arrayList.add(person6);
Iterator<Person> iterator = arrayList.iterator();
while (iterator.hasNext()) {
Person person = iterator.next();
String name = person.getName();
String age = person.getAge();
Log.d("TAG", "name----:" + name + "----age----:" + age);
}
MyComparator myComparator = new MyComparator();
Collections.sort(arrayList, myComparator);
Log.d("TAG", "**********排序后************");
Iterator<Person> iterators = arrayList.iterator();
while (iterators.hasNext()) {
Person person = iterators.next();
String name = person.getName();
String age = person.getAge();
Log.d("TAG", "name----:" + name + "----age----:" + age);
}
}
}
结果:(时间倒序)
D/TAG: name----:张三----age----:2018-01-02
D/TAG: name----:李四----age----:2018-02-02
D/TAG: name----:王五----age----:2018-03-02
D/TAG: name----:赵云----age----:2017-01-02
D/TAG: name----:关羽----age----:2016-01-02
D/TAG: name----:小强----age----:2018-08-02
D/TAG: **********排序后************
D/TAG: name----:小强----age----:2018-08-02
D/TAG: name----:王五----age----:2018-03-02
D/TAG: name----:李四----age----:2018-02-02
D/TAG: name----:张三----age----:2018-01-02
D/TAG: name----:赵云----age----:2017-01-02
D/TAG: name----:关羽----age----:2016-01-02
如果
package com.example.mytest;
import java.util.Comparator;
public class MyComparator implements Comparator<Person> {
@Override
public int compare(Person person, Person t1) {
int flag = person.getAge().compareTo(t1.getAge());
/**
* return -1 表示放在红黑树的左边,即逆序输出
* return 1 表示放在红黑树的右边,即顺序输出
* return 0 表示元素相同,仅存放第一个元素
* */
if (flag > 0) {
return 1;
} else if (flag == 0) {
return 0;
} else {
return -1;
}
}
}
结果:(时间正序)
D/TAG: name----:张三----age----:2018-01-02
D/TAG: name----:李四----age----:2018-02-02
D/TAG: name----:王五----age----:2018-03-02
D/TAG: name----:赵云----age----:2017-01-02
D/TAG: name----:关羽----age----:2016-01-02
D/TAG: name----:小强----age----:2018-08-02
D/TAG: **********排序后************
D/TAG: name----:关羽----age----:2016-01-02
D/TAG: name----:赵云----age----:2017-01-02
D/TAG: name----:张三----age----:2018-01-02
D/TAG: name----:李四----age----:2018-02-02
D/TAG: name----:王五----age----:2018-03-02
D/TAG: name----:小强----age----:2018-08-02
7.特殊需求
1.需求描述
在操作多选题目时,例如标准答案是 abcd。用户在点击时可能先选了d又选了a... 最后操作的顺序是dabc 这样就要将用户选择的dabc改变顺序成abcd,然后再和标准答案比对。
2.代码
package com.example.mytest;
import android.os.Bundle;
import android.util.Log;
import androidx.appcompat.app.AppCompatActivity;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initCollections();
}
public void initCollections() {
ArrayList<String> arrayList = new ArrayList<String>();
arrayList.add("D");
arrayList.add("A");
arrayList.add("B");
arrayList.add("C");
Iterator<String> iterator = arrayList.iterator();
while (iterator.hasNext()) {
Log.d("TAG", iterator.next() + "");
}
Log.d("TAG", "***List按String.CASE_INSENSITIVE_ORDER排序(adc->abc)***");
Collections.sort(arrayList, String.CASE_INSENSITIVE_ORDER);
Iterator<String> iterators = arrayList.iterator();
while (iterators.hasNext()) {
Log.d("TAG", iterators.next() + "");
}
}
}
即:Collections.sort(answerlist, String.CASE_INSENSITIVE_ORDER);
3.结果
D/TAG: D
D/TAG: A
D/TAG: B
D/TAG: C
D/TAG: ***List按String.CASE_INSENSITIVE_ORDER排序(adc->abc)***
D/TAG: A
D/TAG: B
D/TAG: C
D/TAG: D