Core Java (二十四) SortedMap,NavigableMap,SortedSet,NavigableSet接口

时间:2022-08-06 17:54:25

SortedMap接口

根据比较器进行排序,是一个有序映射表。

Comparator<? super K> comparator()Returns the comparator used to order the keys in this map, or null if this map uses the natural ordering of its keys.
Set<Map.Entry<K,V>> entrySet()Returns a Set view of the mappings contained in this map.
K firstKey()Returns the first (lowest) key currently in this map.
SortedMap<K,V> headMap(K toKey)Returns a view of the portion of this map whose keys are strictly less than toKey.
Set<K> keySet()Returns a Set view of the keys contained in this map.
K lastKey()Returns the last (highest) key currently in this map.
SortedMap<K,V> subMap(K fromKey, K toKey)Returns a view of the portion of this map whose keys range from fromKey, inclusive, to toKey, exclusive.
SortedMap<K,V> tailMap(K fromKey)Returns a view of the portion of this map whose keys are greater than or equal to fromKey.
Collection<V> values()Returns a Collection view of the values contained in this map.

NavigableMap接口

此接口实现了SortedMap接口,可以使用SortedMap接口的全部方法。其中,这个接口有三个很重要的方法:

NavigableMap<K,V> tailMap(K fromKey,
boolean inclusive)
Returns a view of the portion of this map whose keys are greater than (or equal to, if inclusive is true) fromKey.
NavigableMap<K,V> subMap(K fromKey,
boolean fromInclusive, K toKey, boolean toInclusive)
Returns a view of the portion of this map whose keys range from fromKey to toKey.
avigableMap<K,V> headMap(K toKey,
boolean inclusive)
Returns a view of the portion of this map whose keys are less than (or equal to, if inclusive is true) toKey.

SortedSet接口

根据比较器进行排序,是一个有序集。

下面是这个接口的全部方法:

Modifier and Type Method and Description
Comparator<? super E> comparator()Returns the comparator used to order the elements in this set, or null if this set uses the natural ordering of its elements.
E first()Returns the first (lowest) element currently in this set.
SortedSet<E> headSet(E toElement)Returns a view of the portion of this set whose elements are strictly less than toElement.
E last()Returns the last (highest) element currently in this set.
SortedSet<E> subSet(E fromElement, E toElement)Returns a view of the portion of this set whose elements range from fromElement, inclusive, to toElement, exclusive.
SortedSet<E> tailSet(E fromElement)Returns a view of the portion of this set whose elements are greater than or equal to fromElement.

NavigableSet接口

此接口有三个很重要的方法:

SortedSet<E> headSet(E toElement)Returns a view of the portion of this set whose elements are strictly less than toElement.
SortedSet<E> subSet(E fromElement, E toElement)Returns a view of the portion of this set whose elements range from fromElement, inclusive, to toElement, exclusive.
SortedSet<E> tailSet(E fromElement)Returns a view of the portion of this set whose elements are greater than or equal to fromElement.


总结上面的接口的方法,其中SortedSet和SortedMap方法返回大于等于form且小于to的所有元素子集;而NavigableSet和NavigableMap方法返回大于form且小于to的所有元素子集,是否包括form和to应由inclusive参数决定。


测试程序

下面是一个测试程序,分别应用了上面的某些方法。

package com.xujin;

import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.NavigableMap;
import java.util.NavigableSet;
import java.util.SortedMap;
import java.util.SortedSet;
import java.util.TreeMap;
import java.util.TreeSet;

public class LinkedListTest{
public static void main(String...arg){
Employee jim = new Employee("Jim");
Employee bob = new Employee("Bob");
Employee gin = new Employee("Gin");

Employee[] staff = new Employee[3];
staff[0] = jim;
staff[1] = bob;
staff[2] = gin;
List<Employee> staffList = Arrays.asList(staff);

System.out.println(staffList);//[Name:Jim, Name:Bob, Name:Gin]
System.out.println(staffList.get(2));//Name:Gin


/************SortedMap接口*******************************************/
SortedMap<String, Employee> sm = new TreeMap<String, Employee>();
sm.put("123", jim);
sm.put("234", bob);
sm.put("345", gin);
System.out.println(sm);//{123=Name:Jim, 234=Name:Bob, 345=Name:Gin}

SortedMap<String, Employee> smSub = sm.subMap("123", "345");//包括123,不包括345
System.out.println(smSub);//{123=Name:Jim, 234=Name:Bob}
smSub = sm.headMap("234");//返回比234小的,不包括234
System.out.println(smSub);//{123=Name:Jim}
smSub = sm.tailMap("234");//返回比234大的,不包括234
System.out.println(smSub);//{345=Name:Gin}


/************NavigableMap接口*******************************************/
NavigableMap<String, Employee> nm = new TreeMap<String, Employee>();
nm.put("123", jim);
nm.put("234", bob);
nm.put("345", gin);
System.out.println(nm);//{123=Name:Jim, 234=Name:Bob, 345=Name:Gin}

NavigableMap<String, Employee> nmSub = nm.subMap("123", true, "345", false);//包括123,不包括345
System.out.println(nmSub);//{123=Name:Jim, 234=Name:Bob}
nmSub = nm.headMap("234", true);//返回比234小的,true表示包括234
System.out.println(nmSub);//{123=Name:Jim, 234=Name:Bob}
nmSub = nm.tailMap("234", true);//返回比234大的,true表示包括234
System.out.println(nmSub);//{234=Name:Bob, 345=Name:Gin}


/************SortedSet接口*******************************************/
SortedSet<Employee> ss = new TreeSet<Employee>(
new Comparator<Employee>(){
public int compare(Employee a, Employee b){
return a.getName().compareTo(b.getName());
}
}
);
ss.add(jim);
ss.add(bob);
ss.add(gin);
System.out.println(ss);//[Name:Bob, Name:Gin, Name:Jim]

//以下的特点是,包括from,不包括to
SortedSet<Employee> ssSub = ss.subSet(gin, jim);//返回比gin大,比jim小的,不包括jim
System.out.println(ssSub);//[Name:Gin]
ssSub = ss.headSet(gin);//返回比gin小的,不包括gin
System.out.println(ssSub);//[Name:Bob]
ssSub = ss.tailSet(gin);//返回比gin大的,包括gin
System.out.println(ssSub);//[Name:Gin, Name:Jim]


/************NavigableSet接口*******************************************/
NavigableSet<Employee> ns = new TreeSet<Employee>(
new Comparator<Employee>(){
public int compare(Employee a, Employee b){
return a.getName().compareTo(b.getName());
}
}
);
ns.add(jim);
ns.add(bob);
ns.add(gin);
System.out.println(ns);//[Name:Bob, Name:Gin, Name:Jim]

//以下的特点是,根据inclusive参数决定是否包括from,to
NavigableSet<Employee> nsSub = ns.subSet(gin, true, jim, true);//返回比gin大,比jim小的,true表示包括gin和jim
System.out.println(nsSub);//[Name:Gin, Name:Jim]
nsSub = ns.headSet(gin, true);//返回比gin小的,true表示包括gin
System.out.println(nsSub);//[Name:Bob, Name:Gin]
nsSub = ns.tailSet(gin, true);//返回比gin大的,包括gin
System.out.println(nsSub);//[Name:Gin, Name:Jim]
}
}

class Employee{
public Employee(String name){
this.name = name;
}

public String getName(){
return this.name;
}

public String toString(){
return "Name:" + name;
}

private String name = "";//实例域初始化
}