Vijay has posted 2 posts at DZone. You can read more from them at their website. View Full User Profile

Collection Framework: Missing Concepts

09.27.2012
| 3578 views |
  • submit to reddit

Introduction 


My first encounter with the collection framework was on my second day of Java programming. The collection framework has many implementations, and each of these is solving some very specific need of the data structure. There was a lot of information about every use case and its optimal collection implementations, but now, after four years, I still have to go through the same process when there's a decision to make about collection use.

Deciding which collection to use is often one of the most important decisions to make when designing a program. By definition, a collection provides means for storing and manipulating groups of data as a single unit. This single unit is called a collection -- it's sometimes also called a container.


The Container 


In object oriented programming, container is defined as a class capable of storing other objects. These classes usually implement some kind of data structure such as map, set, stacks, etc. The size of the collection of objects is adjusted automatically in a container class. A container can be divided into two groups:

  1. Value based containers
  2. Reference based containers


Value-Based Containers 

Value-based containers store copies of objects. If we access an object, an object returns a copy of it. If external object is changed after it has been inserted in container, it will not affect the content of the container at all.

Reference-Based Containers 

Reference-based containers store pointers or references to the object. If we access an object, an object returns reference to it. If external object is changed after it has been inserted in container, it will also affect the content of the container.


The Study 


Further to this, the container can be studied under three points of views.

Access

It means accessing the container elements. In case of arrays accessing is done using array index. For stacks, access of elements is done using LIFO (Last In First Out) and in queues it is done using FIFO (First In First Out).

Storage 

It includes storing of items of containers. Some containers are finite containers and some are infinite containers.

Traversal 

It includes how the item can be traversed.

The Summary 

Now, look for the collection with above concepts imprinted in brain. Selection of the correct container data structures in a program can do wonders.

  1. It can make the code a hundred times faster.
  2. Help in to reduce memory requirements 
  3. To make the source code cleaner (in turn making it easier to write, debug, and expand). 

Typically, the most commonly used data structures are ones which are implementations of abstract data types (containers). Whereas, an abstract data type (ADT) specifies what operations can be performed on a data structure. A data structure is the concrete implementation of ADT, specifying how much memory is required and, crucially, how fast the execution of each operation will be.
Published at DZone with permission of its author, Vijay Shanker.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

Comments

Sha Jiang replied on Thu, 2012/09/27 - 9:44pm

Hi,

I may not understand your mind clearly. I think the containers just store the references, not the objects themselves, so it's no meaning about inner or external object.In fac, they are the same object just with the different references.

You can clone the object if you wish, and you can get an unmodified collection via java.util.Collections. But, I think both of the approches (including clone) cannot ensure the so-call inner objects cannot be modified.

Thanks!

Sha Jiang

Vijay Shanker replied on Fri, 2012/09/28 - 8:40am in response to: Sha Jiang

Hello Sha Jiang,

Containers can be divided into two groups; and the group that is value based contianer is confirms to the that. But in java I donot find any co-related reference of container. 

 Refs : http://en.wikipedia.org/wiki/Container_(abstract_data_type)#Value_based_containers 

Sha Jiang replied on Sat, 2012/09/29 - 9:25am in response to: Vijay Shanker

On the contrary, I think the containers in Java are reference based. I had said that the containers just store the references not the objects themselves.

class Foo {

  private String name;

  public Foo(String name) {
    this.name = name;
  }

  public void setName(String name) {
    this.name = name;
  }

  public void getName() {
    return name;
  }
}

// store a Foo instance into a List container
List<String> strList = new ArrayList<String>();
strList.add(new Foo("A"));

// retrieve the Foo instance, and change its name
Foo fooA = strList.get(0);
fooA.setName("C");

// How do you think about the output?
System.out.println(fooA.getName());

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.