• Post Reply Bookmark Topic Watch Topic
  • New Topic

Which collection class to use  RSS feed

 
Ranch Hand
Posts: 146
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I have a requirement to fetch all the records in the DB, in an order.
Once it is stored in the collection object, It should be fast to access those records..
So, which collection class i need to use ? which collection class is fastest ?

please help
 
Bartender
Posts: 4568
9
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Depends how you want to access them. By position? Lookup based on some attribute (and if so, is it a unique value)? Iteration through the collection? Different collections are good at different things.
 
Ranch Hand
Posts: 375
1
Java Python Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anu satya wrote:Hi,
I have a requirement to fetch all the records in the DB, in an order.
Once it is stored in the collection object, It should be fast to access those records..
So, which collection class i need to use ? which collection class is fastest ?

please help


ArrayList --> When you want indexed access (It is the fastest when it comes to accessing through index)
 
Marshal
Posts: 56608
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
R. Jain wrote: . . . ArrayList --> When you want indexed access (It is the fastest when it comes to accessing through index)
You don’t know that she wants indexed access.

I recommend you read through the Java Tutorials and see what is available. You might want a List, in which case ArrayList might be the fastest to use, but for other purposes as Matthew has already suggested, a Map might be better. I suspect a Set will not be very useful in this instance.
 
Anu satya
Ranch Hand
Posts: 146
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the reply.
I learn that ArrayList is fast for indexed access.
what if i need to iterate through complete collection (search) ? which one is fast?
for example, my collection object is having all the employee names in it. I need to search for a particular(unique) name say "JHON", which one is more efficient and fast?
Which collection to use to print all the names where first name is JHON? there might be multiple employees with first name as JHON.

Can any one please help?
 
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anu satya wrote:Once it is stored in the collection object, It should be fast to access those records...

One thing that may also be worth mentioning is that "fast" is in the eye of the beholder.
If you're pulling 100 rows from a table, the chances are that the type of collection you use won't actually matter much.
If you're pulling a million, it'll make a BIG difference; but you may then want to ask a different question: Why am I pulling a million rows out of the database?

Also: there's the business of how you need to access the data. Database tables allow you to define secondary indexes so that the same data can be retrieved quickly in many different orders. In Java, there is no such animal, but you can create more than one collection based on the same data without too much overhead.

One last point: Databases were designed specifically to store, retrieve and query large volumes of data; Java wasn't. So be careful that you're not using Java to do something that would be better done by the database.

Winston
 
Campbell Ritchie
Marshal
Posts: 56608
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anu satya wrote: . . . Which collection to use to print all the names where first name is JHON? there might be multiple employees with first name as JHON.

Can any one please help?
What Mr Gutkowski said.

That sort of thing is probably best done inside the database.
 
Bartender
Posts: 2407
36
Linux Oracle Postgres Database Python Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As Winston and Campbell said, you should do as much filtering as possible inside the database, before you fetch any data into your Java tier. The database is designed to perform these searches quickly and efficiently, and it has a specialised search language - SQL - and indexing functionality designed specifically to do this. If you fetch the whole table into a Java collection, you are moving more data than necessary across the network, storing more data than necessary in memory, and searching more data than necessary in Java to find the rows you really want. It's unlikely that you can write your program to do this more efficiently than an RDBMS query engine which has 30 years of development by DB experts behind it.

Use the right tool for the job instead.

Anu satya wrote:I need to search for a particular(unique) name say "JHON", which one is more efficient and fast?
Which collection to use to print all the names where first name is JHON? there might be multiple employees with first name as JHON.

Is the name unique or not?
If the name is unique, then you can find the record with a SQL query that should return a single row. But if there are multiple records with the same first name (much more likely), then your SQL query will return several records. If you have other criteria for selecting a single row from the results, include these in your SQL query so you only fetch the record(s) you want.
 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is always recommended to perform the data intensive operations using the native database mechanisms.

Things like searching and sorting etc should be performed at the database end so as to lower the foorprint of the object heap of the JVM.

But for linear iterations, any implementation of the List interface is recommended.

However for readonly iterations, you can have a look at the apache FastArrayList class.

See the link here
 
Saloon Keeper
Posts: 7993
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Personally I would stay away from that class. It doesn't look like it has seen a lot of improvement since 2002, including generic type parameters.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!