Java程序从TreeMap获取子映射
要从TreeMap获取子映射,让我们首先创建一个TreeMap并设置键值对-
TreeMap<String, String>tMap = new TreeMap<String, String>();
tMap.put("1", "A");
tMap.put("2", "B");
tMap.put("3", "C");
tMap.put("4", "D");
tMap.put("5", "E");
tMap.put("6", "F");
tMap.put("7", "G");现在,假设您需要将子图从3映射到7。为此,请使用submap()-
=SortedMap<String, String>map = tMap.subMap("3", "7");示例
import java.util.SortedMap;
import java.util.TreeMap;
public class Demo {
public static void main(String[] args) {
TreeMap<String, String>tMap = new TreeMap<String, String>();
tMap.put("1", "A");
tMap.put("2", "B");
tMap.put("3", "C");
tMap.put("4", "D");
tMap.put("5", "E");
tMap.put("6", "F");
tMap.put("7", "G");
tMap.put("8", "H");
tMap.put("9", "I");
tMap.put("10", "J");
SortedMap<String, String>map = tMap.subMap("3", "7");
System.out.println("SortedMap = " + map);
map = tMap.subMap("5", "7");
System.out.println("SortedMap = " + map);
map = tMap.subMap("8", "8");
System.out.println("SortedMap = " + map);
}
}输出结果
SortedMap= {3=C, 4=D, 5=E, 6=F}
SortedMap = {5=E, 6=F}
SortedMap = {}热门推荐
1 带狗狗训练
4 听力训练狗狗
6 怎训练狗狗
7 腹肌训练狗狗
8 网球训练狗狗
9 记叙文训练狗狗
10 狗狗太凶怎么训练
11 核心力量训练狗狗
12 狗狗乱叫怎么训练
13 发音训练狗狗
14 怎么训练狗狗叼东西给主人
15 荆州训练狗狗
16 篮球训练狗狗
17 做训练狗狗
18 训练狗狗坐姿