aspose file tools*
The moose likes Beginning Java and the fly likes Why does SortedSet class has no .get(location) method? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Why does SortedSet class has no .get(location) method?" Watch "Why does SortedSet class has no .get(location) method?" New topic
Author

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

Aditya Sirohi
Ranch Hand

Joined: Jan 05, 2010
Posts: 93

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

Joined: Oct 02, 2003
Posts: 11480
    
  16

The subset method wouldn't work?


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 3018
    
  10
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

Joined: Mar 05, 2008
Posts: 3018
    
  10
I don't see how a subset method would work - how would you use an index in that?
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19762
    
  20

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 3018
    
  10
Rob Spoor wrote:


I think this needs to be something like:

fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11480
    
  16

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

Joined: Oct 27, 2005
Posts: 19762
    
  20

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
 
 
subject: Why does SortedSet class has no .get(location) method?