• Post Reply Bookmark Topic Watch Topic
  • New Topic

Maximum number of elements in a Treemap...  RSS feed

 
Nick St-Peter
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi!

I was wondering what is the maximum number of elements in a Treemap and if there's a place where these things are documented...

Thank you!

Nick
 
parshuram bingi
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I guess, there should not be any resctriction on the number of elements.
 
pascal betz
Ranch Hand
Posts: 547
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
since Map.size() returns an int... i guess it is Integer.MAX_VALUE....
http://java.sun.com/j2se/1.3/docs/api/java/util/Map.html

pascal
 
Nick St-Peter
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks you both for your help!

Nick
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is true to say that SortedMap (the interface to which TreeMap conforms) has an absolute limit of Integer.MAX_VALUE (about 2 billion) items, because that's the largest value it can return from size().

Depending on how TreeMap is implemented, it may have an internal limit of less than that.

In general, Java Collections can hold more items than it would ever be wise to put into them. Long before you get to hundreds of millions or even billions of entries, you're going to be using ludicrous amounts of memory, which few, if any current JVMs would support. Even if supported, the storage/retrieval performance would likely be awful. you should be considering another way of storing your data - maybe a SQL database - long before that.
[ August 14, 2006: Message edited by: Peter Chase ]
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Peter Chase:
Even if supported, the storage/retrieval performance would likely be awful. You should be considering another way of storing your data - maybe a SQL database - long before that.


What makes you think an SQL database would be faster than an in-memory datastructure???
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ilja Preuss:

What makes you think an SQL database would be faster than an in-memory datastructure???


Regarding performance, a huge in-memory data structure in a TreeMap would be (a lot) faster at retrieving via the map key or retrieving elements in order.

But any other type of retrieval would tend to require iteration through all the elements, which would be very slow. In contrast, databases can be indexed in all sorts of different ways, to enable fast retrieval of data in a wide variety of ways.

Additionally, database indexes are usually persistent, so they do not need to be regenerated with each run of the application, like a TreeMap would. In a real application, that could be a huge performance advantage.

But the main reason for swapping to a database (or some other file-based approach) would be that the huge data simply would not fit in memory.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Peter Chase:
But any other type of retrieval would tend to require iteration through all the elements, which would be very slow. In contrast, databases can be indexed in all sorts of different ways, to enable fast retrieval of data in a wide variety of ways.


Unless you build your own in-memory index, for example using a HashMap, I suppose.


Additionally, database indexes are usually persistent, so they do not need to be regenerated with each run of the application, like a TreeMap would. In a real application, that could be a huge performance advantage.


Well, yes. On the other hand, depending on the circumstances, just using serialization might be a viable alternative.


But the main reason for swapping to a database (or some other file-based approach) would be that the huge data simply would not fit in memory.


Very true!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!