一、Set
set集合List有序的集合 Set无序的集合 有序 取出的顺序和添加的顺序是一样的 无序 取出的顺序和添加的顺序不一定一样
1、实例化HashSet
HashMap实现该类
HashSet set=new HashSet();set.add("23");set.remove("123");set.size();set.add("23");int size=set.size();System.out.println(size);//1
Set中不能存储相同的数据
set.add(456);//456是Integer类型set.add(456);//用equals比较是否重复size=set.size();System.out.println(size);//2//可以存储null值set.add(null);size=set.size();System.out.println(size);//3for(Object item:set){System.out.println(item);}
2、TreeSet 红黑树 TreeMap实现该类
TreeSet内部使用二叉树保存数据,从右到左一次增大 内部的节点是可以比较大小的 也因此for each遍历自带排序
TreeSet test=new TreeSet();//存入数据test.add(2);test.add(200);test.add(3);test.add(88);test.add(45);test.add(72);test.add(1);test.add(99);
不能存null值 报空指针异常 存入时不知将其放到哪个位置
遍历
for(Object item:test){System.out.print(item+",");}
二叉树遍历顺序有先序(中左右)、中序(左中右)、后续遍历(左右中) TreeSet遍历方式 中序遍历
同一个TreeSet对象中存储的内容都应该是可比较的 默认情况下不能存储不同类型
public class EasySet {public static void main(String[] args) {Comparator<Student> com=(a,b)-> {if (a.score==b.score) {return 0;}return a.score > b.score ? 1 : -1;};//内部类 匿名实现类TreeSet<Student> ttset=new TreeSet<>(com);Student stua=new Student();stua.score=89;Student stub=new Student();stub.score=80;Student stuc=new Student();stuc.score=99;Student stud=new Student();stud.score=100;ttset.add(stua);ttset.add(stub);ttset.add(stud);ttset.add(stuc);for(Student stu:ttset){System.out.println(stu.score);}TreeSet<Student> tttset=new TreeSet();Student stuaa=new Student();stuaa.score=90;tttset.add(stuaa);//Student未实现Comparable接口 不可比较 报错ClassCastExceptionstuaa=new Student();stuaa.score=89;tttset.add(stuaa);stuaa=new Student();stuaa.score=8;tttset.add(stuaa);stuaa=new Student();stuaa.score=4;tttset.add(stuaa);for(Student stu:tttset){System.out.println(stu.score);}}
}class Student implements Comparable{int score;@Overridepublic 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("传入对象不可比较");}}
}
如上,若要让TreeSet中的对象可比,要么在定义TreeSet时传入一个实现了Comparator接口的类,要么让存入的对象可比,使存入对象的类实现Comparable接口、重写compareTo()方法
3、LinkedHashSet
是有序的集合Set 按照插入顺序排序
二、Map
不属于Collection 存储键值对 键 就是名字 值 储存的对象
1、HashMap的定义与常用方法
Map map=new HashMap();//存放数据map.put("A1","张三");//可以通过存入的key获取存入的值Object obj=map.get("A1");System.out.println(obj);//通过key删除键值对Object rem_obj=map.remove("A1");System.out.println("删除的value值:"+rem_obj);//是否包含keymap.containsKey("A1");//是否包含valuemap.containsValue("张三");//获取所有的keySet setkey=map.keySet();//获取所有的valueCollection con=map.values();
key、value均可为null 添加key相同的节点,后者value替换前者value
map.put(null,null);map.put("A1",null);map.put("A1","张三");//覆盖掉nullSystem.out.println(map);
2、TreeMap
key应该是可比较的key不能是null值 value可以是null值
3、Hashtable
key和value都不能是null值
Hashtable 是线程安全的集合
Hashtable ht=new Hashtable();ht.put("t1","张三");
4、LinkedHashMap
有序的 维护着一个链表
LinkedHashMap lh=new LinkedHashMap();lh.put("","");
5、ConcurrentHashMap
线程安全 效率较高
与Hashtable的区别
都是线程安全的Map 但Hashtable是一个操作全部锁定
而ConcurrentHashMap 锁的颗粒度较小,只锁一部分 性能较为优异
6、HashMap 的底层实现
数组加链表
HashMap 的数组默认容量 16每次扩容 2倍扩容阈值 0.75一个链达到8就对该链进行树化一支树上的元素低于6个 这个树就会退化成链最小树化容量阈值 64 数组长度达到这个值才能树化(不包括链表上的元素)如果数组的长度没有达到64,优先扩容而不树化
三、泛型
即广泛的数据类型 是确保类型安全的一种途径
1、定义
在类或方法上定义泛型
public class EasyGenericity<N,M,J, Easy> {private M m;public void test(M m,N n){}public <E> E test(E e){//返回的类型和传入的类型是同一种return e;}
}
2、对类型安全的保障
不安全的情况
List list=new ArrayList();list.add("123");list.add(new Object());for(Object item:list){//类型转换时没有明确对象的数据类型进行强制数据类型转换//会抛出ClassCastException(类型转换异常)//类型不安全String str=(String) item;System.out.println(str);}
安全的情况
List<String> listA=new ArrayList<>();//在声明处检查泛型 new后只有传递作用listA.add("123");
new后的泛型可以不写,不影响之后对于存入对象的限制,而前面声明处的泛型则不能省略
3、泛型的上/下限
定义泛型上限
public static <J extends List> J testA(J j){return j;}
只能存入 List类或 List类子类的对象
定义泛型下限
public class EasyGenericity<N,M,J, Easy> {public class EasyGenericity<N,M,J, Easy> {List<C> list1=new ArrayList<>();testAA(list1,new C()); }public static <A> void testAA(List <? super A>a,A aa){//定义泛型下限 A与List<T>中T同类型或是T的子类}
}
class B {}
class C extends B{}
class D extends C{}