//1.Collections工具类
主要是为List集合服务,进行相关的List功能处理
//注意: Collection与Collections要区分开
//扩展了解复合泛型
//>: 所使用的T类型必须要实现Comparable
//>:所使用的T类型或T的父亲实现Comparable接口
//例如:此处List存Integer类型,定义复合类型说明Integer或父亲必须实现Comparable
class Student{
int age;
public Student(int age) {
this.age=age;
}
}
public class Test1 {
public static void main(String[] args) {
List list = new ArrayList<>();
list.add(3);
list.add(1);
list.add(2);
list.add(4);
Collections.reverse(list); //集合的反转
Collections.shuffle(list); //随机重置集合
System.out.println(list);
Collections.sort(list); //排序
System.out.println(list);
//------扩展思考:List存自定义对象,并进行排序------
List list2 = new ArrayList();
list2.add(new Student(30));
list2.add(new Student(25));
//Collections.sort(list2); //有约束,Student类必须实现Comparable接口才行
}
}
//2.Set实现类HashSet(重点)
//2.1. HashSet存储原理
//HashSet实现类: 是Set接口的实现类
//特点:无序,无下标,元素唯一
//使用方式:
//分析HashSet存储原理:通过hash算法进行存储
public class Test1 {
public static void main(String[] args) {
Set set = new HashSet<>();
set.add(11);
set.add(33);
set.add(22);
set.add(33); //唯一的
System.out.println(set);
//因为无下标,所以不能使用基本for遍历
for(int i=0;i set = new HashSet();
set.add(new Student("zs"));
set.add(new Student("ls"));
set.add(new Student("zs"));
System.out.println(set);
//为什么下面的new String,它会根据属性的一致确定唯一性?
//根据存储原理分析结论得出:
//只要源码中的hashCode和equals一致,则确定唯一性,可以得出String类型肯定重写过
//如果存储自定义对象,要想根据属性确定唯一性,需要重写hashCode与equals
Set set2 = new HashSet();
set2.add(new String("zs"));
set2.add(new String("ls"));
set2.add(new String("zs"));
System.out.println(set2);
}
}
//===================存储多个属性的对象===================
//思考:假设两个属性的自定义对象存储;
class Teacher{
String name;
int age;
public Teacher(String name, int age) {
this.name = name;
this.age = age;
}
@Override
public String toString() {
return "Teacher [name=" + name + ", age=" + age + "]";
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + age;
result = prime * result + ((name == null) ? 0 : name.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Teacher other = (Teacher) obj;
if (age != other.age)
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
return true;
}
}
public class Test3 {
public static void main(String[] args) {
Set set = new HashSet();
set.add(new Teacher("laoLi",28));
set.add(new Teacher("laoHou",29));
set.add(new Teacher("laoLi",28));
System.out.println(set);
}
}
//3.Set实现类linkedHashSet
//3.1 linkedHashSet使用特点
//linkedHashSet: 是Set接口的实现类,也是HashSet的子类
//存值判断唯一性,交给父类完成的
//使用特点: 有序,唯一
public class Test1 {
public static void main(String[] args) {
Set set = new linkedHashSet();
set.add(1);
set.add(3);
set.add(2);
set.add(3);
System.out.println(set);
}
}
//3.1 集合的简单源码分析案例
class Super{
public void add(int a) {
insertsort();
}
public void insertsort() {
System.out.println("调用的是自己的...");
}
}
class Son extends Super{
@Override
public void insertsort() {
System.out.println("调用子类的方法..");
}
}
public class Test2 {
public static void main(String[] args) {
//new Super().add(1);
new Son().add(3);
}
}
//4. Set实现类TreeSet(重点)
//4.1 TreeSet的使用特点
//TreeSet:是Set接口的实现类
//TreeSet存储特点:可排序,唯一,无下标
//TreeSet的存储原理:通过二叉树进行存储
//细节
public class Test1 {
public static void main(String[] args) {
Set set = new TreeSet();
set.add(11);
set.add(33);
set.add(22);
set.add(11);
System.out.println(set);
//基本for: 不能
for(int i=0;i