Work with Collections in Java.(for students)
Java Collections Framework (JCF) in Java Tutorial for students of universities
Author: Oxana Dudnik
Hierarchy of Collection Framework Methods of Collection interface 1.public boolean add(Object element)is used to insert an element in this collection.
2.public boolean addAll(collection c)is used to insert the specified collection elements in the invoking collection.
3.public boolean remove(Object element)is used to delete an element from this collection.
4.public boolean removeAll(Collection c)is used to delete all the elements of specified collection from the invoking collection.
5.public boolean retainAll(Collection c)is used to delete all the elements of invoking collection except the specified collection.
6.public int size()return the total number of elements in the collection.
7.public void clear()removes the total no of element from the collection.
8.public boolean contains(object element)is used to search an element.
9.public boolean containsAll(Collection c)is used to search the specified collection in this collection.
10.public Iterator iterator()returns an iterator.
11.public Object[] toArray()converts collection into array.
12.public boolean isEmpty()checks if collection is empty.
13.public boolean equals(Object element)matches two collection.14public int hashCode()returns the hashcode number for collection. Methods of Iterator interface public boolean hasNext() it returns true if iterator has more elements.
public object next() it returns the element and moves the cursor pointer to the next element.
public void remove() it removes the last elements returned by the iterator. It is rarely used.
Java ArrayList class Java ArrayList class uses a dynamic array for storing the elements.It extends AbstractList class and implements List interface.
Java ArrayList class can contain duplicate elements.
Java ArrayList class maintains insertion order.
Java ArrayList class is non synchronized.
Java ArrayList allows random access because array works at the index basis.
In Java ArrayList class, manipulation is slow because a lot of shifting needs to be occurred if any element is removed from the array list.
Java LinkedList class Java LinkedList class uses doubly linked list to store the elements. It extends the AbstractList class and implements List and Deque interfaces.
Java LinkedList class can contain duplicate elements.
Java LinkedList class maintains insertion order.
Java LinkedList class is non synchronized.
In Java LinkedList class, manipulation is fast because no shifting needs to be occurred.
Java LinkedList class can be used as list, stack or queue.
Java HashSet class uses hashtable to store the elements.It extends AbstractSet class and implements Set interface.
contains unique elements only.
Difference between List and Set:
List can contain duplicate elements whereas Set contains unique elements only. Java LinkedHashSet class: contains unique elements only like HashSet. It extends HashSet class and implements Set interface.
maintains insertion order.
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.
Commonly used methods of Map interface:
public Object put(object key,Object value): is used to insert an entry in this map.
public void putAll(Map map):is used to insert the specified map in this map.
public Object remove(object key):is used to delete an entry for the specified key.
public Object get(Object key):is used to return the value for the specified key.
public boolean containsKey(Object key):is used to search the specified key from this map.
public boolean containsValue(Object value):is used to search the specified value from this map.
public Set keySet():returns the Set view containing all the keys.
public Set entrySet():returns the Set view containing all the keys and values. 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.
What is difference between HashSet and HashMap?
HashSet contains only values whereas HashMap contains entry(key and value). Java Hashtable class A Hashtable is an array of list.Each list is known as a bucket.The position of bucket is identified by calling the hashcode() method.A Hashtable contains values based on the key. It implements the Map interface and extends Dictionary class.
It contains only unique elements.
It may have not have any null key or value.
It is synchronized. Difference between HashMap and Hashtable Static Methods on Collections •Search and sort: binarySearch(), sort()Reorganization: reverse(), shuffle()Wrappings: unModifiableCollection, synchonizedCollection Method of Collections class for sorting List elements public void sort(List list): is used to sort the elements of List.List elements must be of Comparable type. Comparable interface Syntax:
public int compareTo(Object obj): is used to compare the current object with the specified object. Comparator interface Syntax of compare method
public int compare(Object obj1,Object obj2): compares the first object with second object. Tests http://www.javatpoint.com/directload.jsp?val=92