19集合2

1. Set: 无序去重集合

无放入顺序,去重集合

  • boolean add(E); 元素有可能添加不进去

2 HashSet: hashcode散列集合

用hash算法和equals方法判断 两个元素是否相同。


 this.hash == key.hash && ( this.equasl(key) )

3 TreeSet: 排序树集合: 有(排序)序集合

  • 去重,有排序集合。

  • Comparable自然排序接口: 覆盖compareTo(T t) 方法;( this - t)就是升序,反之降序;

    • 很多类都有compareTo(); 正数是大于, 0是等于, 负数是小于。
  • TreeSet底层是TreeMap.

在这里插入图片描述

            do {
                parent = t;
                cmp = k.compareTo(t.key);
                if (cmp < 0)
                    t = t.left;
                else if (cmp > 0)
                    t = t.right;
                else
                    return t.setValue(value);
            } while (t != null);

Set: 去重和 无序(放入的顺序)

HashSet: equals和hashcode;

TreeSet: Comparable接口的compareTo方法; 有(排序)序

4. 比较器相关:

List的

  • sort(Comparetor)方法 : JDK8
    • Comparetor里面的compare(T o1, T o2) {} ; o1 - o2 升序,o2 - o1降序
 for(int i = destLow, p = low, q = mid; i < destHigh; i++) {
            if (q >= high || p < mid && c.compare(src[p], src[q]) <= 0)
                dest[i] = src[p++];
            else
                dest[i] = src[q++];
 }
  • Collections.sort(list); list的元素的 自然排序(Comparable)
  • Collections.sort(list, Comparetor); 用比较器进行排序

元素的 自然排序(Comparable)

  • Collections.sort(list, Comparetor); 用比较器进行排序
posted @ 2021-04-11 12:59  剑心空明  阅读(0)  评论(0编辑  收藏  举报  来源