| Author |
K&B-6-pg-591 - Backed Collections - exam watch
|
Leonid Shchervinsky
Ranch Hand
Joined: Sep 07, 2006
Posts: 32
|
|
Quote from K&B-6-pg-591 - Backed Collections - exam watch: ========================================================== "... . So it's most likely that invoking pollFirstXxx() on the copy (assuming - on the backed collection) will remove an entry from both collections, but invoking pollFirstXxx() on the original will remove only the entry form the original." Since I created a backed map using say, sumMap() method on a TreeMap instance wich returns a SortedMap, and SortedMap does not have pollFirstXxx() method, that would not be a good example. What would be an example of a backed collection which does have pollFirstXxx() methods? Thanks Leonid
|
<a href="http://www.shchervinsky.com/java/" target="_blank" rel="nofollow">http://www.shchervinsky.com/java/</a>
|
 |
Ankit Garg
Saloon Keeper
Joined: Aug 03, 2008
Posts: 9189
|
|
Well to actually use pollFirstXXX() method, first of all you need to have JDK 1.6.. Then you will have to retrieve a backed set or map using the new subSet or subMap method which takes 4 arguments. They will return a TreeSet and TreeMap respectively, then you can use pollFirstXXX and pollLastXXX methods on them... The list of methods is given on page 588 and 590...
|
SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
|
 |
Leonid Shchervinsky
Ranch Hand
Joined: Sep 07, 2006
Posts: 32
|
|
|
except that submap returns SortedMap and SortedMap does not have pollxxxxEntry()...
|
 |
Ankit Garg
Saloon Keeper
Joined: Aug 03, 2008
Posts: 9189
|
|
Originally posted by Leonid Shchervinsky: except that submap returns SortedMap and SortedMap does not have pollxxxxEntry()...
I didn't understand what you said...but what I am saying is that you must use NavigableMap subMap(start, boolean, end, boolean); and NavigableSet subSet(start, boolean, end, boolean); then you can call pollFirstXXX and pollLastXXX methods on the set or map returned...
|
 |
Leonid Shchervinsky
Ranch Hand
Joined: Sep 07, 2006
Posts: 32
|
|
Originally posted by Ankit Garg: I didn't understand what you said...but what I am saying is that you must use NavigableMap subMap(start, boolean, end, boolean); and NavigableSet subSet(start, boolean, end, boolean); then you can call pollFirstXXX and pollLastXXX methods on the set or map returned...
NavigableMap is implemented by SortedMap, but SortedMap does not implement pollFirstXXX and pollLastXXX methods. If you think this should work, can you please show a code snippet? Thanks Leonid
|
 |
Ankit Garg
Saloon Keeper
Joined: Aug 03, 2008
Posts: 9189
|
|
If you see the documentation(which obviously you didn't), NavigableMap implements SortedMap and not the reverse.
|
 |
Leonid Shchervinsky
Ranch Hand
Joined: Sep 07, 2006
Posts: 32
|
|
Originally posted by Ankit Garg:
If you see the documentation(which obviously you didn't), NavigableMap implements SortedMap and not the reverse.
|
 |
Ankit Garg
Saloon Keeper
Joined: Aug 03, 2008
Posts: 9189
|
|
Leonid why don't you try to understand. There are two subMap methods in TreeMap and two subSet in TreeSet class. TreeMap subMap(Object start, boolean inclusive, Object end, boolean inclusive) SortedMap subMap(Object start, Object end) TreeSet subSet(Object start, boolean inclusive, Object end, boolean inclusive) SortedSet subSet(Object start, Object end) In your code just change these lines SortedMap<String, String> submap; submap = map.subMap("a", "h"); to TreeMap<String, String> subMap; subMap = map.subMap("a", true, "h", true);
|
 |
Leonid Shchervinsky
Ranch Hand
Joined: Sep 07, 2006
Posts: 32
|
|
Originally posted by Ankit Garg: Leonid why don't you try to understand. There are two subMap methods in TreeMap and two subSet in TreeSet class. TreeMap subMap(Object start, boolean inclusive, Object end, boolean inclusive) SortedMap subMap(Object start, Object end) TreeSet subSet(Object start, boolean inclusive, Object end, boolean inclusive) SortedSet subSet(Object start, Object end) In your code just change these lines SortedMap<String, String> submap; submap = map.subMap("a", "h"); to TreeMap<String, String> subMap; subMap = map.subMap("a", true, "h", true);
Thanks, Ankit: I am trying :-) I have just changed the two lines as you recommended: TreeMap<String, String> submap; submap = map.subMap("a", true, "h", true); // error Getting: "incompatible types: found NavigatableMap, required: TreeMap" Can you please run this code and see what is up? Thanks Leonid
|
 |
Leonid Shchervinsky
Ranch Hand
Joined: Sep 07, 2006
Posts: 32
|
|
Thanks, Ankit for trying to help me out. Am I totally off base here? Are there any bartenders on this forum? Thanks Leonid
|
 |
Ankit Garg
Saloon Keeper
Joined: Aug 03, 2008
Posts: 9189
|
|
Just change the code to NavigableMap<String, String> submap; submap = map.subMap("a", true, "h", true); // error
|
 |
Terence Gronowski
Ranch Hand
Joined: Dec 19, 2007
Posts: 64
|
|
I wonder what kind of backed (or related) situations exist. It seems to be a very important Issue at SCJP 6.
Here some situations I know they exist (does somebody know other?) :?:
a) TreeSet (Methods headSet, tailSet, subSet): Adding within range allowed, out of range => key out of range Exception
Example:
b) TreeMap (Methods headMap, tailMap, subMap): Adding within range allowed, out of range => key out of range Exception (same as above)
Example:
c) toArray() results in a fixed sized array (arrays are always fixed!). Altering of array-element allowed. Changing of ArrayList has no influence on array.
d) Arrays.asList(): Not allowed to add new element to list, as list comes from fixed array. Changing of list allowed, changes array as it is backed.
Answers welcome to have more systematics in this topic!
|
Docendo discimus (we learn by teaching)
SCJP 5 Zertifizierung, Vorbereitungsbuch; SCJP 6 Zertifizierung, Vorbereitungsbuch
|
 |
Bert Bates
author
Sheriff
Joined: Oct 14, 2002
Posts: 8712
|
|
Hey Leonid,
I haven't followed this thread in detail, but you're in good hands with Ankit!
Bert
|
Eliminate fossil fuel subsidies. (If you're not on the edge, you're taking up too much room.)
|
 |
Jesper de Jong
Java Cowboy
Bartender
Joined: Aug 16, 2005
Posts: 12911
|
|
|
Please use code tags when you post source code.
|
Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
|
 |
 |
|
|
subject: K&B-6-pg-591 - Backed Collections - exam watch
|
|
|