Set集合
list是有序的的集合 Set是无序的
有序 取出和添加的顺序是一样的
无序 取出和添加的顺序不一定一样
实例化HashSet集合
HashSet set=new HashSet();
Set集合元素的添加,删除,查看元素个数
set.add("23");
set.remove("123");
set.size();
对于下面代码,456存储的是Integer类型,456大于缓存范围,所以不是同一个对象,但是在Set里面用的是重写后的equal方法,将它们视为是为一个值。
所以size的值只会加1。HashSet中存储相同的数据,只会存一次,可以存储null值。
set.add(456);
set.add(456);
int size=set.size();
HashSet集合的遍历
for (Object item:set){
System.out.println(item);
}
TreeSet是红黑树结构采用中序遍历,遍历顺序是左根右
TreeSet内部使用二叉树,内部节点可以比较大小
同一个TreeSet存储的类型都应该是可以比较的,默认情况下不可以存储不同类型的元素。存入数据,不能存null值,null值无法比较
TreeSet集合的实例化和集合的遍历
TreeSet test = new TreeSet();
test.add(2);
test.add(200);
test.add(20);
test.add(10);
test.add(28);
test.add(66);
for (Object item:test){
System.out.print(item+",");
}
比较器(下例实定义了比较两个 Student 对象的排序顺序)
Comparator<Student> com = (a,b)->{
if (a.score==b.score){
return 0;
}
return a.score>b.score?1:-1;
};
比较器的使用当将上面的比较器传递给 TreeSet<Student> et = new TreeSet<>(com); 时,TreeSet 将会根据这个比较器的逻辑来确定集合中元素的排序顺序。
TreeSet<Student> et=new TreeSet<>(com);
Student student1=new Student(); student1.score=89;
Student student2=new Student(); student2.score=99;
Student student3=new Student(); student3.score=88;
Student student4=new Student(); student4.score=96;
et.add(student1); et.add(student2); et.add(student3); et.add(student4);
for(Student item:et){
System.out.println(item.score);
}
在Student类中通过实现 Comparable 接口的 compareTo 方法来进行比较操作。将 Student 对象放入 TreeSet 中时,TreeSet 会使用 compareTo 方法来决定元素的排序顺序。根据 score 属性的值来比较 student1 和 student2 的大小关系,并输出相应的结果。
TreeSet<Student> te=new TreeSet();
Student st1=new Student(); st1.score=90;
te.add(st1);
st1=new Student(); st1.score=87;
te.add(st1);
st1=new Student(); st1.score=98;
te.add(st1);
st1=new Student(); st1.score=89;
te.add(st1);
for (Student item:te){
System.out.println(item.score);
}
class Student implements Comparable{
@Override
public int compareTo(Object o){
if(o instanceof Student){
Student item = (Student)o;
if(this.score==item.score){
return 0;
}
return this.score > item.score?1:-1;
}else {
//如果o对象不是Student就无法比较 这是程序运行时出现的特殊情况
//异常情况 我们的方法处理不了这种情况 就要抛出一个异常对象告知要用此方法的代码
throw new RuntimeException("传入对象不可比较");
}
}
int score;
}
Collection : list set
list: ArraysList Vector(也是通过数组来存储数据的集合列表线程安全的集合) LinkList
Set : HashSet TreeSet LinkedHashSet(链表结构,是一个有序的Set)
Map
存储键值对 键就是名字 存储的对象
Map map=new HashMap();
方法的调用
存放数据
map.put("A1","张三");
可以通过存入的key获取存储的对象
Object obj = map.get("A1");//获取A1对应的values值
System.out.println(obj);
通过key删除键值对,remove会返回被删除的value值
Object rem_obj=map.remove("A1");
System.out.println(rem_obj);
是否包含key
map.containsKey("A1");
是否包含value值
map.containsValue("张三");
获取所有的key
Set setkey=map.keySet();
获取所有的value
Collection con = map.values();
key值应该是可以比较的,key值不能重复,key可以为null值(只能一个key值为null ,因为key不能重复)value可以为null,可重复
map.put(null,null);
map.put("A1",null);
map.put("A1","张三");//同样的key值会覆盖上一个相同key值
TreeMap
TreeMap 中的key不能为null
Hashtable key和value都不能是null
线程安全的Map:Hashtable和ConcurrentHashMap(性能优异锁的颗粒度比较小)
Hashtable ht = new Hashtable();
ht.put("t1","lisa");
// LinkedHashMap
LinkedHashMap lMap=new LinkedHashMap();
lMap.put("","");
ConcurrentHashMap线程安全的 效率较高
ConcurrentHashMap chm;
HashMap 底层实现 数组加链表
HashMap hmap=new HashMap();
hmap.put("","");
HashMap 每次扩容2倍 数组默认值为 16
扩容阈值 0.75(达到阈值就要扩容)
树化阈值 一个链上的元素达到8个就对改链进行树化
一只树上的元素低于6个这个数就会退化成链
最小树化容量阈值 64 数组达到64 且链上元素大于8
如果数组元素没有到64 优先扩容
泛型
广泛的数据类型,确保类型安全的一种途径
定义:类或方法上定义泛型
public class EasyGenericity<J,M,N,Y,Easy>{}
public <J extends List> J testA(J j){return j;}
返回的类型和传入的类型是同一种
public <E> E test(E e){return e;}
在类型转换的时候没有明确对象的数据类型进行强制数据类型转换
会报ClassCaseException(类型转换异常)类型不安全