Java TreeMap pollLastEntry() 方法



描述

Java TreeMap pollLastEntry() 方法用於移除並返回此對映中最大鍵關聯的鍵值對映,如果對映為空則返回 null。

宣告

以下是 java.util.TreeMap.pollLastEntry() 方法的宣告。

public Map.Entry<K,V> pollLastEntry()

引數

返回值

方法呼叫返回此對映中移除的最後一個條目,如果此對映為空則返回 null。

異常

從 Integer,Integer 對的 TreeMap 獲取最後一個條目示例

以下示例演示了 Java TreeMap pollLastEntry() 方法的使用,用於移除並獲取此對映中最大鍵關聯的鍵值對映。我們建立了一個 Integer,Integer 對的 TreeMap 物件。然後添加了一些條目,列印對映,然後使用 pollLastEntry() 列印給定鍵的返回值,並再次列印對映以檢視移除條目的效果。

package com.tutorialspoint;

import java.util.TreeMap;

public class TreeMapDemo {
   public static void main(String[] args) {

      // creating tree map 
      TreeMap<Integer, Integer> treemap = new TreeMap<>();

      // populating tree map
      treemap.put(2, 2);
      treemap.put(1, 1);
      treemap.put(3, 3);
      treemap.put(6, 6);
      treemap.put(5, 5);   

      // polling last entry
      System.out.println("Value before poll: "+ treemap);            
      System.out.println("Value returned: "+ treemap.pollLastEntry());      
      System.out.println("Value after poll: "+ treemap);
   }    
}

輸出

讓我們編譯並執行上述程式,這將產生以下結果。

Value before poll: {1=1, 2=2, 3=3, 5=5, 6=6}
Value returned: 6=6
Value after poll: {1=1, 2=2, 3=3, 5=5}

從 Integer,String 對的 TreeMap 獲取最後一個條目示例

以下示例演示了 Java TreeMap pollLastEntry() 方法的使用,用於移除並獲取此對映中最大鍵關聯的鍵值對映。我們建立了一個 Integer,String 對的 TreeMap 物件。然後添加了一些條目,列印對映,然後使用 pollLastEntry() 列印給定鍵的返回值,並再次列印對映以檢視移除條目的效果。

package com.tutorialspoint;

import java.util.TreeMap;

public class TreeMapDemo {
   public static void main(String[] args) {

      // creating tree map 
      TreeMap<Integer, String> treemap = new TreeMap<>();

      // populating tree map
      treemap.put(2, "two");
      treemap.put(1, "one");
      treemap.put(3, "three");
      treemap.put(6, "six");
      treemap.put(5, "five");   

      // polling last entry
      System.out.println("Value before poll: "+ treemap);            
      System.out.println("Value returned: "+ treemap.pollLastEntry());      
      System.out.println("Value after poll: "+ treemap);
   }    
}

輸出

讓我們編譯並執行上述程式,這將產生以下結果。

Value before poll: {1=one, 2=two, 3=three, 5=five, 6=six}
Value returned: 6=six
Value after poll: {1=one, 2=two, 3=three, 5=five}

從 Integer,Object 對的 TreeMap 獲取最後一個條目示例

以下示例演示了 Java TreeMap pollLastEntry() 方法的使用,用於移除並獲取此對映中最大鍵關聯的鍵值對映。我們建立了一個 Integer,Student 對的 TreeMap 物件。然後添加了一些條目,列印對映,然後使用 pollLastEntry() 列印給定鍵的返回值,並再次列印對映以檢視移除條目的效果。

package com.tutorialspoint;

import java.util.TreeMap;

public class TreeMapDemo {
   public static void main(String[] args) {

      // creating tree map 
      TreeMap<Integer, Student> treemap = new TreeMap<>();

      // populating tree map
      treemap.put(2, new Student(2, "Robert"));
      treemap.put(1, new Student(1, "Julie"));  
      treemap.put(3, new Student(3, "Adam"));
      treemap.put(6, new Student(6, "Julia"));
      treemap.put(5, new Student(5, "Tom")); 

      // polling last entry
      System.out.println("Value before poll: "+ treemap);            
      System.out.println("Value returned: "+ treemap.pollLastEntry());      
      System.out.println("Value after poll: "+ treemap);
   }    
}
class Student {
   int rollNo;
   String name;

   Student(int rollNo, String name){
      this.rollNo = rollNo;
      this.name = name;
   }

   @Override
   public String toString() {
      return "[ " + this.rollNo + ", " + this.name + " ]";
   }
   @Override
   public boolean equals(Object obj) {
      if(obj == null) return false;
      Student s = (Student)obj;
      return this.rollNo == s.rollNo && this.name.equalsIgnoreCase(s.name);
   }
}

輸出

讓我們編譯並執行上述程式,這將產生以下結果。

Value before poll: {1=[ 1, Julie ], 2=[ 2, Robert ], 3=[ 3, Adam ], 5=[ 5, Tom ], 6=[ 6, Julia ]}
Value returned: 6=[ 6, Julia ]
Value after poll: {1=[ 1, Julie ], 2=[ 2, Robert ], 3=[ 3, Adam ], 5=[ 5, Tom ]}
java_util_treemap.htm
廣告

© . All rights reserved.