Collection in java


Collection in Java
Collections is a framework that provides an architecture to store and manipulate the group of objects. It provides readymade architecture and represents interfaces and classes.
Java.util package contains all the classes and interfaces.


Collection Interface
1.      add()-  add() is used to insert an element in the collection.
Syn-   public boolean add(Object element)
2.      addAll() –  addAll()is used to insert the specified collection elements in the invoking collection.
Syn-     public boolean addAll(collection c)
3.      remove()-   remove()is used to delete an element from this collection.



Syn-     public boolean remove(Object element)
4.      removeAll()-  is used to delete all the elements of specified collection from the invoking collection.
Syn-    public boolean removeAll(Collection c)
5.      retainAll()-   is used to delete all the elements of invoking collection except the specified collection.
Syn-  public boolean retainAll(Collection c)
6.      size()-   is used to get the number of elements in collection.
Syn- public int size()
7.      clear()-  clears the collection.
Syn-   public void clear()
8.      contains()-   return true if element exists otherwise false
Syn-   public boolean contains(object element)
9.      containAll()-   returns true if entire given collection exists otherwise false.
Syn-   public boolean containsAll(Collection c)
10.  iterator()-   returns an iterator.
Syn-   public Iterator iterator()
11.    toArray()-  Converts collection to array. It returns the array of object.
Syn-  public Object[] toArray()
12.    isEmpty()-  checks whether collection is empty or not.
Syn-  public boolean isEmpty()
13.    equals()-  matches two collections.
Syn-   public boolean equals(Object element)
14.    hashCode()-  returns hash number for collection
Syn-  public int hashCode()
Iterator Interface
Iterator interface provides the facility of iterating the elements in forward direction only.
There are only three methods in the Iterator interface. They are:
  1. hasNext()-  it returns true if iterator has more elements.
Syn- public boolean hasNext() 
  1. next()-  it returns the element and moves the cursor pointer to the next element.
Syn- public object next() 
  1. remove()-   it removes the last elements returned by the iterator. It is rarely used.
Syn-  public void remove()
List Interface
  1. public void add(int index,Object element);
  2. public boolean addAll(int index,Collection c);
  3. public object get(int Index position);
  4. public object set(int index,Object element);
  5. public object remove(int index);
  6. public ListIterator listIterator();
  7. public ListIterator listIterator(int i);


ListeIterator Interface      
  1. public boolean hasNext();
  2. public Object next();
  3. public boolean hasPrevious();
  4. public Object previous();
ArrayList Class : ArrayList supports dynamic arrays that can grow as needed.
ArrayList class has following Constructors
ArrayList( )
ArrayList(Collection c)
ArrayList(int capacity)

Methods
add() : adds element at specified index.
Syn : void add(int index, Object element)
addAll() : adds given collection at specified index.
Syn. boolean addAll(int index, Collection c)

clone() : returns shallow copy of ArrayList.
Syn: Object clone()

ensureCapacity() : Increases the capacity of this ArrayList instance.
Syn: void ensureCapacity(int minCapacity)

get() : returns element of the given index.
Syn: Object get(int index)

indexOf() :  returns index of first occurence of the given element if exists otherwise false.
Syn : int indexOf(Object o)

lastIndexOf() :  returns index of last occurence of the given element if exists otherwise false.
Syn: int lastIndexOf(Object o)

remove() : removes and returns the element at the specified.
Syn: Object remove(int index)

removeRange() : removes elements between range.
Syn: protected void removeRange(int fromIndex, int toIndex)

set() : replaces the object at specified index.
Syn : Object set(int index, Object element)

size() : returns size of the list.
Syn: int size();

trimToSize() : Trims the capacity of this ArrayList instance to be the list's current size.
Syn: void trimToSize()


LinkedList Class
The LinkedList class extends AbstractSequentialList and implements the List interface and deque interface. It provides a doubly linked-list data structure.
Constructors
LinkedList( )
LnkedList(Collection c)

Methods
void add(int index, Object element)
boolean addAll(int index, Collection c)
void addFirst(Object o)

void addLast(Object o)
void clear()

Object clone()

boolean contains(Object o)

Object get(int index)

Object getFirst()

Object getLast()

int indexOf(Object o)

int lastIndexOf(Object o)

ListIterator listIterator(int index)

Object remove(int index)

Object removeFirst()
Object removeLast()

Object set(int index, Object element)
int size()

Object[] toArray()

Vector Class
Vector implements a dynamic array. It is similar to ArrayList, but with two differences:
·         Vector is synchronized.
·         Vector contains many legacy methods that are not part of the collections framework.
Constructors
Vector() Creates empty vector.
Vector(int capacity) Creates vector with initial capacity.
Vector(int capacity, int increment) Creates vector with initial capacity. The increment specifies the number of elements to allocate each time that a vector is resized upward.
Vector(Collection c) Creates vector with collection c.
Methods
void add(int index, Object element)
Inserts the specified element at the specified position in this Vector.
boolean add(Object o)
Appends the specified element to the end of this Vector.
boolean addAll(Collection c)
Appends all of the elements in the specified Collection to the end of this Vector, in the order that they are returned by the specified Collection's Iterator.
boolean addAll(int index, Collection c)
Inserts all of the elements in in the specified Collection into this Vector at the specified position.


void addElement(Object obj)
Adds the specified component to the end of this vector, increasing its size by one.
int capacity()
Returns the current capacity of this vector.
void clear()
Removes all of the elements from this Vector.
Object clone()
Returns a clone of this vector.
boolean contains(Object elem)
Tests if the specified object is a component in this vector.
boolean containsAll(Collection c)
Returns true if this Vector contains all of the elements in the specified Collection.
void copyInto(Object[] anArray)
Copies the components of this vector into the specified array.
Object elementAt(int index)
Returns the component at the specified index.
Enumeration elements()
Returns an enumeration of the components of this vector.
void ensureCapacity(int minCapacity)
Increases the capacity of this vector, if necessary, to ensure that it can hold at least the number of components specified by the minimum capacity argument.
boolean equals(Object o)
Compares the specified Object with this Vector for equality.
Object firstElement()
Returns the first component (the item at index 0) of this vector.
Object get(int index)
Returns the element at the specified position in this Vector.
int hashCode()
Returns the hash code value for this Vector.
int indexOf(Object elem)
Searches for the first occurence of the given argument, testing for equality using the equals method.
int indexOf(Object elem, int index)
Searches for the first occurence of the given argument, beginning the search at index, and testing for equality using the equals method.
void insertElementAt(Object obj, int index)
Inserts the specified object as a component in this vector at the specified index.
boolean isEmpty()
Tests if this vector has no components.
Object lastElement()
Returns the last component of the vector.
int lastIndexOf(Object elem)
Returns the index of the last occurrence of the specified object in this vector.
int lastIndexOf(Object elem, int index)
Searches backwards for the specified object, starting from the specified index, and returns an index to it.
Object remove(int index)
Removes the element at the specified position in this Vector.
boolean remove(Object o)
Removes the first occurrence of the specified element in this Vector If the Vector does not contain the element, it is unchanged.
boolean removeAll(Collection c)
Removes from this Vector all of its elements that are contained in the specified Collection.
void removeAllElements()
Removes all components from this vector and sets its size to zero.
boolean removeElement(Object obj)
Removes the first (lowest-indexed) occurrence of the argument from this vector.
void removeElementAt(int index)
removeElementAt(int index)
protected void removeRange(int fromIndex, int toIndex)
Removes from this List all of the elements whose index is between fromIndex, inclusive and toIndex, exclusive.
boolean retainAll(Collection c)
Retains only the elements in this Vector that are contained in the specified Collection.
Object set(int index, Object element)
Replaces the element at the specified position in this Vector with the specified element.
void setElementAt(Object obj, int index)
Sets the component at the specified index of this vector to be the specified object.
void setSize(int newSize)
Sets the size of this vector.
int size()
Returns the number of components in this vector.
List subList(int fromIndex, int toIndex)
Returns a view of the portion of this List between fromIndex, inclusive, and toIndex, exclusive.
Object[] toArray()
Returns an array containing all of the elements in this Vector in the correct order.
Object[] toArray(Object[] a)
Returns an array containing all of the elements in this Vector in the correct order; the runtime type of the returned array is that of the specified array.
String toString()
Returns a string representation of this Vector, containing the String representation of each element.
void trimToSize()
Trims the capacity of this vector to be the vector's current size.

Stack Class : Stack is a subclass of Vector that implements a standard last-in, first-out stack.
Constructor
Stack()
Methods
boolean empty()
Tests if this stack is empty. Returns true if the stack is empty, and returns false if the stack contains elements.
Object peek( )
Returns the element on the top of the stack, but does not remove it.
Object pop( )
Returns the element on the top of the stack, removing it in the process.
Object push(Object element)
Pushes element onto the stack. element is also returned.
int search(Object element)
Searches for element in the stack. If found, its offset from the top of the stack is returned. Otherwise, .1 is returned.



Queue Interface
  1. public boolean add(object);
  2. public boolean offer(object);
  3. public Object remove();
  4. public Object poll();
  5. public Object element();
  6. public Object peek();
Deque Interface
5.       public boolean addFirst(object);
6.       public boolean addLast(object);
7.       public boolean offerFirst(object);
8.       public boolean offerLast(object);
9.       public Object removeFirst();
10.    public Object removeLast();
11.    public Object pollFirst();
12.    public Object pollLast();
13.    public Object getFirst();
14.    public Object getlast();
15.    public Object peekFirst();
16.    public Object peekLast();

Java Map Interface

A map contains values based on the key i.e. key and value pair.Each pair is known as an entry.Map contains only unique elements.

Methods:

  1. public Object put(object key,Object value): is used to insert an entry in this map.
  2. public void putAll(Map map):is used to insert the specified map in this map.
  3. public Object remove(object key):is used to delete an entry for the specified key.
  4. public Object get(Object key):is used to return the value for the specified key.
  5. public boolean containsKey(Object key):is used to search the specified key from this map.
  6. public boolean containsValue(Object value):is used to search the specified value from this map.
  7. public Set keySet():returns the Set view containing all the keys.
  8. public Set entrySet():returns the Set view containing all the keys and values.


Entry

Entry is the subinterface of Map.So we will access it by Map.Entry name.It provides methods to get key and value.

Methods of Entry interface:

  1. public Object getKey(): is used to obtain key.
  2. public Object getValue():is used to obtain value.

Java HashMap class

  • A HashMap contains values based on the key. It implements the Map interface and extends AbstractMap class.
  • It contains only unique elements.
  • It may have one null key and multiple null values.
  • It maintains no order.

Hierarchy of HashMap class:

Java LinkedHashMap class

  • A LinkedHashMap contains values based on the key. It implements the Map interface and extends HashMap class.
  • It contains only unique elements.
  • It may have one null key and multiple null values.
  • It is same as HashMap instead maintains insertion order.






Post a Comment

0 Comments