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?
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.
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?
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.