How to use subSet() method of Java NavigableSet Class

Java 8Object Oriented ProgrammingProgramming

<p>Use the subset() method to get elements from a limit. At first, create NavigableSet and add elements &minus;</p><pre class="result notranslate">NavigableSet&lt;Integer&gt; set = new TreeSet&lt;&gt;(); set.add(10); set.add(25); set.add(40); set.add(55); set.add(70); set.add(85);</pre><p>Now, use the subset() method &minus;</p><pre class="result notranslate">set.subSet(40, 85)</pre><p>The following is an example to implement subset() method of Java NaviagbleSet class &minus;</p><h2>Example</h2><p><a class="demo" href="http://tpcg.io/DOP9iS" rel="nofollow" target="_blank">&nbsp;Live Demo</a></p><pre class="prettyprint notranslate">import java.util.NavigableSet; import java.util.TreeSet; public class Demo { public static void main(String[] args) { NavigableSet&lt;Integer&gt; set = new TreeSet&lt;&gt;(); set.add(10); set.add(25); set.add(40); set.add(55); set.add(70); set.add(85); set.add(100); System.out.println(&quot;Returned Value = &quot; + set.subSet(40, 85)); } }</pre><h2>Output</h2><pre class="result notranslate">Returned Value = [40, 55, 70]</pre>
raja
Updated on 30-Jul-2019 22:30:24

Advertisements