Collections in Java

Collections 

Collections is the framework. In which the Collection and Map are used for collection.

Collection

Collection is an Interface in java.
Collection is a set of homogeneous kind of data. Means in collection we can collect same kind of data.
For Ex:- Shoes Collection, Book Collection etc.

Methods of Collection Interface

1.       public boolean add(E e)
2.       public boolean addAll(Collection c)
3.       public boolean remove(Object element)
4.       public boolean removeAll(Collection c)
5.       public boolean retainAll(Collection c)
6.       public int size()
7.       public void clear()
8.       public boolean contains(Object element)
9.       public boolean containsAll(Collection c)
10.   public Iterator iterator()
11.   public Object[] toArray()
12.   public boolean isEmpty()
13.   default Spliterator<E> spliterator()
14.   public boolean equals(Object element)

15.   public int hashCode()
Points to Remember:
·         Every Collection class by default Inherit Seriializable and Clonable Interface
·         ArrayList and Vector Implements RandomAccess Interface.
·         RandomAccess Interface also known as Marker Interface i.e. it does not contain any method.

·         java.util package in which all collection object are reside.

The collection Interface comes under java.lang.Iterable Interface. which have a single method
i.e. java.util.Iterator iterator();.
Collection comes under the java.util Package.
The Interfaces of Collections Framework are as follows:
  1. Collection(I)
  2. List(I)
  3. Set(I)
  4. SortedSet(I)
  5. NavigabeSet(I)
  6. Queue(I)

All these 6 interfaces are used to representing the group of individual objects.
We can not directly create the objects of these interfaces that's there are some classes of these interfaces, which are as follows:

List(I)

  • List is an Interface, which implements the collection interface. 
  • List is in ordered collection.
  • List allows duplicate values to be stored.
We can’t directly initiate the object of List Interface for that we need a class which implements this Interface. The following classes are as follows:
a)       ArrayList
  • List l= new ArrayList();

b)      LinkedList
  • List l= new LinkedList();

c)       Vector
  • List l= new Vector();

d)      Stack

  • List l= new Stack();

Classes comes under List are as follows:
  1. ArrayList
  2. LinkedList
  3. Vector
  4. Stack

ArrayList:

·         Resizable array or grow able array.
·         Duplicate value Allowed.
·         Insertion order is preserved.
·         Heterogeneous value allowed.
·         Null Insertion is possible.

Constructor

·         ArrayList al  = new ArrayList();
If we want to insert element more than the size of the array then it will create a new ArrayList then the new Capacity:
                                                                (currentCapacity * 3/2) + 1
·         ArrayList al = new ArrayList(int capaciy);
·         ArrayList al = new ArrayList(Collection c)

Method:

·         1. add(element)
·         2. add(index, element)
·         3. remove(element)
       Advantages
·                           Retrieval is easy
       Disadvantages:
          Insertion and Deletion is difficult.

How to get Synchronized version of Array?

  • List l1 = new ArrayList();
  • List l2 = Collections.synchronizedList(l1);


LinkedList:

  • Uses Doubly LinkedList to sore data.
  • Insertion order maintained.
  • Duplication allowed.
  • Heterogeneous elements are allowed.
  • Null is allowed.
  • Does not Implement RandomAccess Interface.

Constructor

LinkedList l = new LinkedList();
LinkedList ll = new LinkedList(Collectio c);

Method

  1. void addFirst(Object o)
  2. void addLast(Object o)
  3. Object getFirst()
  4. Object getLast()
  5. Object removeFirst()
  6. Object removeLast()
  7. T[] toArray(T[] a)
  8. Object[] toArray()
  9. E pop()

Advantages of LinkedList

·         Insertion and deletion is easy here,

Disadvantages of Linked List

·         Retrival is difficult it takes time.

J     

Vector

·         100% Resizable
·         Duplicate value insertion allowed.
·         Insertion order maintained.
·         Null value allowed.
·         Heterogeneous value allowed.
·         It implements the Serializable and Cloneable Interface.
·         RandomAccess Interface implementation allowed.
·         It is ThreadSafe i.e. Synchronized.

Constructor

·         Vector v = new Vector ();
·         Vector v = new Vector (int initialCapacity);
·         Vector v = new Vector (int capacity, int incrementVal);
·         Vector v = new Vector (Collection c);

Method

·         allElement()
·         removeAllElement()
·         removeElementAt(int index)
·         Object elementAt(int index)
·         Object firstElement()
·         Object lastElement()
·         int size ()
·         int capacity ()
·         Enumeration elements()

Advantages

·         Size of the vector can be changed
·         Multiple objects can be stored
·         Elements can be deleted from a vector

Disadvantages

·         A vector is an object, memory consumption is more.

Stack

Satck is based on LIFO(last in first out).

Constructor

·         Stack s = new Stack();

Method

·         boolean emply()
·         Object peek()
·         Object pop()
·         Object push(element)
·         int search(element)


Set(I)

  • Set is an interface, which implements the Collection Interface.
  • Set is an un-ordered Collection .
  • Set doesn't allow he duplicate values.
class comes under Set are as follows:

  1. HashSet  *LinkedHashSet

SortedSet(I)

SortedSet is an Interface, which implements the set Interface.
class:
 TreeSet (c)



ArrayList: 

No comments:

Post a Comment

Short note of uml basic building blocks

(SHORT NOTE) Basic building blocks of UML UML(Unified Modeling Language) There are three kinds of building blocks of UML 1...