• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

K&B-6-pg-591 - Backed Collections - exam watch

 
Leonid Shchervinsky
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Ankit Garg
Sheriff
Posts: 9528
32
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
Leonid Shchervinsky
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
except that submap returns SortedMap and SortedMap does not have pollxxxxEntry()...
 
Ankit Garg
Sheriff
Posts: 9528
32
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 9528
32
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you see the documentation(which obviously you didn't), NavigableMap implements SortedMap and not the reverse.

 
Leonid Shchervinsky
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ankit Garg:
If you see the documentation(which obviously you didn't), NavigableMap implements SortedMap and not the reverse.



 
Ankit Garg
Sheriff
Posts: 9528
32
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 9528
32
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just change the code to

NavigableMap<String, String> submap;
submap = map.subMap("a", true, "h", true); // error
 
Terence Gronowski
Ranch Hand
Posts: 64
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
Bert Bates
author
Sheriff
Posts: 8900
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey Leonid,

I haven't followed this thread in detail, but you're in good hands with Ankit!

Bert
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15441
41
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please use code tags when you post source code.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic