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
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Beginning Java
Reply 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: 91

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: 9948
    
    6

The subset method wouldn't work?


Never ascribe to malice that which can be adequately explained by stupidity.
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 2780
    
    2
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: 2780
    
    2
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: 19216

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
How To Ask Questions How To Answer Questions
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 2780
    
    2
Rob Spoor wrote:


I think this needs to be something like:

fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 9948
    
    6

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: 19216

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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Why does SortedSet class has no .get(location) method?
 
Similar Threads
Why I can apply "this" ?
Collections API
Array Question
Copying objects from one Arraylist to another Arraylist
An easy question about swap