獲取儲存在 Java TreeMap 中的最低鍵


使用 firstKey() 方法來獲取儲存在 TreeMap 中的最低鍵。

我們先設定 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");

現在獲取最低鍵 −

m.firstKey()

以下是獲取 TreeMap 中最低鍵的示例 −

示例

 線上示例

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("Lowest key in TreeMap: " + m.firstKey());    } }

輸出

TreeMap Elements...
{1=PHP, 2=jQuery, 3=JavaScript, 4=Ruby, 5=Java, 6=AngularJS, 7=ExpressJS}
Lowest key in TreeMap: 1

更新於: 30-7-2019

1K+ 瀏覽

開啟您的職業生涯 生涯

完成課程以獲得證書

開始
廣告
© . All rights reserved.