Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
I have to store 500-1000 values in somewhere, but i cant decide which one is more efficient(ArrayList vs Database).Can you explain pros and cons of arraylist and database?
thanks...
In memory data structures are always faster than DB.
But tradeoff is between size of data structure and available memory. For your need, arraylist will be faster. But data will be gone once application is stopped or killed.
Database is persistent data store. If your need is to store temp data then arraylist is suitable and if you need to store it permanently then you have 2 options:
Database (standard way, API availalbe, standard practice)
Filesystem (keep your data in data structure till application stops and then write it to a file in encrypted form if security is required.)
If you need, explore about in memory databases for android(sqlite). This is best suitable for you.
the use of array list and the data base is different.
if u doesn't want to store your record permanently then you can use "Array List", but if u want to store your record permanently then u need to use data base.
because when you store the record in Array list those record stored into RAM. records don't store into the secondary storage. but when you store the record in to the data base then records stores into the database.
if your concern about the speed of accessing the data from Array List or data base then arrayList is faster then the database
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I have an app (field data collection) where I used the sign in option and authentication with firebase.
All the other database the default option is to store data as a local SQLite database, however, I want to add the option if the user wants to sign in and transfer the local database to an online database so it can be accessed on multiple devices.
Can I have together firebase and sqlite?
I mean can the user store the data offline and when goes online with a sync button can have the data online in the firebase cloud?
My sqlite database stores data from drop down columns, google maps longtitude and latitude data, voice recordings and images.
I would appreciate your help, and hope you understand my questions!
Yours sincerely,
Tzirineto
Yes, this can be possible. The data in SQLite is stored in the form of tables i.e rows and columns i.e. its a relational database. But the data in the firebase id store in the form of documents i.e. key-value pair. Your first step is to create objects in Java. Then it will be easy for you to dump same objects in both SQLite and in firebase. Also you have to maintain a proper relations among the data using uid in firebase. When the user clicks sync button you have to add the objects in the firebase. In order to do that first you have to retrieve the complete data from SQLite and store it into java objects then send them to firebase. You have to keep a check among relationship that exists among the data. Hope it helps
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I need to store many Long-Long pairs in disk, not in memory, for example:
(123, 2)
(1253, 3)
(12345, 6)
...
So later I can easily do:
long value = get(123); // => 2
Is there a simple approach to do that? I am hoping it does not need to be as complicated as a hashtable in disk.
To make it clear, the pairs will NOT be stored in memory, they will be written straight to a file or set of files in disk.
There are multiple key/value stores available. A quite comprehensive list can be found here.
One good alternative is to use MapDB which is a fast and easy-to-use NoSQL database which supports disk storage and transactions.
To use it, simply follow the instructions in the cheat sheet:
File file = new File(“dbFileName”);
DB db = DBMaker.newFileDB(file).make();
//use map
Map map = db.getHashMap(“mapName”);
map.put(“aa”,”bb”);
//commit and close database
db.commit();
db.close();
Plus, it obviously works for Long values as well!
You can still use a hashtable (best way to store key/value pairs), you just have to serialize it.
You can access your serialized files and read those in. For example, after reserialization (assuming hashmap style): Long x = Long.parseLong(y.get("123"));
Unless this is a CS homework, I would simply use a Redis persistent instance.
http://www.redis.io
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
In my web application i have a employee table with employee id,name,designation salary... as attributes which may contain thousands of records in it.. I want to search employee name, so for searching employeename which one will work faster hitting DB every time or create list of employee names once in java bean and iterating it for searching every time... Which one is better..??
By far, even if you have millions of records, it is better to hit the database per request. To enhance this, you can add a key/index on your name field in your employee table and the requests will be faster.
In case the data in your employee table doesn't vary that much, you have another option which is using a cache for your employee table. With this, your access to the data will be even faster since it will look up the employee at cache (usually RAM), but this comes with a more complex design and adding policies for your cache data retrieval and setting periods to update the cache data.
This is depends in few things..
Hitting the DB is IO action and in case you have specific screen/process that does a lot in specific flow of course it will be better to load list from DB once and use it several times, And this is in case that you can be sure that employees list won't be change in DB by other process/Or it can change and this is not critical for you..
If the screen/process make only few hits to get employees it should be hitting DB.
Remember that Hitting DB a lot of time can also load the DB and cause him to be slow.. He can't handle with infinite number of request.
Hope that helps
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I'm trying to count the number of times multiple id's appears in a database table, then using them numbers to put into a JFreeChart.
Im currently unaware how to do this and cannot find code online to do this.
Well I'm certainly no expert but I recently did something similar in an application. You can hardcode the required SQL query strings into your application code and thus, retrieve the required data from your database. You will need a database connector for this. The connector you need depends on what language you are writing code in and what database you are using.
You will receive resultsets where the data from your database is returned as Strings. This includes data that was stored in your database in numeric format so you may need to cast the strings to another format if thats what you require. You feed the result set into a collection structure such as 'ArrayList' (if you are using Java for example). You said you are trying to count id's so you could use searching methods for whatever collection you use to tell you whether the strings in the collection are duplicates, you may need to use a a set (a collection which cant have duplicates) for comparison purposes. There's not that much detail in your question but this should help, just count the duplicates in your collection and keep track of the numbers.
At the end if this you will have a collection of numerical values so you simply feed this to a class which imports the required JFreeChart modules and uses the data to create a chart.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 years ago.
Improve this question
simple question: any ideas how this should be properly done?. I have 3 txt files with lots of information, I created a class that will be in charge of reading the data from the txt files and returning the data as a list of DTO components (Yes, the information can be bundle as such logic unit), depending on the txt file, after that the client will use a DAO and will use such a list and insert the data to a local database (sqlite). My concern is that having such a List could be memory demanding, should I avoid using such list and somehow insert this data using the dao object directly without bundling the data into a dto and finally such list?
You are asking a good question and partially answering it yourself.
Yes, sure if you really have a lot of information you should not read all information from file and then store it in DB. You should read information chunk-by chunk or even if it is possible (from application point of view) line-by-line and store each line is DB.
In this case you will need memory for one line only at any time.
You can design the application as following.
File parser that returns Iterable<Row>
DB writer that accepts Iterable<Row> and stores rows in DB,
Manager that calls both.
In this case the logic responsible on reading and writing file will be encapsulated into certain modules and no extra memory consumption will be required.
do not return list, but an iterator like in this example: Iterating over the content of a text file line by line - is there a best practice? (vs. PMD's AssignmentInOperand)
you have to modify this iterator to return your DTO instead of String:
for(MyDTO line : new BufferedReaderIterator(br)){
// do some work
}
Now you will iterate over file line by line, but you will return DTOs instead of returning lines. Such solution has small memory impact.