《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);
}