Collections
Collections
The main advantage of Array is we can represent multiple values by using Single variable. So that
readability of the code will be improved.
Limitations of Arrays:
Array are fixed in size . i.e. once we create an Array there is no chance of increasing or
decreasing the size based on our requirement. Due to this , to use Arrays concept compulsory we
should know the size in advance. Which may not be possible always
Array can hold homogenous datatype elements.
We can solve this problem by using object type array
Object [] a= new Student ();
a[1]=new Customer ();
Collections :
• collections are grow able in nature I .e., Based on our requirement we can
increase or decrease the size.
• being a programmer ,we are responsible to use those methods, we are not
responsible to implement those methods
Collections :
s1 s2 Individual objects
s3 s4
Single entity
s5
If we want to represent group of individual objects as a single entity then we should go for
collection.
Collection Framework :
• it contains several classes and interfaces which can be used to represent group of individual
objects as a single entity.
• Collection interface defines the most common methods which are defined for any collection
object.
Difference between Collection and Collections :
List Interface :
list is the child interface of collection.
We can preserve insertion order via index or with index and we can differentiate
duplicate objects by using index. Hence, index will play an important role in List.
List Interface also has its own specific methods. They are:
• void add ( int index, Object O);
• boolean addAll( int index, Collection C);
• Object get( int index );
• Object remove (int index );
• Object set(int index, Object new)
To replace the element present at specific index with provided objects and returns old object
int indexOf(Object O);
ListIterator listIterator();
Array List (i c)
The underlying Data structure is resizable array or growable array.
Duplicates are allowed
Insertion order is preserved
Heterogenous object are allowed (Except TreeSet & TreeMap) everywhere heterogeneous
object are allowed.
Null insertion is allowed
Constructor:
Creates an empty Array list objects with default initial capacity 10, once Array list reaches its
maximum capacity then a new array list object will be created with given default capacity 10.
ArrayList l=new ArrayList(int initial Capacity);
create an empty ArrayList object with specified initial capacity
ArrayList l=new ArrayList(Collection c);
Linked List
• The underlying Data Structure is double link list //circular list
• Insertion order is persevered.
• Duplicate objects allowed
• Heterogenous object allowed
• Null insertion is possible
• Linkedlist implements Serializable and cloneable interfaces But not random access.
• Linkedlist is best Choice if our frequent operation is insertion or deletion In the middle.
• Linked list is the worst choice if our frequent operation is retriable.
LinkedList Constructor, Methods
1> LinkedList l=new Linkedlist();
create an empty Linkedlist object
2> LinkedList l=new Linkedlist(collection c);
create an equivalent LinkedList object for the given collection
Methods
1> Void addfirst (object o); 2> void addlast (object o);
HashSet LinkedHashSet