Collections工具类
有了这个工具类,大家可以选择性记住一些集合
单列集合只需要记住 ArrayList
双列集合记住 HashMap TreeMap LinkedHashMap
一般关系集合哪些特点:
1.是否有序
2.是否唯一
3.是否可重复
4.是否可排序
5.是否线程安全
6.效率 【数据结构】
7.求最值
8.倒置
9.查找
10.随机
equals和hashcode方法用来去重
Comparator用来排序
//去重操作:普通类实现equals和hashcode方法
Set<Bos> l=new HashSet();
l.add(new Bos("zx",12));
l.add(new Bos("zx2",122));
l.add(new Bos("zx",12));
l.add(new Bos("zx",12));
l.add(new Bos("zx3",142));
System.out.println(l);
//[Bos [name=zx, age=12], Bos [name=zx3, age=142], Bos [name=zx2, age=122]]
排序操作
ArrayList<Integer> li=new ArrayList();
li.add(12);
li.add(17);
li.add(13);
li.add(15);
li.add(12);
Collections.sort(li); //因为是Integer包装类,普通类要实现Conparator接口,才能用该方法
//[12, 12, 13, 15, 17]
//线程安全
Collections.synchronizedCollection(l);
//求最值
Collections.max(li);//17
Collections.min(li);//12
//查找li集合中key对应的下标
int index=Collections.binarySearch(li, 17);
System.out.println(index);//4
//倒置[17, 15, 13, 12, 12]
Collections.reverse(li);
System.out.println(li);