chap11-Sorting

《java collections》学习笔记

comparable basics

Comparable

<java.lang.Comparable>

public interface Comparable<T> {
    /**
     *
     * this > o  return 正数
     * this == o return 0
     * this < o return 负数
     */
    public int compareTo(T o);
}

String.compareTo()

public int compareTo(String anotherString) {
   int len1 = value.length;
   int len2 = anotherString.value.length;
   //取最小的长度
   int lim = Math.min(len1, len2);
   char v1[] = value;
   char v2[] = anotherString.value;

   int k = 0;
   //先行比较string 前面lim 每个字符的大小;
   while (k < lim) {
       char c1 = v1[k];
       char c2 = v2[k];
       if (c1 != c2) {
           return c1 - c2;
       }
       k++;
   }
   
   //如果前面lim各个字符相等, 则判断字符串长度
   return len1 - len2;
}

Comparator

虽然Comparable对象可以跟多个实例进行比较,但是Comparator更为方便,它提供的compare方法带有两个入参.
故针对collection的排序,基本都是用Comparator作为比较函数来进行排序的。
<java.lang.Comparator>

public interface Comparator<T> {
    /**
     *  o1 > o2  return 正数
     *  o1 == o2 return 0
     *  o1 < o2  return 负数
     */
    int compare(T o1, T o2);
   
    //与collection的排序无关; 用来判断 this 与 其他Comparator 的比较关系。
    //因为所有的类都继承自Object,Object中已经定义了`equals`方法,故如果无特殊要求,无需`override`此方法.
    boolean equals(Object obj);

}

Using Comparator

@Test
public void test01(){
   Set<String> set = new TreeSet<>(new Comparator<String>() {
       @Override
       public int compare(String o1, String o2) {
//				return o1.compareTo(o2); //正序
           return o2.compareTo(o1); //逆序
       }
   });
   set.addAll(Arrays.asList("a", "b", "c"));
   set.add("d");
   System.out.println(set); //  [d, c, b, a]
}


SortedSet

在这里插入图片描述

<java.uti.SortedSet>

public interface SortedSet<E> extends Set<E> {
    /**
     * 返回比较器; 
     * 如果返回null 则调用元素的compareTo方法;
     */
    Comparator<? super E> comparator();

     //[fromElement,toElement)
    SortedSet<E> subSet(E fromElement, E toElement);

    //[first , toElement)
    SortedSet<E> headSet(E toElement);

    //[fromElement,last]
    SortedSet<E> tailSet(E fromElement);

    E first();
    E last();
}

注意

  • 当comparator()为空时,集合中的元素(key)必须要实现Comparable接口,然后调用它的key.compareTo(anotherKey)方法
  • 当comparator() not null时,则集合中元素(key)不强制实现Comparable接口

<java.util.NavigableSet>

public interface NavigableSet<E> extends SortedSet<E> {
     // < e 的最大元素; 没有则返回null
    E lower(E e);

    // <= e 的最大元素; 没有则返回null
    E floor(E e);

    // >= e 的最小元素; 没有则返回null
    E ceiling(E e);

    // > e  的最小元素; 没有则返回null
    E higher(E e);

    //获取并移除第一个(最低)元素;如果此 set 为空,则返回 null。
    E pollFirst();

    // 获取并移除最后一个(最高)元素;如果此 set 为空,则返回 null。
    E pollLast();

    //以升序返回在此 set 的元素上进行迭代的迭代器。
    Iterator<E> iterator();

    //以降序返回在此 set 的元素上进行迭代的迭代器。
    Iterator<E> descendingIterator();
    //返回此 set 中所包含元素的逆序视图。
    NavigableSet<E> descendingSet();

 
    //fromInclusive:表示是否包含fromElement ,toInclusive同理
    NavigableSet<E> subSet(E fromElement, boolean fromInclusive,
                           E toElement,   boolean toInclusive);
    NavigableSet<E> headSet(E toElement, boolean inclusive);
    NavigableSet<E> tailSet(E fromElement, boolean inclusive);


    SortedSet<E> subSet(E fromElement, E toElement);
    SortedSet<E> headSet(E toElement);
    SortedSet<E> tailSet(E fromElement);
}


SortedMap

在这里插入图片描述

<java.util.SortedMap>

SortedMap定义的前6个方法,基本同SortedSet一致,这里不再赘述.

public interface SortedMap<K,V> extends Map<K,V> {
    Comparator<? super K> comparator();

    SortedMap<K,V> subMap(K fromKey, K toKey);
    SortedMap<K,V> headMap(K toKey);
    SortedMap<K,V> tailMap(K fromKey);

    K firstKey();
    K lastKey();
  
    Set<K> keySet();
    Collection<V> values();
    Set<Map.Entry<K, V>> entrySet();
}

<java.util.NavigableMap>

public interface NavigableMap<K,V> extends SortedMap<K,V> {
  
    //
    K lowerKey(K key);
    K floorKey(K key);
    K ceilingKey(K key);     
    K higherKey(K key);
    
    Map.Entry<K,V> lowerEntry(K key);
    Map.Entry<K,V> floorEntry(K key);
    Map.Entry<K,V> ceilingEntry(K key);
    Map.Entry<K,V> higherEntry(K key);


    Map.Entry<K,V> firstEntry();
    Map.Entry<K,V> lastEntry();

    Map.Entry<K,V> pollFirstEntry();
    Map.Entry<K,V> pollLastEntry();

 
    
    //map中包含的key 的 NavigableSet 视图。
    NavigableSet<K> navigableKeySet();
    //map中包含的key 的 NavigableSet 逆序视图。
    NavigableSet<K> descendingKeySet();
    //逆序map
    NavigableMap<K,V> descendingMap();
   
    NavigableMap<K,V> subMap(K fromKey, boolean fromInclusive,
                             K toKey,   boolean toInclusive);
    NavigableMap<K,V> headMap(K toKey, boolean inclusive);
    NavigableMap<K,V> tailMap(K fromKey, boolean inclusive);

    SortedMap<K,V> subMap(K fromKey, K toKey);
    SortedMap<K,V> headMap(K toKey);
    SortedMap<K,V> tailMap(K fromKey);
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值