當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


Java TreeMap.pollFirstEntry()、pollLastEntry()用法及代碼示例


Java.util.TreeMap還包含支持在值的高端和低端進行檢索和刪除的函數,因此在適用性和日常使用方麵具有很大的靈活性。此函數是poll(),並具有本文討論的2個變體。

1. pollFirstEntry():它刪除並檢索映射中鍵值最小的鍵值對,並且“null”為map為空。

用法:
public Map.Entry pollFirstEntry()
參數:
NA
返回值:
Retrieves and removes the least key-value if map is filled else returns null.
Exception:
NA

// Java code to demonstrate the working 
// of pollFirstEntry() 
import java.io.*; 
import java.util.*; 
public class pollFirstEntry { 
    public static void main(String[] args) 
    { 
  
        // Declaring the tree map of String and Integer 
        TreeMap<String, Integer> tmp = new TreeMap<String, Integer>(); 
  
        // Trying to retrieve and remove in empty map 
        // returns null 
        System.out.println 
        ("The smallest key value pair is:" + tmp.pollFirstEntry()); 
  
        // assigning the values in the tree map 
        // using put() 
        tmp.put("Geeks", 1); 
        tmp.put("for", 4); 
        tmp.put("geeks", 1); 
  
        // Printing the initial map 
        System.out.println 
        ("The initial Map before deletion is:" + tmp); 
  
        // Use of pollFirstEntry() 
        // Removes the first entry and returns the least key 
        // lexicographically smallest in case of String 
        // prints Geeks-1 
        System.out.println 
        ("The smallest key value pair is:" + tmp.pollFirstEntry()); 
  
        // Printing the map after deletion 
        System.out.println 
        ("The resultant Map after deletion is:" + tmp); 
    } 
}

輸出:

The smallest key value pair is:null
The initial Map before deletion is:{Geeks=1, for=4, geeks=1}
The smallest key value pair is:Geeks=1
The resultant Map after deletion is:{for=4, geeks=1}

2. pollLastEntry():刪除並檢索映射中具有最大鍵值的鍵值對,而“null”為map為空。


用法:
public Map.Entry pollLastEntry()
參數:
NA
返回值:
Retrieves and removes the largest key-value if map is filled else returns null.
Exception:
NA

// Java code to demonstrate the working 
// of pollLastEntry() 
import java.io.*; 
import java.util.*; 
public class pollLastEntry { 
    public static void main(String[] args) 
    { 
  
        // Declaring the tree map of String and Integer 
        TreeMap<String, Integer> tmp = new TreeMap<String, Integer>(); 
  
        // Trying to retrieve and remove in empty map 
        // returns null 
        System.out.println 
        ("The largest key value pair is:" + tmp.pollFirstEntry()); 
  
        // assigning the values in the tree map 
        // using put() 
        tmp.put("Geeks", 1); 
        tmp.put("for", 4); 
        tmp.put("geeks", 1); 
  
        // Printing the initial map 
        System.out.println 
        ("The initial Map before deletion is:" + tmp); 
  
        // Use of pollLastEntry() 
        // Removes the last(max) entry and returns the max key 
        // lexicographically greatest in case of String 
        // prints geeks-1 
        System.out.println 
        ("The largest key value pair is:" + tmp.pollLastEntry()); 
  
        // Printing the map after deletion 
        System.out.println 
        ("The resultant Map after deletion is:" + tmp); 
    } 
}

輸出:

The largest key value pair is:null
The initial Map before deletion is:{Geeks=1, for=4, geeks=1}
The largest key value pair is:geeks=1
The resultant Map after deletion is:{Geeks=1, for=4}

實際應用:使用雙端隊列或優先隊列的概念可以考慮許多應用。下麵的代碼顯示了一個這樣的示例。

// Java code to demonstrate the application 
// of pollLastEntry() and pollFirstEntry() 
import java.io.*; 
import java.util.*; 
public class pollAppli { 
    public static void main(String[] args) 
    { 
  
        // Declaring the tree map of Integer and String 
        TreeMap<Integer, String> que = new TreeMap<Integer, String>(); 
  
        // assigning the values in que 
        // using put() 
        que.put(10, "astha"); 
        que.put(4, "shambhavi"); 
        que.put(7, "manjeet"); 
        que.put(8, "nikhil"); 
  
        // Defining the priority 
        // takes highest value, if priority is high 
        // else takes lowest value 
        String prio = "high"; 
  
        // Printing the initial queue 
        System.out.println("The initial queue is:" + que); 
        if (prio == "high") { 
            System.out.println 
            ("The largest valued person is:" + que.pollLastEntry()); 
            System.out.println 
            ("The resultant queue after deletion is:" + que); 
        } 
        else { 
            System.out.println 
            ("The lowest valued person is:" + que.pollFirstEntry()); 
            System.out.println 
            ("The resultant queue after deletion is:" + que); 
        } 
    } 
}

輸出:

The initial queue is:{4=shambhavi, 7=manjeet, 8=nikhil, 10=astha}
The largest valued person is:10=astha
The resultant queue after deletion is:{4=shambhavi, 7=manjeet, 8=nikhil}



相關用法


注:本文由純淨天空篩選整理自Shambhavi Singh 1大神的英文原創作品 Java.util.TreeMap.pollFirstEntry() and pollLastEntry() in Java。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。