tags:

views:

90

answers:

5

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: 

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.

Olivier Croisier
+1  A: 

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.

Amber Shah
After reading the other answer, I think maybe I was being too abstract in my answer. On the other hand, maybe that's what you wanted...
Amber Shah
He asked about the memory footprint, not about the speed. Any and all in-memory list are, well.. in-memory..
arcticpenguin
Yes, all in memory lists are ... in memory. You are right.What I was saying was, if you have a not-in-memory list (in a file, in a DB, don't care, etc) then the way to add to the end would be to have a "pointer" to the end. When I said it's not necessary to traverse the list, I didn't mean you'd save time, I meant you don't need those items in memory.
Amber Shah
+1  A: 

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?

mbaird
+1  A: 

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.

Bozho
+1  A: 
INSERT INTO list VALUES 1, 2, 3;
Mnementh