Results 1 to 7 of 7
  1. #1
    Join Date
    Aug 2013
    Posts
    108
    Rep Power
    0

    Default SortedMap interface

    From the complete reference book:

    Sorted maps allow very efficient manipulations of submaps (in other words, subsets of a map). To obtain
    a submap, use headMap( ), tailMap( ), or subMap( ). The submap returned by these methods is backed by
    the invoking map. Changing one changes the other.
    To get the first key in the set, call firstKey( ). To get
    the last key, use lastKey( ).
    I am not able to understand the part in bold. Someone pls guide me.

    The original TreeMap is not changed by calling headMap() function. See example code:

    Java Code:
    import java.util.SortedMap;
    import java.util.TreeMap;
      
    public class Main {    
     public static void main(String[] args) {  
    	 SortedMap<String, String> sm=new TreeMap<String, String>();
    	 sm.put("A", "one");
    	 sm.put("B", "two");
    	 sm.put("C", "three");
    	 sm.put("A", "four");
    	 sm.put("D", "five");
    	 System.out.println(sm);
    	 System.out.println(sm.headMap("C"));
    	 System.out.println(sm);
     }    
    }
    Last edited by suhaas.mohandos@gmail.com; 08-23-2013 at 03:09 AM.

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,415
    Rep Power
    5

    Default Re: SortedMap interface

    As you said, you are not understanding the bold type. You have not changed anything. If you change part of the submap then the invoking map will also be changed. Here is an example:

    Java Code:
    import java.util.SortedMap;
    import java.util.TreeMap;
    
    public class Main {
       public static void main(String[] args) {
          SortedMap<String, String> sm = new TreeMap<String, String>();
          sm.put("A", "one");
          sm.put("B", "two");
          sm.put("C", "three");
          sm.put("A", "four");
          sm.put("D", "five");
          SortedMap<String,String> submap = sm.headMap("C"); // obtain submap
          submap.put("B", "Elephant"); // change "B" in submap
          System.out.println(sm.get("B")); // change is now reflected in invoking map
       }
    }
    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  3. #3
    Join Date
    Aug 2013
    Posts
    108
    Rep Power
    0

    Default Re: SortedMap interface

    Ok. But we can't put any other key in the submap other than the those present the submap or what? What if I want to add a new key in the submap without actually modifying the original map?

    Error generating code:
    Java Code:
    import java.util.SortedMap;
    import java.util.TreeMap;
     
    public class Main {
       public static void main(String[] args) {
          SortedMap<String, String> sm = new TreeMap<String, String>();
          sm.put("A", "one");
          sm.put("B", "two");
          sm.put("C", "three");
          sm.put("A", "four");
          sm.put("D", "five");
          SortedMap<String,String> submap = sm.headMap("C"); // obtain submap
          System.out.println(submap);
          submap.put("D", "Elephant");
          System.out.println(submap);
          System.out.println(sm); // change is now reflected in invoking map
       }
    }
    Output:
    {A=four, B=two}
    Exception in thread "main" java.lang.IllegalArgumentException: key out of range
    at java.util.TreeMap$NavigableSubMap.put(Unknown Source)
    at Main.main(Main.java:14)

  4. #4
    gimbal2 is online now Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,749
    Rep Power
    5

    Default Re: SortedMap interface

    Quote Originally Posted by suhaas.mohandos@gmail.com View Post
    Ok. But we can't put any other key in the submap other than the those present the submap or what? What if I want to add a new key in the submap without actually modifying the original map?
    So did you read and attempt to understand the API documentation of the headMap() method? Because then I am really confused why you need to be asking this. Let me make a quote which makes me believe you did in fact yet again not read the API docs:

    "so changes in the returned map are reflected in this map, and vice-versa"
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  5. #5
    Join Date
    Aug 2013
    Posts
    108
    Rep Power
    0

    Default Re: SortedMap interface

    I think you misunderstood it. I am saying what if I want to enter some new key in the submap without directly modifying the original map(indirectly if it modifies I am fine). Is it possible or not?

  6. #6
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,371
    Blog Entries
    7
    Rep Power
    20

    Default Re: SortedMap interface

    Quote Originally Posted by suhaas.mohandos@gmail.com View Post
    I think you misunderstood it. I am saying what if I want to enter some new key in the submap without directly modifying the original map(indirectly if it modifies I am fine). Is it possible or not?
    Then you have to make a copy of the sub-map before you change it (the copy) just because of its defined behaviour; you can't have both.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    gimbal2 is online now Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,749
    Rep Power
    5

    Default Re: SortedMap interface

    Quote Originally Posted by suhaas.mohandos@gmail.com View Post
    I think you misunderstood it. I am saying what if I want to enter some new key in the submap without directly modifying the original map(indirectly if it modifies I am fine). Is it possible or not?
    And if you would have read the documentation, you would KNOW this is not possible using headMap().
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

Similar Threads

  1. Replies: 1
    Last Post: 07-08-2012, 05:23 PM
  2. interface help
    By alpdog14 in forum New To Java
    Replies: 3
    Last Post: 05-04-2011, 11:17 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •