TruePS
TruePS

Reputation: 513

What is by default capacity of list?How many elements can a list store?

My first question is I want to select 100000 elements from database,can list store that many elements?
My second question is I want to fetch all the elements from database in minimum time?Is list is the best way to store elements or is there any other way which can improve performance?

Upvotes: 0

Views: 5025

Answers (7)

Stephen C
Stephen C

Reputation: 719149

I want to select 100000 elements from database,can list store that many elements?

Yes. There is an upper limit on the size of an ArrayList (2^31) ... but you are a long way off that. And some other List implementations don't have that limit.

I want to fetch all the elements from database in minimum time? Is list is the best way to store elements or is there any other way which can improve performance?

Most of the CPU time will be spent performing the query and reading from the resultset rather than appending to the list.

The performance of the collection will depend on the element type (object or primitive), and on whether or not you know how many elements there will be. A bare array will give you the best performance if you know the element count beforehand, and an ArrayList if you don't know1. For the case of a primitive types, consider using the "trove" list type instead to avoid the overhead of using primitive type wrappers.

1 - That is ... unless you are prepared to implement an ArrayList-like expansion algorithm for your array based collection.

Upvotes: 0

meriton
meriton

Reputation: 70574

can list store that many elements?

Many implementations of java.util.List do not restrict the number of elements, i.e. the number of elements is only limited by available heap memory.

The most commonly used List implementation, ArrayList, is limited to about 2 billion elements (Integer.MAX_VALUE), because that is the maximum length of a Java array.

Other List implementations, such as the Lists returned by Arrays.asList(), Collections.emptyList(), or Collections.singletonList(), have a fixed size, and can not be added to.

Is list is the best way to store elements or is there any other way which can improve performance?

If all you need is to store the elements for later iteration, an ArrayList is probably the best choice, but compared to the cost of communicating with a database, the overhead of any collection implementation will be insignificant, as the database will generally have to perform disk I/O, which is far slower than writing the data to main memory, and writing the actual data (the objects in the list) will take longer than writing the Collection itself.

Upvotes: 1

Erwin Bolwidt
Erwin Bolwidt

Reputation: 31279

The maximum size of a List is limited by the maximum value of a Java integer, because integers are used to index the list and to return the size of the list in the method int size();. The maximum value of an int in Java is Integer.MAX_VALUE which is 2147483647.

A particular implementation of List could have a lower limit, but for java.util.ArrayList, that is the limit.

Of course you could run out of memory long before that, that really depends on the memory of your computer and whether you are using a 64-bit version of the JVM or the 32-bit version.

For your second question: the time it takes to transfer data from the database is almost always far higher than the time taken to store the data in the memory of the computer, so if you only worry about the time it takes to store the data in the list, then you should not worry. If you however are thinking about the time it takes to retrieve the data, then it really depends on how you are retrieving the data from the collection (using a particular key for example).

In many cases, an implementation of java.util.Map such as java.util.HashMap will have better performance when you are retrieving data by a particular key.

Upvotes: 1

anirudh
anirudh

Reputation: 4176

  • List can store more that 100000 elements. The list capacity is only bound by the JVM memory capacity or Integer.MAX_VALUE whichever is less.

  • However, If you use know the number of elements that will be retrieved, then, using a simple array gives far better performance.

Upvotes: 1

RKC
RKC

Reputation: 1874

you can store Integer.MAX_VALUE elements in List I suspect since the value of index can not accept more than this.

Upvotes: 1

AndreaTaroni86
AndreaTaroni86

Reputation: 559

i normally use list over your quota, and lists is a good way. If you use string is really great but what about raw type?

Upvotes: 1

AJ.
AJ.

Reputation: 4534

1) Yes, list can store 100000+ elements.The maximum capacity of an List is limited only by the amount of memory the JVM has available.

2) For performance issues, it depends on the type of data to be stored. Normaly HashMaps are used for databases.

Upvotes: 1

Related Questions