声明:以下笔记来源于韩顺平视频https://www.bilibili.com/video/BV1fh411y7R8?p=1,笔记为楼主亲手劳动,劳动不易,转载请标明谢谢。
Collections 工具类
Collections 工具简介
- Collections是一个操作Set、List 和Map等集合的工具类
- Collections中提供了一系列静态的方法对集合元素进行排序、查询和修改等操作
Collections 常用方法
- reverse(List):反转List中元素的顺序
- shuffle(List):对List 集合元素进行随机排序
- sort(List):根据元素的自然顺序对指定List 集合元素按升序排序
- sort(List, Comparator): 根据指定的Comparator产生的顺序对List集合元素进行排序
- swap(List, int, int):将指定list集合中的i处元素和j处元素进行交换
- Object max(Collection):根据元素的自然顺序,返回给定集合中的最大元素
- Object max(Collection, Comparator): 根据Comparator指定的顺序,返回给定集合中的最大元素
- Object min(Collection)
- Object min(Collection, Comparator)
- int frequency(Collection, Object): 返回指定集合中指定元素的出现次数
- void copy(List dest,List src):将src中的内容复制到dest中
- boolean replaceAll(List list, Object oldVal, Object newVal):使用新值替换List对象的所有旧值
@SuppressWarnings({"all"}) public class Collections_ { public static void main(String[] args) { ArrayList list = new ArrayList(); list.add("tom"); list.add("smith"); list.add("king"); list.add("milan"); //1. reverse(List):反转List中元素的顺序 System.out.println("==reverse(List):反转List中元素的顺序=="); Collections.reverse(list); System.out.println(list); //2. shuffle(List):对List 集合元素进行随机排序 System.out.println("==shuffle(List):对List 集合元素进行随机排序=="); Collections.shuffle(list); System.out.println(list); //3. sort(List):根据元素的自然顺序对指定List 集合元素按升序排序 System.out.println("==sort(List):根据元素的自然顺序对指定List 集合元素按升序排序=="); Collections.sort(list); System.out.println(list); //4. sort(List, Comparator): 根据指定的Comparator产生的顺序对List集合元素进行排序 System.out.println("==sort(List, Comparator): 根据指定的Comparator产生的顺序对List集合元素进行排序=="); Collections.sort(list, new Comparator() { @Override public int compare(Object o1, Object o2) { // return ((String)o1).compareTo((String)o2); return ((String)o1).length() - ((String)o2).length(); } }); System.out.println(list); //5. swap(List, int, int):将指定list集合中的i处元素和j处元素进行交换 System.out.println("==swap(List, int i, int j):将指定list集合中的i处元素和j处元素进行交换=="); Collections.swap(list,0,1); System.out.println(list); //1. Object max(Collection):根据元素的自然顺序,返回给定集合中的最大元素 System.out.println("==Object max(Collection):根据元素的自然顺序,返回给定集合中的最大元素=="); Comparable max = Collections.max(list); System.out.println(max); //2. Object max(Collection, Comparator): 根据Comparator指定的顺序,返回给定集合中的最大元素 System.out.println("==Object max(Collection, Comparator): 根据Comparator指定的顺序,返回给定集合中的最大元素=="); Collections.max(list, new Comparator() { @Override public int compare(Object o1, Object o2) { // return ((String)o1).compareTo((String)o2); return ((String)o1).length() - ((String)o2).length(); } }); //3. Object min(Collection) //4. Object min(Collection, Comparator) //5. int frequency(Collection, Object): 返回指定集合中指定元素的出现次数 System.out.println("==int frequency(Collection, Object): 返回指定集合中指定元素的出现次数=="); int time = Collections.frequency(list, "tom"); System.out.println("tom出现次数=" + time); //6. void copy(List dest,List src):将src中的内容复制到dest中 System.out.println("==void copy(List dest,List src):将src中的内容复制到dest中=="); ArrayList dest = new ArrayList(); for (int i = 0;i < 4;i++) {//因为目标链表的长度要 >= 被复制的链表长度,不然会数组下标越界 IndexOutOfBoundsException dest.add(i); } Collections.copy(dest,list); System.out.println("dest=" + dest); //7. boolean replaceAll(List list, Object oldVal, Object newVal):使用新值替换List对象的所有旧值 System.out.println("==boolean replaceAll(List list, Object oldVal, Object newVal):使用新值替换List对象的所有旧值=="); Collections.replaceAll(list,"tom","汤姆"); System.out.println(list); } }
面试简答题
1.试分析HashSet和TressSet是如何去重的
- HashSet 底层是根据 HashCode() 值与 equals() 方法判断是否添加元素,底层先通过存入对象通过运算得到一个hash值,通过hash值算出table表的索引位置,当该位置没有元素时自动组装,当有位置时进行对象比较与equals()比较,判断两个元素是否为同一个对象,不相同则加入,相同则放弃加入
- TressSet 的去重机制是根据传入的Comparator匿名对象,使用compare方法比较,返回值为<0 、>0进行加入,等于0则放弃加入,如果没有传入比较器Comparator 匿名对象,则以你添加的对象实现的Comparable的compareTo()方法进行比较(其实是按默认字母表比较)。
2.是判断下面异常是否会抛异常
TreeSet treeset = new TreeSet(); treeset.add(new Person());
答:会抛出ClassCastException的异常,因为没有传入Comparator匿名内部类,会按照传入对象的CompareTo()方法进行比较,但new Person()类并没有实现Comparable接口,在compara方法内进行强转会报错。
3.试写出Vector与ArrayList的比较
底层结构 版本 线程安全 扩容机制 ArrayList 可变数组Object[] jdk1.2 线程不安全 如果使用有参构造器每次按照1.5倍扩容,如果是无参,第一次扩容10,之后按照1.5倍扩容 Vector 可变数组 jdk1.0 安全,效率不高 如果是无参,默认第一次扩容10,之后按两倍扩容,如果是有参,每次扩容按照两倍扩容
4.分析如下代码的输出
//已知Person类按照id与name重新了HashCode的值与equals()方法
public class homework01 { public static void main(String[] args) { HashSet set = new HashSet(); Persion p1 = new Persion(1001, "AA"); Persion p2 = new Persion(1002, "BB"); set.add(p1); set.add(p2); p1.name = "CC"; set.remove(p1); System.out.println(set);//分析 set.add(new Persion(1001,"CC")); System.out.println(set);//分析 set.add(new Persion(1001,"AA")); System.out.println(set);//分析 } } class Persion { public int numb; public String name; public Persion(int numb, String name) { this.numb = numb; this.name = name; } @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Persion persion = (Persion) o; return numb == persion.numb && Objects.equals(name, persion.name); } @Override public int hashCode() { return Objects.hash(numb, name); } @Override public String toString() { return "Persion{" + "numb=" + numb + ", name='" + name + '\'' + '}'; } }
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-beTiZlZj-1648481197045)(D:\Typora Data\Img\image-20220308164736064.png)]