1 Reply Latest reply: Nov 30, 2011 1:00 AM by Kayaman RSS

    How Backed Collections work

    797326
      Hi Guys,

      I have posted this in another forum, and I am posting this here now: I was tring out some code from the Sera Bates SCJP 6 book. I have some difficulty understanding how the code works or why it works in that way. Do have a look at the code:



      import java.util.SortedMap;

      import java.util.TreeMap;



      *public class BackedCollections {*


      *public static void main(String[] args) {*


      TreeMap<String, String> map = new TreeMap<String, String>();

      map.put("a", "ant");

      map.put("d", "dog");

      map.put("h", "horse");


      SortedMap<String, String> submap;

      submap = map.subMap("b", "g");


      System.out.println(map + " " + submap);


      *}*

      *}*

      The out put for this code is :

      +{a=ant, d=dog, h=horse} {d=dog}+

      I don't understand the contents of the second curly brace {d=dog}. If I am not wrong, this is coming from the submap, but didnt the sub map just do a ("b", "g")??? I hope somone can help explain. Thanks.
        • 1. Re: How Backed Collections work
          Kayaman
          JavaIsLife wrote:
          I don't understand the contents of the second curly brace {d=dog}. If I am not wrong, this is coming from the submap, but didnt the sub map just do a ("b", "g")??? I hope somone can help explain. Thanks.
          What did you expect it to do? You asked for a submap that contains elements from key "b" to key "g", which in this case gives you only the "d=dog" entry.

          Did you read the Javadoc and if not, why? (And if yes, was it too difficult to understand?)

          Also your example doesn't really have anything to do with backed collections, until you make changes to the original map and show how those affect the submap.

          P.S. How about posting in the "New to Java" or "Java Programming" forum instead of here. I'll ask an admin to move this thread.

          Edited by: Kayaman on Nov 30, 2011 9:00 AM