• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Junilu Lacar
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Paweł Baczyński
  • Piet Souris
  • Vijitha Kumara

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

 
Ranch Hand
Posts: 93
Eclipse IDE VI Editor Linux
  • 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
 
lowercase baba
Posts: 12773
51
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The subset method wouldn't work?
 
Master Rancher
Posts: 3397
33
  • 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
Master Rancher
Posts: 3397
33
  • 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?
 
Sheriff
Posts: 21817
104
Eclipse IDE Spring VI Editor Chrome Java Ubuntu 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
Master Rancher
Posts: 3397
33
  • 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
Posts: 12773
51
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
Posts: 21817
104
Eclipse IDE Spring VI Editor Chrome Java Ubuntu 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
 
Destroy anything that stands in your way. Except this tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!