Results 1 to 6 of 6
- 10-13-2010, 09:05 PM #1
Member
- Join Date
- Oct 2010
- Posts
- 35
- Rep Power
- 0
- 10-13-2010, 10:19 PM #2
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,537
- Rep Power
- 11
The problem is less one of faster random access than it is of having index based access at all! In the standard Java collection API a TreeMap is not a List. And this applies to maps generally: they are sorted by the natural order of their keys and not by index. They don't offer an indexOf() or get().
You might want to look at Google Collections.
- 10-13-2010, 10:56 PM #3
Member
- Join Date
- Oct 2010
- Posts
- 35
- Rep Power
- 0
- 10-13-2010, 11:41 PM #4
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,537
- Rep Power
- 11
entrySet() actually returns a Set<Map.Entry<K,V>> (informally, a set of entries) rather than any old Collection. But, again, the lack of index based access isn't an implementation detail - it doesn't do this because a tree of some sort is involved in its implementation - rather it follows from the fact that a Map (any map) offers no index based access.
I should have mentioned that I posted the google link not because I know it has such a collection lurking there somewhere, but because it is more modern. If you can't what you want there (a map with index based access), it might be time to question whether such a thing is the way to go about whatever it is that you are going about.
- 10-13-2010, 11:55 PM #5
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,537
- Rep Power
- 11
If you are adding and removing elements only very rarely why not have a class that contains *both* a Map (or SortedMap of some variety) and a List of keys (or entries). Use the map to provide map-like functionality and the list to provide index based access. The list would only have to be updated on the rare occasions that the collection's content changes.
- 10-14-2010, 02:23 PM #6
Member
- Join Date
- Oct 2010
- Posts
- 35
- Rep Power
- 0
This would work, but it would duplicate the memory used by the lists and I can't afford this in the application I am developing. I decided to write my own map class finally. It uses also primitives instead of a generics when possible, so it will be a more memory efficient and optimal class for this particular application.
Thanks
Similar Threads
-
Decimal to Binary "Using Array"
By pinkdreammsss in forum Java AppletsReplies: 10Last Post: 04-23-2010, 06:21 PM -
Google Error - "This site may harm your computer" on every search result?!?!
By angryboy in forum Forum LobbyReplies: 2Last Post: 03-18-2009, 08:36 PM -
MoneyOut.println("It took you (whats wrong?>",year,"<WW?) years to repay the loan")
By soc86 in forum New To JavaReplies: 2Last Post: 01-24-2009, 06:56 PM -
Eclispe: "Launch failed. Binary not found"
By qwertyuiop23 in forum EclipseReplies: 1Last Post: 11-16-2008, 06:06 AM -
the dollar sign "$", prints like any other normal char in java like "a" or "*" ?
By lse123 in forum New To JavaReplies: 1Last Post: 10-20-2008, 07:35 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks