- Collections工具类
- 增强for循环
- 泛型
- 红黑树
- Set集合
public static void shuffle(List<?> list):打乱集合顺序。public static <T> void sort(List<T> list):将集合中元素按照默认规则排序。public static <T> void sort(List<T> list,Comparator<? super T> ):将集合中元素按照指定规则排序。
/**
* java.util.Collections集合操作的工具类
* 主要对Collection集合操作(单列集合)
* 方法:
* static shuffle(List list)元素随机的排列
* static sort(List list)对List集合进行排序,升序
* static sort(List list,Comparator c)对List集合进行排序,升序
* 第二个参数比较器,根据比较器的结果,进行排序
*/
public static void main(String[] args) {
ArrayList<Integer> list = new ArrayList<Integer>();
list.add(100);
list.add(300);
list.add(200);
list.add(50);
//排序方法
Collections.sort(list);
System.out.println(list);
}创建一个学生类,存储到ArrayList集合中完成指定排序操作。
Student 类
public class Student{
private String name;
private int age;
//构造方法
//get/set
//toString
}接口实现类
/**
* 自定义接口Comparator的实现类
* 实现接口,重写抽象方法,创建Person对象的比较器
*/
public class MyComparator implements Comparator<Student>{
/**
* 重写接口中的抽象方法
* 返回值是int,方法的参数是 Student o1, Student o2
* o2对象-o1对象,如果是正数,大的在前面
*/
public int compare(Student o1,Student o2){
//o1是李四,o2是张三
//张三的年龄-李四的年龄
// 谁的年龄大,谁在前面
return o2.getAge() - o1.getAge();// 正数
}
}测试类
public static void main(String[] args) {
ArrayList<Student> list = new ArrayList<Student>();
list.add(new Student("rose",18));
list.add(new Student("jack",16));
list.add(new Student("abc",20));
/**
* 使用工具类Collections方法sort对集合排序
* 集合存储了自定义的对象,Student
* sort:
* 对集合中的元素排序
* 对象Student无法排序导致
* 自定义Student的顺序
* sort方法的第二个参数 Comparator类型的对象
* 传递 Comparator接口的实现类对象
*/
Collections.sort(list, new MyComparator());
}
for (Student stu : list) {
System.out.println(student);
}
}
Student{name='jack', age=16}
Student{name='rose', age=18}
Student{name='abc', age=20}格式
for(元素的数据类型 变量 : Collection集合or数组){
//写操作代码
}注意:它用于遍历Collection和数组。通常只进行遍历元素,不能在遍历的过程中对集合元素进行增删改操作。
*Iterable子接口Collection*
*Collection子接口,List Set*
*任何单列集合,都可以使用增强for循环,包含数组*
示例代码
public static void main(String[] args) {
/**
* 增强for循环遍历数组
* 思考问题:
* 好处: 减少代码量,适合遍历
* 弊端: 不能动容器中的任何元素
*/
int[] arr = {3,5,6,87};
//使用增强for遍历数组
for(int a : arr){//a代表数组中的每个元素
System.out.println(a);
}
/**
* 增强for循环遍历集合
*/
Collection<String> coll = new ArrayList<String>();
coll.add("小河神");
coll.add("老河神");
coll.add("神婆");
for(String s :coll){
System.out.println(s);
}
/**
* 增强for循环遍历集合
* 自定义对象
*/
public static void method_3(){
List<Person> list = new ArrayList<Person>();
list.add(new Person("张三",15));
list.add(new Person("李四",19));
for (Person p:list){
System.out.println(p);
}
}- 将运行时期的ClassCastException,转移到了编译时期变成了编译失败。
- 避免了类型强转的麻烦。
- 泛型,只在编译的时候有效,编译成功的class文件中,没有泛型 (伪泛型)
格式
集合类<要存储的数据类型> 变量名 = new 集合类<要存储的数据类型>();例如,ArrayList<String> list = new ArrayList<String>();
此时,变量E的值就是String类型,那么我们的类型就可以理解为:
class ArrayList<String>{
public boolean add(String e){ }
public String get(int index){ }
...
}再例如,ArrayList<Integer> list = new ArrayList<Integer>();
此时,变量E的值就是Integer类型,那么我们的类型就可以理解为:
class ArrayList<Integer> {
public boolean add(Integer e) { }
public Integer get(int index) { }
...
}当使用泛型类或者接口时,传递的数据中,泛型类型不确定,可以通过通配符<?>表示。但是一旦使用泛型的通配符后,只能使用Object类中的共性方法,集合中元素自身方法无法使用。
泛型的通配符:不知道使用什么类型来接收的时候,此时可以使用?,?表示未知通配符。
此时只能接受数据,不能往该集合中存储数据。
举个例子:
public static void main(String[] args) {
Collection<Intger> list1 = new ArrayList<Integer>();
getElement(list1);
Collection<String> list2 = new ArrayList<String>();
getElement(list2);
}
//?为通配符,可以接收任意类型
public static void getElement(Collection<?> coll){
Iterator<?> it = coll.iterator();
while(it.hasNext()){
System.out.println(it.next());
}
}注意:泛型不存在继承关系 Collection list = new ArrayList();这种是错误的。
之前设置泛型的时候,实际上是可以任意设置的,只要是类就可以设置。但是在JAVA的泛型中可以指定一个泛型的上限和下限。
- 格式:
类型名称 <? extends 类 > 对象名称 - 意义:
只能接收该类型及其子类
- 格式:
类型名称 <? super 类 > 对象名称 - 意义:
只能接收该类型及其父类型
举例
需求:创建老师类和班主任类,提供姓名和年龄属性,并都具有work方法。将多个老师对象和多个班主任对象存储到两个集合中。提供一个方法可以同时遍历这两个集合,并能调用work方法。
Employee员工类:
public abstract class Employee {
private String name;
private int age;
public Employee() {
}
public Employee(String name, int age) {
this.name = name;
this.age = age;
}
//省略get/set
public abstract void work();
}Teacher类:
public class Teacher extends Employee{
public Teacher() {
}
public Teacher(String name, int age) {
super(name, age);
}
@Override
public void work() {
System.out.println("老师在上课");
}
}Manager类:
public class Manager extends Employee {
public Manager() {
}
public Manager(String name, int age) {
super(name, age);
}
@Override
public void work() {
System.out.println("班主任管理班级");
}
}测试类:
public static void main(String[] args) throws UnsupportedEncodingException {
List<Teacher> teacherList = new ArrayList<Teacher>();
teacherList.add(new Teacher("张三",30));
teacherList.add(new Teacher("李四",32));
List<Manager> managerList = new ArrayList<Manager>();
managerList.add(new Manager("王五",25));
managerList.add(new Manager("赵六",23));
getElement(teacherList);
getElement(managerList);
}
//定义方法,参数为List集合,泛型被限定为Employee,可以接收的泛型为Employee或者子类!
public static void getElement(List<? extends Employee> list){
Iterator<? extends Employee> it = list.iterator();
while (it.hasNext()){
Employee employee = it.next();
System.out.println(employee.getName()+"::"+employee.getAge());
employee.work();
}
}Set集合: 不包含重复的元素,没有索引
Set接口的实现类 HashSet, LinkedHashSet
Set接口中的抽象方法,和他的父接口Collection完全一样
Set集合有多个子类,这里我们介绍其中的java.util.HashSet、java.util.LinkedHashSet这两个集合。
注意:集合取出元素的方式可以采用:迭代器、增强for
哈希表存储采用数组+链表+红黑树实现,当链表长度超过阈值(8)时,将链表转换为红黑树,这样大大减少了查找时间。
给HashSet中存放自定义类型元素时,需要重写对象中的hashCode和equals方法,建立自己的比较方式,才能保证HashSet集合中的对象唯一.
创建自定义Student类:
public class Student {
private String name;
private int age;
//get/set
//可以直接生成
@Override
public boolean equals(Object o) {
if (this == o)
return true;
if (o == null || getClass() != o.getClass())
return false;
Student student = (Student) o;
return age == student.age &&
Objects.equals(name, student.name);
}
@Override
public int hashCode() {
return Objects.hash(name, age);
}
}创建测试类:
public static void main(String[] args) {
//创建集合对象 该集合中存储 Student类型对象
HashSet<Student> stuSet = new HashSet<Student>();
//存储
stuSet.add(new Student("于谦", 43));
stuSet.add(new Student("郭德纲", 44));
stuSet.add(new Student("于谦", 43));
stuSet.add(new Student("郭麒麟", 23));
for (Student stu2 : stuSet) {
System.out.println(stu2);
}
}