TreeMap headMap() method in java

import java.util.SortedMap;
import java.util.TreeMap;
public class TreeMapHeadMapMethodExample
{
   public static void main(String[] args)
   {
      TreeMap<String, Integer> tm = new TreeMap<String, Integer>();
      // map string values to integer keys
      tm.put("mango", 65);
      tm.put("apple", 63);
      tm.put("grapes", 35);
      tm.put("pineapple", 60);
      tm.put("banana", 26);
      System.out.println("Given TreeMap is: " + tm);
      // create SortedMap for map head
      SortedMap<String, Integer> sm = new TreeMap<String, Integer>();
      sm = tm.headMap("pineapple");
      // Getting map head
      System.out.println("headmap is: " + sm);
   }
}

4.27
10
Jean-Paul 120 points

                                    import java.util.NavigableMap;
import java.util.TreeMap;
public class TreeMapHeadMapBooleanInclusiveMethod
{
   public static void main(String[] args)
   {
      TreeMap&lt;Integer, String&gt; tm = new TreeMap&lt;Integer, String&gt;();
      NavigableMap&lt;Integer, String&gt; nm = new TreeMap&lt;Integer, String&gt;();
      tm.put(96, &quot;violet&quot;);
      tm.put(93, &quot;green&quot;);
      tm.put(20, &quot;yellow&quot;);
      tm.put(36, &quot;red&quot;);
      tm.put(53, &quot;blue&quot;);
      // get head map inclusive 93
      nm = tm.headMap(93, true);
      System.out.println(&quot;Check values of TreeMap&quot;);
      System.out.println(&quot;Value is: &quot; + nm);
   }
}

4.27 (11 Votes)
0
4
10

                                    import java.util.SortedMap;
import java.util.TreeMap;
public class TreeMapHeadMapMethodExample
{
   public static void main(String[] args)
   {
      TreeMap&lt;Integer, String&gt; tm = new TreeMap&lt;Integer, String&gt;();
      // map string values to integer keys
      tm.put(65, &quot;mango&quot;);
      tm.put(63, &quot;apple&quot;);
      tm.put(35, &quot;grapes&quot;);
      tm.put(60, &quot;pineapple&quot;);
      tm.put(26, &quot;banana&quot;);
      System.out.println(&quot;Given TreeMap is: &quot; + tm);
      // create SortedMap for map head
      SortedMap&lt;Integer, String&gt; sm = new TreeMap&lt;Integer, String&gt;();
      sm = tm.headMap(60);
      // Getting map head
      System.out.println(&quot;headmap is: &quot; + sm);
   }
}

4 (10 Votes)
0
Are there any code examples left?
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source