3

I don't understand how NavigableMap.floorEntry() and ceiling Entry() work.

Oracle simply writes about ceilingEntry(key):

Returns a key-value mapping associated with the least key greater than or equal to the given key, or null if there is no such key.

Experimentally I learned that if I define NavigableMap with integer key (NavigableMap<Integer, Anything>), floorEntry(keyArg) and ceilingEntry(keyArg) always return the entry with given argument (keyArg)*

I understand that - it works like floor and ceiling functions in math: those functions return closest integer (if given not integer argument), given an integer argument they always return this given argument.

I thought (based on floor/entry definition in math) that snippet below would return "integer" 1.0, 2.0, etc.

Double 2.0 is not an integer. But ceilingEntry(keyArg) is of no use with Integer keys, if it also does not work with Double keys (also returning its argument key) then how is it used, what is the use of it and working use cases?

    NavigableMap<Double, String> m = new TreeMap<>();
    m.put(1.3, "hey");
    m.put(1.5, "hey2");
    m.put(1.62, "hey3"); // not this 
    m.put(1.6, "hey3");     // argument given to ceilingEntry
    m.put(1.68, "hey3"); // not this
    m.put(1.7, "hey4");  // not this
    m.put(2.0, "hey4");  // not this
    m.put(1.0, "hey4");
    m.put(3d, "hey4");   // not this
    m.put(0d, "hey4");
                         // WHAT THEN ???    

    System.out.println(m.ceilingEntry(1.6)); // OUTPUT: 1.6=hey3

    // I guess, to get something different from argument 
    //I shall give something not equal to any key as argument

    System.out.println(m.ceilingEntry(1.601)); // 1.62=hey3

I got it. Whatever the key type, if exactly such key is already present in the map, ceilingEntry would simply return its argument.

It would return anything different only if the given argument is NOT present within map (as key). In this case ceilingEntry would return closest key in the map (in case of ceilingEntry - searching in ascending order (towards positive infinity)).

So it is quite unlike pure math floor/ceiling - java is not looking for closest integer, java is looking for the closest existing key. Java does not care, whether this closest key is integer or 0.000786 (which is closest to arguments like 0.000785 or0.7)

Abraham
  • 8,525
  • 5
  • 47
  • 53
Code Complete
  • 3,146
  • 1
  • 15
  • 38

3 Answers3

3

The way floorEntry() and ceilingEntry() work resembles Math.floor() and Math.ceiling() in the following manner:

Math:

Integers:
1
2          <-- floor(2.5) = 2
           <-- Key: 2.5
3          <-- ceil(2.5) = 3
4

Similarly, NavigableMap:

Entries:
Alice
Barry         <-- floor(Bert) = Barry
              <-- Key: Bert
Constance     <-- ceil(Bert) = Constance
Dan
DodgyCodeException
  • 5,963
  • 3
  • 21
  • 42
1

Ceiling does just what it says: Returns a key-value mapping associated with the least key greater than or equal to the given key, or null if there is no such key. Nothing to do with integers and doubles. So if I have 2 keys in the map 2 and 5 and I call Ceiling(3), I will get 5

Jacob Botuck
  • 411
  • 6
  • 22
1

ceilingEntry(K key) Returns a key-value mapping associated with the least key greater than or equal to the given key, or null if there is no such key.

It works like below

    NavigableMap<Double, String> rankMap = new TreeMap<Double, String>();
    rankMap.put(1.0, "Student1");
    rankMap.put(2.0, "Student2");
    rankMap.put(3.0, "Student3");
    rankMap.put(4.0, "Student4");
    rankMap.put(5.0, "Student5");

    System.out.println(rankMap.ceilingEntry(1.5));//Output 2.0=Student2
    System.out.println(rankMap.floorEntry(1.5)); //1.0=Student1
Satya Mahesh
  • 339
  • 1
  • 3
  • 14