实验代码:
import java.util.Map;
import java.util.SortedMap;
import java.util.TreeMap;
public class sortedmap {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("集合输出左闭右开");
SortedMap<String, Integer> sm = new TreeMap<String, Integer>();
sm.put("c", 3); sm.put("b", 2); sm.put("d", 4); sm.put("e", 5); sm.put("a", 1);
System.out.println("headSet()");
for(Map.Entry<String, Integer> me:sm.headMap("c").entrySet()) {
System.out.print(me.getKey()+"->"+me.getValue()+"\n");
}
System.out.println("tailSet()");
for(Map.Entry<String, Integer> me:sm.tailMap("c").entrySet()) {
System.out.print(me.getKey()+"->"+me.getValue()+"\n");
}
System.out.println("subSet()");
for(Map.Entry<String, Integer> me:sm.subMap("a", "c").entrySet()) {
System.out.print(me.getKey()+"->"+me.getValue()+"\n");
}
}
}
运行结果如下:
一句话总结:三个方法返回值都是“左闭右开区间”。