在許多地方都使用找到小於給定值的最大數,而將其放在基於Map的容器中始終是加號。 Java.util.TreeMap也使用floor()函數提供此函數。有2個變體,下麵都會討論。
1. floorEntry():它返回與小於或等於給定鍵的最大鍵關聯的鍵值映射,如果沒有這樣的鍵,則返回null。
參數: key:This is the key to be matched. 返回值: It returns an entry with the greatest key less than or equal to key, or null if there is no such key. Exception: ClassCastException:This exception is thrown if the specified key cannot be compared with the keys currently in the map. NullPointerException :This exception is thrown if the specified key is null and this map uses natural ordering, or its comparator does not permit null keys.
// Java code to demonstrate the working
// of floorEntry()
import java.io.*;
import java.util.*;
public class floorEntry {
public static void main(String[] args)
{
// Declaring the tree map of Integer and String
TreeMap<Integer, String> tmp = new TreeMap<Integer, String>();
// assigning the values in the tree map
// using put()
tmp.put(10, "ten");
tmp.put(7, "seven");
tmp.put(19, "nineteen");
tmp.put(3, "three");
// use of floorEntry()
// displays the floor value of 6
// prints 3=three
System.out.println("The greatest key-value less than 6 is:"
+ tmp.floorEntry(6));
}
}
輸出:
The greatest key-value less than 6 is:3=three
2. floorKey():它返回小於或等於給定鍵的最大鍵;如果沒有這樣的鍵,則返回null。
參數: key:This is the key to be matched. 返回值: It returns an entry with the greatest key less than or equal to key, or null if there is no such key. Exception: ClassCastException:This exception is thrown if the specified key cannot be compared with the keys currently in the map. NullPointerException :This exception is thrown if the specified key is null and this map uses natural ordering, or its comparator does not permit null keys.
// Java code to demonstrate the working
// of floorKey()
import java.io.*;
import java.util.*;
public class floorKey {
public static void main(String[] args)
{
// Declaring the tree map of Integer and String
TreeMap<Integer, String> tmp = new TreeMap<Integer, String>();
// assigning the values in the tree map
// using put()
tmp.put(10, "ten");
tmp.put(7, "seven");
tmp.put(19, "nineteen");
tmp.put(3, "three");
// use of floorKey()
// displays the floor key of
// prints 3
System.out.println("The greatest key less than 6 is:"
+ tmp.floorKey(6));
}
}
輸出:
The greatest key less than 6 is:3
實際應用:該函數的可能應用很多。從最大距離開始使用給定的汽油或使用給定成分的可能菜肴。前一個在下麵的代碼中討論。
// Java code to demonstrate the application
// of floorKey() and floorEntry
import java.io.*;
import java.util.*;
public class floorappli {
public static void main(String[] args)
{
// Declaring the tree map of Integer and String
TreeMap<Integer, String> destin = new TreeMap<Integer, String>();
// assigning the km with destination
// from beginning
// using put()
destin.put(10, "Delhi");
destin.put(7, "Gurgaon");
destin.put(19, "Noida");
destin.put(3, "Ring Road");
// Entering the available Petrol (consider 1km/litre)
int petr = 12;
// Maximum place you can reach
System.out.println("The maximum place you can reach with given petrol:"
+ destin.floorEntry(petr));
}
}
輸出:
The maximum place you can reach with given petrol:10=Delhi
相關用法
- Java TreeMap floorKey()用法及代碼示例
- Java NavigableMap floorKey()用法及代碼示例
- Java Java lang.Long.numberOfTrailingZeros()用法及代碼示例
- Java Java lang.Long.lowestOneBit()用法及代碼示例
- Java Java.util.Collections.rotate()用法及代碼示例
- Java Java.util.concurrent.Phaser用法及代碼示例
- Java Java.util.function.LongPredicate用法及代碼示例
- Java Java.util.function.BiPredicate用法及代碼示例
- Java Java.util.Collections.disjoint()用法及代碼示例
- Java Java.util.function.IntPredicate用法及代碼示例
- Java Java.util.function.DoublePredicate用法及代碼示例
注:本文由純淨天空篩選整理自Shambhavi Singh 1大神的英文原創作品 Java.util.TreeMap.floorEntry() and floorKey() in Java。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。