I have a web service that holds information for users. Each user has a list of items, possibly thousands. I want to add an item to the list without loading the entire list. Is there a list implementation that allows you to add elements to the list without bringing the entire list into memory?
A Doubly Linked List. By definition it's not necessary to traverse the list to add something to the end since it contains a pointer to the end.
the list is on a remote database. –
Lumpy
So the list is in a database, so it's not really a Java List? It's just a bunch of database rows? In that case why not just do an insert into the database to add another row?
INSERT INTO list VALUES 1, 2, 3;
Lazy proxies. You can use a JDK dynamic proxy (java.lang.reflect.Proxy) where you store only the information needed for retrieving the items from the database, not the items themselves. Only when calling the get(..), size(), contains(..) methods - fetch the data.
However I have a feeling that you are doing things the wrong way. Give more details about your implementation.
None that I know.
With middlewares such as Terracotta, some collections (such as maps) can be loaded on-demand and partially, but this doesn't exist as-is in the standart JDK.
Related
Our db have around 400k strings (usernames) at the moment that will need to be checked do they exist on request. Due to large amount of requests needed to check in single second (and because data in mysql will be updated only periodically) it will probably be better to use java instead of mysql query. So my thought is that ill be better to load data from mysql to java and get result if some specific usernames exist in it instead of overheating mysql.
And so my question is, to which one should i load records to, to array ArrayList list collection map HashMap or something third if you can think of, for best speed for only to check if (per request) one username is on that list or not?
You can find details on how each is implemented in the java docs. I believe ArrayList will do a linear search. So HashMap is most likely better.
I'm trying to develop my Android app with Realm database.
Today I got below problem:
I added a list of records to table and then try to deleted one of them.
after deleting the order of the rest was changed (it's different with the order before deleting).
please see the images below to see detail.
Before deleting
After delete the 3rd item
And the question is: That's is an function or an bug? And how Can I keep the order of record?
I know that I can easy to get the correct order as I want with add a new field as createTime or something like that but I want to find an very simple solution as config something for Realm.
Items in a Realm are not sorted by default, so you should think of any query result as an unordered set unless you explicitly sorted it.
Generally the items will come out in the order you inserted them in, but it is not a guarantee. The underlying reason technical reason is that we compact the data on the disk, so if you delete items in the middle of a list, the last item will be moved to its place.
So the answer is: It is working as intended, and you should use a sorting method if you want your results to be sorted.
So I've got this school project, and I would really like to approach it with the best practices.
I need to make a list of customers for an insurance company. Each of these shall have a unique customer number, generated in ascending order.
Every customer can have zero to many insurances, also stored in seperate lists for each customer. Adding of insurances will happen more often than adding of customers.
Every customer can also have any numbers of claims. Every claim also has a unique id number.
If a customer cancels all insurances. All data on this customer will remain as history.
All data need to be stored via one of the file classes in the Java Standard Library. Databases are not allowed.
Actions such as showing of statistics will also be available.
Users of the program will be employees, with rights to edit every data field.
Questions:
What Collection class would be the most effective one to use? LinkedList, ArrayList, Hashmap or any other?
What file class would be the best one for saving the lists? ObjectOutputStream?
What is the best method of generating new unique ids for both customers and claims? As private fields in the customer list class? Information on the next unique id has to be restored every time the program exits and restarts.
Edit:
Not looking for help with any code. Just advice on the most common classes to use in a scenario like this.
What Collection class would be the most effective one to use?
LinkedList, ArrayList, Hashmap or any other?
Ans - LinkedList and ArrayList are types of List. HashMap is a type of Map.
What implementation of List you want to use depends on your requirement. If you are going to perform insertions and removals of elements at different points of a List frequently, then LinkedList makes more sense. It is more efficient at, say for example, removing an element in the middle of the List. Otherwise prefer to use ArrayList.
What is the best method of generating new unique ids for both
customers and claims? As private fields in the customer list class?
Information on the next unique id has to be restored every time the
program exits and restarts.
You may want to use a Singleton to generate IDs, and also persist them to a file.
I am learning hibernate with JPA.
With one to many relationship, I got a issue of lazy initialization. Then I changed fetch type as EAGER, it was showing "can not fetch multiple bags".
Then I changed my List into Set. I wonder its working fine.
But I want to know the reason, why set is better than list.
Kindly explain me to understand the functionality of set and List.
Set is a collection that cannot contain duplicate elements.
List is an ordered collection and can contain duplicate elements. You can access any element from it’s index. List is more like array with dynamic length.
Lookup from a HashSet is constant time O(1), when lookup from ArrayList will take time O(n). So HashSet performance will become more reliable than ArrayList. For more detail you can read it here for list and here for set.
While making my program i have come across this requirement that i have to assign unique id's to some Objects that i create. Now i am creating the objects dynamically on GUI, and initially i used simple counter to assign int value to the created node, and it worked just fine.
However the problem that this approach creates is that if while creating the GUI, if some node has to be deleted, this id is also removed and is never used again. With the next new node, everytime i have to use the latest counter value and this creates lot of missing int values if nodes are deleted during the process.
I wanted to reuse those missing id's upon creating of new nodes, for this i am confused which approach i should addopt.
MY Ideas:
Using a ArrayList that contains the available values, plus if a node
is deleted, it's id is added to this list, i sort this list and use
the minimum value for new node. Fine but, when i use this value, if
i remove it from List, the index is not deleted and this causes
problem.
HashMap, similarly like above i add available id's and remove not used, but not sure how to sort this hashMap???
Can you suggest how i should go about it? May be i need some kind of stack where i can push values, sort it and use the minimum value, and if that i used, it is removed from this stack, please give some ideas about this how to accomplish this task???
Keep a list of the deleted IDs, and when you create a new node, check that list for an ID to re-use (doesn't matter which you take); if the list is empty (as it will be initially), get a new ID "the old way". Even more clever: make the list an object that will generate a new ID if there aren't any deleted ones in it, so the caller doesn't have to worry about HOW the ID was arrived at.
You could use a TreeSet (which automatically sorts all entries added from least to greatest) to store the deleted id's (myTreeSet.add(old_id)). That way, when you go to create a new instance, you would check to see if there are any entries in the TreeSet first. To grab the lowest value, you would use myTreeSet.first() (which should be an O(1) operation). If the TreeSet is empty, which means all known id's are currently in use, then you would go ahead and use the next available id as normal.
How about a TreeSet to store the used IDs? You could then use higher(0) to find the lowest free ID. If it returns null, then you know that you have no used IDs.
The first solution works fine only if you have few nodes! Imagine an application with thousands nodes! What about memory consumption?
The Hashmap solution is better to you aims and need less controls.