Collections工具类中常用的方法
COllections工具类
java.util.Collections 是对集合进行操作的工具类,里边含有大量的静态方法
addAll()和shuffle()方法
前者是往集合中添加一些元素(多个),后者是将集合中元素的顺序打乱。类似于纸牌游戏的洗牌功能
import java.util.ArrayList;
import java.util.Collections;
/**
* Collections 工具类
* public static <T> boolean addAll(Collection<T> , T...elements):往集合中添加元素
* public static shuffle( List <?> list)打乱顺序:打乱集合顺序
*
* @author HolyTrees
* @date 2020-10-21 下午 14:45
*/
public class DemoCollections {
public static void main(String[] args) {
ArrayList<String> list = new ArrayList<>();
/**
* 往集合中添加元素
*/
Collections.addAll(list,"a","b","c","d","e");
System.out.println("初始元素顺序的集合");
System.out.println(list);
System.out.println();
/**
* 打乱集合中元素的顺序
*/
Collections.shuffle(list);
System.out.println("打乱元素顺序后的集合");
System.out.println(list);
System.out.println();
/**
* 将指定的元素和参数元素进行替换
*/
Collections.replaceAll(list,"a","k");
System.out.println("元素替换后的集合");
System.out.println(list);
}
}
输出结果
初始元素顺序的集合
[a, b, c, d, e]
打乱元素顺序后的集合
[e, a, c, b, d]
元素替换后的集合
[e, k, c, b, d]
sort()方法
用于给集合中的元素进行排序
public static void sort(List list):将集合中的元素按照默认规则排序
如果要用自定义的对象进行排序,那么该类要继承Comparable接口,并且要重写接口中的CompareTo方法;才能实现对自定义的对象进行排序;
自定义一个CollectionPerson类,继承Comparable接口,重写compareTo方法
重写toString方法():否则打印集合得到的是集合中元素的地址值
/**
* 用自定义的类型进行排序
* @author HolyTrees
* @date 2020-10-21 下午 15:06
*/
public class CollectionPerson implements Comparable<CollectionPerson>{
/**
* 定义成员变量 name age
*/
private String name;
private int age;
public CollectionPerson() {
}
public CollectionPerson(String name, int age) {
this.name = name;
this.age = age;
}
/**
* 获取方法
* @return
*/
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
/**
* 重写toString方法
* @return
*/
@Override
public String toString() {
return "CollectionPerson{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
/**
* 重写的compareTo方法
* @param o
* @return
*/
@Override
public int compareTo(CollectionPerson o) {
/**
* return 0;认为两个参数相同
* 自定义比较规则,比较两个人的年龄(this,参数CollectionPerson)
* return this.getAge()-o.getAge() 升序排序
* return o.getAge() - this.getAge() 降序排序
*/
return o.getAge()-this.getAge();
}
}
下面进行测试
import java.util.ArrayList;
import java.util.Collections;
/**
* public static <T> boolean addAll(Collection<T> , T...elements):往集合中添加元素
* public static shuffle( List <?> list)打乱顺序:打乱集合顺序
* public static <T> void sort(list <T> list):将集合按照默认规则排序;
* sort使用前提:被排序的集合里面存储的元素必须实现Comparable,重写接口中的方法compareTo方法定义的规则;
* CompareTo方法排序规则:
* (this). - 参数:升序
* 反之则降序
*
* @author HolyTrees
* @date 2020-10-21 下午 14:58
*/
public class DemoCollectionsSort {
public static void main(String[] args) {
ArrayList<String>list = new ArrayList<>();
/**
* 添加元素
*/
Collections.addAll(list,"a","c","d","e","f");
System.out.println(list);
/**
* 打乱元素顺序
*/
System.out.println("打乱元素顺序");
Collections.shuffle(list);
System.out.println(list);
/**
* 默认顺序排序
*/
Collections.sort(list);
System.out.println("默认方式排序:");
System.out.println(list);
System.out.println();
/**
* 自定义的类型进行排序
*/
ArrayList<CollectionPerson> personList = new ArrayList<>();
personList.add(new CollectionPerson("张三",18));
personList.add(new CollectionPerson("李四",20));
personList.add(new CollectionPerson("王五",19));
System.out.println("集合初始顺序");
System.out.println(personList);
System.out.println();
System.out.println("采用自定义降序排序:");
Collections.sort(personList);
System.out.println(personList);
}
}
输出结果:
[a, c, d, e, f]
打乱元素顺序
[d, f, e, c, a]
默认方式排序:
[a, c, d, e, f]
集合初始顺序
[CollectionPerson{name='张三', age=18}, CollectionPerson{name='李四', age=20}, CollectionPerson{name='王五', age=19}]
采用自定义降序排序:
[CollectionPerson{name='李四', age=20}, CollectionPerson{name='王五', age=19}, CollectionPerson{name='张三', age=18}]
public static void sort(List list,Comparator<? super T>):将集合中的元素按照指定规则排序;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
/**
* @author HolyTrees
* @date 2020-10-21 下午 16:01
*/
public class DemoWork {
public static void main(String[] args) {
ArrayList<Integer>list = new ArrayList<>();
/**
* 添加元素
*/
Collections.addAll(list,51,22,46,93,72,19);
System.out.println(list);
System.out.println();
/**
* 打乱顺序
*/
Collections.shuffle(list);
System.out.println("打乱顺序后的集合");
System.out.println(list);
System.out.println();
/**
* 升序排序
*/
Collections.sort(list);
System.out.println("升序排序");
System.out.println(list);
System.out.println();
/**
* 降序排序,使用匿名对象重写此方法
*/
Collections.sort(list, new Comparator<Integer>() {
/**
* o2 - o1降序
* o1 - o2升序
* @param o1
* @param o2
* @return
*/
@Override
public int compare(Integer o1, Integer o2) {
return o2 - o1;
}
});
/**
* 重写后的降序排序
*/
System.out.println("采用重写后的方法排序");
System.out.println(list);
}
}
[51, 22, 46, 93, 72, 19]
打乱顺序后的集合
[22, 93, 72, 51, 19, 46]
升序排序
[19, 22, 46, 51, 72, 93]
采用重写后的方法排序
[93, 72, 51, 46, 22, 19]