Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Why does SortedSet class has no .get(location) method?

 
Aditya Sirohi
Ranch Hand
Posts: 93
Eclipse IDE Linux VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I have put some data in the SortedSet, but i can only iterate over it using an iterator. If i have to get a element on a certain index i need to first put elements from the SortedSet to ArrayList and then do a .get(location). Is there any other better less expensive way to get a element on a certain index in SortedSet?

-Adi
 
fred rosenberger
lowercase baba
Bartender
Posts: 12180
34
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The subset method wouldn't work?
 
Mike Simmons
Ranch Hand
Posts: 3090
14
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Fundamentally, a Set doesn't have an index for each position. If that's what you want, you should probably use a List instead.
 
Mike Simmons
Ranch Hand
Posts: 3090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't see how a subset method would work - how would you use an index in that?
 
Rob Spoor
Sheriff
Pie
Posts: 20605
60
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Aditya Sirohi wrote:I have put some data in the SortedSet, but i can only iterate over it using an iterator. If i have to get a element on a certain index i need to first put elements from the SortedSet to ArrayList and then do a .get(location). Is there any other better less expensive way to get a element on a certain index in SortedSet?

Iterate until you've reached the index:
This code is flawed as it doesn't check if index is valid, but you can use this basic principle.

This is slightly better than using an ArrayList, because with that you have to iterate over all the elements, even those past the index you want. After all, adding the SortedSet elements into the ArrayList requires all of the elements to be iterated over.
 
Mike Simmons
Ranch Hand
Posts: 3090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Spoor wrote:


I think this needs to be something like:

 
fred rosenberger
lowercase baba
Bartender
Posts: 12180
34
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mike Simmons wrote:I don't see how a subset method would work - how would you use an index in that?

I didn't read it closely enough. I thought you could say "from element 2 to element 3"
 
Rob Spoor
Sheriff
Pie
Posts: 20605
60
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mike Simmons wrote:
Rob Spoor wrote:


I think this needs to be something like:


Yes, of course... I forgot to decrement index, and index can of course be 0 as well
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic