2

My question is: Is there any way to implement database kind of functionality (which In Memory database provides) with java.util.collections (ArrayList/HashMap/3rd party LIB etc...) Let me put in little bit descriptive.

I have Model class Account with different 25 attributes, and i can store the data of lets say 5000 Accounts on ArrayList. Same way I can hold the data on In Memory Database by creating the table t_Account. Now I want to implement some basic operations as mentioned below

  1. Sorting in ascending/descending order based on each attribute. (e.g. Order By operation of SQL)

  2. Filtering out the specific Model Accounts based on different filter criteria of each attribute? (applying WHERE/AND conditions on table in SQL)

  3. Search of specific Account based on specific attribute? ( applying LIKE operations on table in SQL)

In short my main aim is to achieve the In Memory Database related features using Java, which should be as good as In Memory database operations?

Thanks in advance for your valuable time and suggestion...

2 Answers 2

1

Of course you can do part of what a database do in memory as long as you don't

  • want to implement complex querying

  • look for performances on big sets of data : complex indexes (including their maintenance) isn't an easy thing

But 5000 records is a very small set, so you can probably manage them in memory with custom functions and indexes without difficulty. Sorting, filtering, searching are easily implemented on small sets in any language.

You seem to use java ("ArrayList"), look at Collections.sort(). Sorting a small collection is fast as long as your comparison functions are fast.

On the other hand, using a small database like sqlite is easy too. If you're unsure you can start with the java solution and integrate external tools later if a profiling shows you cannot handle your in memory database efficiently enough for your need. If it fits easily in RAM and you don't have to persist the data, try to do it in memory. The first reason to use a database is the persistence. At contrary, if you have to keep the records between start-stops (or crashes) of your program, use a database.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for quick suggestion, i just put 5000 for example it may go upto 20k-25k. below is more details
If it fits easily in RAM and you don't have to persist the data, try to do it in memory. The first reason to use a database is the persistence.
Thanks for quick reply, i just put 5000 for example it may go upto 20k-25k. below is more details 1. To fetch the data i may need to aply cple of filters based on different criteria, so in that case i have to iterate through whole list. 2. I can use sort() method but i may need to implement for each property. 3. Is there any better approach to apply any action on List instead of iterating through each element? like e.g. One attribute of Account class is "type" so when i want subList of all Accounts by passing as "checking/saving" instead of iterating through all elements it should give results
1

Both the HSQLDB and H2 pure Java SQL DBs can operate completely in-memory (and they are quite fast despite not being native).

Connection strings, HSQLDB: jdbc:hsqldb:mem:, H2: jdbc:h2:mem:. You can add an identifier after them to create in-memory databases that support multiple connections in the same VM.

The native embedded SQL DB, SQLite, can also operate completely in-memory and the Xerial SQLite driver uses the connection string jdbc:sqlite::memory:. Note that SQLite does not allow an identifier after :memory: and if you open two connections in the same VM to :memory, you will get two different in-memory databases.

I don't see a real need to use named in-memory connections because you can reuse the same connection object if you need to use it from different threads and all three of the above databases are thread-safe.

The Firebird embedded database does not provide in-memory databases.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.