Java Collections: is an object added directly to a collection?

I know that when I add an object to some collections like LinkedList, the item is not added directly to the collection; in fact, a node is added to the collection that provides the linking functionality, and that node has a reference to the Object that I am adding to the collection. Is this true for all Java classes? For example, when I do the following:

List<String> list = new ArrayList<String>();
list.add("Car");

      

Is the String object "Car" added directly to the list, or is only the node added to the list that points to "Car"?

Also, can I consider a proxy design pattern?

+3


source to share


3 answers


ArrayList in Java uses an array to store object references, so it has nothing to do with the proxy pattern. You can see it for yourself, here is a link to the OpenJDK implementation. Line 103 has a corresponding code snippet.

private transient Object[] elementData;

      



You can also compare it to LinkedList , which uses nodes to store links to the next and previous item in List

.

+5


source


Adding items to LinkedList is not a proxy pattern. A proxy template is a creation template. There is no creation in the LinkedList as to what it contains. The fact that Node is wrapping an object is not enough to qualify it as a proxy. The Node wrapper also does not implement the shared interface and object contained in the collection.

A proxy is an object that can stand in place of another object, so it is important to have a common interface. This object in an object can store memory, time, or both, creating a real object on demand. He can also choose to destroy the object when not needed. A proxy server manages the lifetime of the object it wraps, which is a hallmark of the proxy pattern. The Node in the collection does not manage the lifetime of the object it points to because it was provided with the object it wraps.



For example, let's say we have an application that connects to many databases, but doesn't always need to connect to all of them at the same time. We could implement a custom DataSource that will only create the underlying DataSource when a connection is requested. It can also close the DataSource when you are not using an active connection (say when the last connection was closed).

This is similar to how pooling is implemented in most pooling libraries. The underlying DataSource simply provides a connection to the database, but the connection pool is overlaid on top of the DataSource and provides the merge without the client having to do it.

+2


source


You can actually look at the source code ArrayList

, etc.

An ArrayList

uses an internal array Object[]

, so it does not contain additional wrappers, but it stores object references directly. So there is definitely no "proxy" here.

If you add primitive types, they will auto-block as objects. I would not call this an instance of a proxy template.

If you're looking for a proxy pattern, take a look at RMI.

+1


source







All Articles