import java.util.TreeMap;
导入 java.util.Set;
import java.util.Iterator;
import java.util.Map;
public class parcours_treemap {
public static void main(String args[]) {
/* TreeMap 声明 */
TreeMaptm = new TreeMap ();
/*向 TreeMap 添加元素*/
tm.put(12, val1”);
tm.put(5, val2”);
tm.put(8, val3”);
tm.put(2, val4”);
/* 使用迭代器显示内容 */
Set set = tm.entrySet();
迭代器 迭代器 = set.iterator();
while(iterator.hasNext()) {
Map.Entry mapentry = (Map.Entry)iterator.next();
System.out.print(key: ”+ mapentry.getKey() + +
”- value: +mapentry.getValue()+”\n);
}
}
}
key: 2 - value: val4我们可以看到这对夫妇<键、值>按 keys.
key: 5 - 值:val2
key:8 - 值:val3
key:12 - 值:val1
import java.util.TreeMap;执行此代码将输出以下结果:
public class parcours {
public static void main(String args[]) {
/* TreeMap 声明 */
TreeMaptm = new TreeMap ();
/*向 TreeMap 添加元素*/
tm.put(12, val1”);
tm.put(5, val2”);
tm.put(8, val3”);
tm.put(2, val4”);
TreeMaptm2 = new TreeMap ();
tm2.putAll(tm);
/*显示几对只是为了检查 */
System.out.print(key: 12” + - value: ”+tm2.get(12)+\n”);
System.out.print(key: 8” + - 值: ”+tm2.get(8)+\n”);
/*第一个和最后一个键*/
System.out.print(第一个键:”+tm2.firstKey()+ 最后一个键:”+tm2.lastKey());
}
}
key: 12 - value: val1References:
key: 8 - value: val3
first key: 2 最后一个键:12
Please disable your ad blocker and refresh the window to use this website.