Download as rtf, pdf, or txt
Download as rtf, pdf, or txt
You are on page 1of 6

Collection:

Array once declared ,its size is fixed,we cannot change the size of an Array.To make an
expandable array we use Collection,which is an Interface.Therefore we cannot create its
object.So to implement a Collection,we use ArrayList.

ArrayList :
· It is a class which implements Collection (Interface) indirectly.So actually ArrayList (class)
implements List (interface),which extends Collection (interface).

· ArrayList can be shrinked and expanded any time u want.

· So wenever the size is fixed,go for Arrays,otherwise go for Collection.

In arrays,we specify the type of data which we want to store.

But in Collection,we dont need to specify the type of the data to be stored.The datatype of
collection is by default "object".

But wat if we want to store only one type of data in our collection?

There comes the concept of Generics.


In java 1.2 we only had the concept of Collection.

In java 1.5, there came the concept of Generics.

But 1.7 and later ,says when we mention datatype on LHS,then y to mention it on RHS :

Suppose if we want to add 8 in between 5 and 6,we cant do that now bcz Collection doesnt
work on the concept of "index".

So to solve this prblm,we make use of "List".It makes use of index to store the elements inside
it.

To create object,ArrayList can be used wid Collection as well as wid List.

· There also is another concept of "Set".Set is an interface again(Cant create its objects).

· Hashmap is a class which implements Set.

· But Set contains only unique values,no duplicate values are allowed.
· What are the Similarities Between TreeSet and HashSet?

Both TreeSet and HashSet are classes belong to the collection hierarchy.Both TreeSet and
HashSet stores only unique elements.Both TreeSet and HashSet can be used to store and
manipulate many elements.Both TreeSet and HashSet do not maintain the inserted order.

· TreeSet v/s HashSet:

In programming, it is required to store data elements dynamically. Programming languages such


as Java supports Collections to achieve this task. There is a number of interfaces and classes in
the collection hierarchy. The TreeSet and HashSet are two class in the Collection hierarchy. Both
implements the Set interface. TreeSet is a class that implements the Set interface and it is used
to store unique elements in ascending order. HashSet is a class that implements the Set
interface and it is used to store unique elements using the Hashing mechanism. The difference
between TreeSet and HashSet is that TreeSet stores the elements in ascending order while the
HashSet does not store the elements in ascending order.

· Map

There is another one called Map.This again is an interface.So a class is needed to implement this
interface.So that class is HashMap or u can use HashTable.

Map has key value pair.Their datatype can be anything.

Generics in Java

The Java Generics programming is introduced in J2SE 5 to deal with type-safe objects. It makes
the code stable by detecting the bugs at compile time.

Before generics, we can store any type of objects in the collection, i.e., non-generic. Now
generics force the java programmer to store a specific type of objects.

Advantage of Java Generics


There are mainly 3 advantages of generics. They are as follows:

1) Type-safety: We can hold only a single type of objects in generics. It doesn?t allow to store
other objects.

Without Generics, we can store any type of objects.

List list = new ArrayList();

list.add(10);

list.add("10");

With Generics, it is required to specify the type of object we need to store.

List<Integer> list = new ArrayList<Integer>();

list.add(10);

list.add("10");// compile-time error

2) Type casting is not required: There is no need to typecast the object.

Before Generics, we need to type cast.

List list = new ArrayList();

list.add("hello");

String s = (String) list.get(0);//typecasting

After Generics, we don't need to typecast the object.

List<String> list = new ArrayList<String>();

list.add("hello");

String s = list.get(0);

3) Compile-Time Checking: It is checked at compile time so problem will not occur at runtime.
The good programming strategy says it is far better to handle the problem at compile time than
runtime.
List<String> list = new ArrayList<String>();

list.add("hello");

list.add(32);//Compile Time Error

Vector:

You might also like