Collections 工具类

声明:以下笔记来源于韩顺平视频https://www.bilibili.com/video/BV1fh411y7R8?p=1,笔记为楼主亲手劳动,劳动不易,转载请标明谢谢。

Collections 工具类

Collections 工具简介
  1. Collections是一个操作Set、List 和Map等集合的工具类
  2. Collections中提供了一系列静态的方法对集合元素进行排序、查询和修改等操作
Collections 常用方法
  1. reverse(List):反转List中元素的顺序
  2. shuffle(List):对List 集合元素进行随机排序
  3. sort(List):根据元素的自然顺序对指定List 集合元素按升序排序
  4. sort(List, Comparator): 根据指定的Comparator产生的顺序对List集合元素进行排序
  5. swap(List, int, int):将指定list集合中的i处元素和j处元素进行交换
  6. Object max(Collection):根据元素的自然顺序,返回给定集合中的最大元素
  7. Object max(Collection, Comparator): 根据Comparator指定的顺序,返回给定集合中的最大元素
  8. Object min(Collection)
  9. Object min(Collection, Comparator)
  10. int frequency(Collection, Object): 返回指定集合中指定元素的出现次数
  11. void copy(List dest,List src):将src中的内容复制到dest中
  12. 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是如何去重的
  1. HashSet 底层是根据 HashCode() 值与 equals() 方法判断是否添加元素,底层先通过存入对象通过运算得到一个hash值,通过hash值算出table表的索引位置,当该位置没有元素时自动组装,当有位置时进行对象比较与equals()比较,判断两个元素是否为同一个对象,不相同则加入,相同则放弃加入
  2. 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)]

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值