目录
1.Collections常用方法
2.自然排序
3.整体排序
4.Iterator
1.Collections常用方法
addAll(Collection super T> c, T... elements) | 将所有指定元素添加到指定collection中 |
reverse(List> list) | 反转指定列表中元素的顺序 |
shuffle(List> list) | 使用默认随机源对指定列表进行置换 |
sort(List |
根据元素的自然顺序对指定列表进行排序 |
sort(List |
根据指定比较器对指定列表进行排序 |
2.自然排序
Collections提供两种排序方法,自然排序和整体排序.自然排序方法要求list中的元素实现Comparable
public class CollectionsDemo2 { public static void main(String[] args) { ArrayListlist1 = new ArrayList<>(); Collections.addAll(list1, 2, 4, 6, 8, 0, 1, 23, 45); Collections.sort(list1); System.out.println(list1); } }
运行结果:[0, 1, 2, 4, 6, 8, 23, 45]
public class CollectionsDemo2 { public static void main(String[] args) { ArrayListlist2 = new ArrayList<>(); Person[] people = { new Person("zhangsan"), new Person("lisi"), new Person("wanglaowu") }; Collections.addAll(list2, people); Collections.sort(list2); System.out.println(list2); } } class Person implements Comparable { String name; public Person(String name) { this.name = name; } // 根据名字排序 @Override public int compareTo(Person p) { return this.name.compareTo(p.name); } @Override public String toString() { return name; } }
运行结果:[lisi, wanglaowu, zhangsan]
3.整体排序
整体排序方法要求在sort方法中传入一个Comparator
public class CollectionsDemo3 { public static void main(String[] args) { ArrayListlist3 = new ArrayList<>(); Person[] people = { new Person("zhangsan", 40), new Person("lisi", 25), new Person("wanglaowu", 32), new Person("kobe", 25), new Person("james", 40) }; Collections.addAll(list3, people); // 通过匿名内部类实现对Person的比较 Collections.sort(list3, new Comparator () { // 按年龄从小到大,年龄相同的按名字排序 public int compare(Person p1, Person p2) { int rtv = 0; if ((rtv = p1.age - p2.age) == 0) { rtv = p1.name.compareTo(p2.name); } return rtv; } }); System.out.println(list3); } } class Person { String name; int age; public Person(String name, int age) { this.name = name; this.age = age; } @Override public String toString() { return age + ":" + name; } }
运行结果:[25:kobe, 25:lisi, 32:wanglaowu, 40:james, 40:zhangsan]
4.Iterator
1)常用方法
hasNext() | 判断是否仍有元素可以迭代 |
next() | 返回迭代的下一个元素 |
remove() | 从集合中移除迭代器返回的最后一个元素 |
forEachRemaining() | 对未进行迭代的元素执行一个action(1.8新增) |
2)筛选元素
public class IteratorDemo { public static void main(String[] args) { ArrayListlist = new ArrayList<>(); Collections.addAll(list, 95, 34, 67, 89, 23, 79, 66, 12, 98); Iterator car = list.iterator(); while (car.hasNext()) { int value = car.next(); if (value < 60) { car.remove(); } } System.out.println(list); } }
运行结果:[95, 67, 89, 79, 66, 98]
3)forEachRemaining()
对于forEachRemaining()方法,迭代器会对集合中所有未进行迭代的元素进行迭代,相当于利用for each循环语句.在执行此方法时,不能调用remove()方法对当前collection的元素进行删除,同时,执行此方法后,hasNext()的返回值应该为false.
public class IteratorDemo { public static void main(String[] args) { ArrayListlist = new ArrayList<>(); Collections.addAll(list, 95, 34, 67, 89, 23, 79, 66, 12, 98); Iterator car = list.iterator(); // 利用lambda表达式实现Consumer car.forEachRemaining((value) -> { if (value > 60) { // car.remove();//抛出异常 System.out.println(value); } }); System.out.println(car.hasNext()); } }
运行结果:
95
67
89
79
66
98
false
注意,我们只是打印时忽略了小于60的分数,而list内存储的元素并未发生改变.