刪除 Java 中 TreeMap 的第一個條目


若要刪除 TreeMap 的第一個條目,請使用 pollFirstEntry() 方法。

我們先建立一個 TreeMap 並新增元素 −

TreeMap<Integer,String> m = new TreeMap<Integer,String>();
m.put(1,"India");
m.put(2,"US");
m.put(3,"Australia");
m.put(4,"Netherlands");
m.put(5,"Canada");

現在刪除第一個條目 −

m.pollFirstEntry()

以下是刪除 TreeMap 中第一個條目的示例。

示例

 線上演示

import java.util.*;
public class Demo {
   public static void main(String args[]) {
      TreeMap<Integer,String> m = new TreeMap<Integer,String>();
      m.put(1,"India");
      m.put(2,"US");
      m.put(3,"Australia");
      m.put(4,"Netherlands");
      m.put(5,"Canada");
      System.out.println("TreeMap Elements = "+m);
      System.out.println("Removing First Entry : "+m.pollFirstEntry());
      System.out.println("Updated TreeMap Elements = "+m);
   }
}

輸出

TreeMap Elements = {1=India, 2=US, 3=Australia, 4=Netherlands, 5=Canada}
Removing First Entry = 1=India
Updated TreeMap Elements = {2=US, 3=Australia, 4=Netherlands, 5=Canada}

更新於:2019 年 7 月 30 日

已瀏覽 966 次

開啟您的 職業生涯

完成課程並獲得認證

開始學習
廣告
© . All rights reserved.