HashSet类
1、哈希值概念
哈希值:哈希值就是调用对象的hashCode()方法后返回的一个int型数字
哈希桶:简单点理解就是存储相同哈希值对象的一个容器
2、HashSet类中的add(obj)方法如何保证元素的唯一性
重写hashCode()和equals()方法 ,为什么,如果一个对象的哈希值和eques方法与容器中的对象有相同的,就不添加。如果没有就添加
TreeSet类
1、如何保证元素唯一性
内部采用的是二叉树结构,相同的元素就不操作
2、如何判断两个元素是否相同
A、对象所属的类去实现Comparable接口
重写compareTo方法
如果该方法返回0 ,则两个元素相同public class Person
implements Comparable{
@Override
public int compareTo(Object o) {
return 0;
}
}
TreeSet set = new TreeSet();
set.add(new Person());
B、调用TreeSet的带参构造方法,传入Comparator的子类对象
该子类对象重写compare(T o1, T o2)方法
如果返回0则两个元素相同
public class MyComparator implements Comparator {
@Override
public int compare(Object o1, Object o2) {
return 0;
}
}
TreeSet set = new TreeSet(new MyComparator());
set.add(new Student);
Collections类
排序 : public static void sort(List list)
查找 : public static int binarySearch(List list, Object obj)
反转 : public static void reverse(List list)
最大值 : public static Object max(Collection coll)
随机换位 : public static void shuffle(List list)
Collection和Collections的区别
Collection是单列集合的顶层接口,而Collections是集合的工具类