Get lower key from NavigableMap in Java

Java 8Object Oriented ProgrammingProgramming

<p>To get lower key means returning the greatest key strictly less than the given key. This can be done using lowerKey() method.</p><p>The following is an example to get lower key from NavigableMap &minus;</p><h2>Example</h2><p><a class="demo" href="" rel="nofollow" target="_blank">&nbsp;Live Demo</a></p><pre class="prettyprint notranslate">import java.util.*; public class Demo { public static void main(String[] args) { NavigableMap&lt;Integer, String&gt; n = new TreeMap&lt;Integer, String&gt;(); n.put(5, &quot;Tom&quot;); n.put(9, &quot;John&quot;); n.put(14, &quot;Jamie&quot;); n.put(1, &quot;Tim&quot;); n.put(4, &quot;Jackie&quot;); n.put(15, &quot;Kurt&quot;); n.put(30, &quot;Tiger&quot;); n.put(24, &quot;Jacob&quot;); System.out.println(&quot;NavigableMap elements... &quot;+n); System.out.println(&quot;Lower Key = &quot;+n.lowerKey(11)); } }</pre><h2>Output</h2><pre class="result notranslate">NavigableMap elements... {1=Tim, 4=Jackie, 5=Tom, 9=John, 14=Jamie, 15=Kurt, 24=Jacob, 30=Tiger} Lower Key = 9</pre>
Updated on 30-Jul-2019 22:30:24