從 Java 中的 TreeMap 獲取子對映
要獲取 Java 中的子對映,請使用 submap() 方法。它返回 Map 中範圍內的元素。
首先,建立一個 TreeMap 並新增元素 −
TreeMap<Integer,String> m = new TreeMap<Integer,String>(); m.put(1,"PHP"); m.put(2,"jQuery"); m.put(3,"JavaScript"); m.put(4,"Ruby"); m.put(5,"Java"); m.put(6,"AngularJS"); m.put(7,"ExpressJS");
現在,獲取 4 到 6 之間的子對映 −
m.subMap(4, 6)
下面是一個示例,用於從 Java 中的樹對映獲取子對映
示例
import java.util.*;
public class Demo {
public static void main(String args[]){
TreeMap<Integer,String> m = new TreeMap<Integer,String>();
m.put(1,"PHP");
m.put(2,"jQuery");
m.put(3,"JavaScript");
m.put(4,"Ruby");
m.put(5,"Java");
m.put(6,"AngularJS");
m.put(7,"ExpressJS");
System.out.println("TreeMap Elements...
"+m);
System.out.println("Sub Map = " + m.subMap(4, 6));
}
}輸出
TreeMap Elements...
{1=PHP, 2=jQuery, 3=JavaScript, 4=Ruby, 5=Java, 6=AngularJS, 7=ExpressJS}
Sub Map = {4=Ruby, 5=Java}
廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP