Collections in Java

Collections in java is framework that provide an architecture to store and manipulate group of objects.

1) Iterator Interface:
a) hasNext()
b) next();
c) remove(); // remove last returned element.

2) ListIterator  Interface  // ListIterator extends Iterator, it allow to traverse  in either direction.
a) add(Object obj);
b) hasNext();
c) hasPrevious();
d) next();
e) previous();
f) remove();
g) set(Object obj ); // set last returned element  next/ previous


3) RandomAccess Interface // dummy or empty interface or a marker

interface RandamAccess
{}

A Class implements only List interface provides sequential access but if a class also implements RandamAccess then it provide randam access.

4) Compatrator Interface :

int compare (Object o1, Object o2) ;// o1 > o2  then positive value


5) Collection Interface : it declare fundamental mehods because all the collection implements  collection frameworks.

boolean add ( object obj);
void clear(); //remove all the elements
boolean contains (object obj); //return true  if collection contains obj
boolean equals (object obj);
boolean isEmpty();
Iterator iterator();
boolean remove (object obj);
int size ();   //return number of elements
Object[] toArray();  // return a array which contain all the elements of cllection
String toString()  // return all the elements of collection in string enclosed between [] and separated by comma.

6) List Interface : List interface extends collection interface and in List we can add/remove   elements by index with zero based index and can have duplicate elements.

add(int index , object elements); //no element is removed  post elements are shifted up.
get(int index)
indexOf(object element)
listIterator(0
remove(int index)
set(int index, object element) //replace index element with new element.

7) Set Interface: it extends collection interface and does not provide any extra method and it not allows duplicate elements.


8) SortedSet: it extend the set and provide a behavior of sorted in ascending order.
object first()  // first element of set or smallest element
object last()   // largest element

###Collection Classes:
9) ArrayList Class:
Constructor: ArrayList() // create a array with initial capacity of ten
                     ArrayList(Collection a);  //create a list using a collection object
                     ArrayList(int capacity)
Methods:
                    void ensureCapacity(int cap);
                     void trimToSize()  // reduce the size of the array as large as the number of elements.

public static void main(String[] arg){

ArrayList a = new ArrayList();     //its a generic type  but we can restrict type of elements
System.out.println("size of ArrayList " + a.size());
System.out.println("size of ArrayList " + a.size());
a.add(11);
a.add("A");
a.add('a');
a.add(7.77);
a.add(2,'N');
System.out.println("size of ArrayList " + a.size());
System.out.println("elements  of ArrayList " + a);

}

public static void main(String[] arg){

ArrayList <String> a = new ArrayList<String>();
System.out.println("size of ArrayList " + a.size());
System.out.println("size of ArrayList " + a.size());
a.add(11);   //error ArrayList can store Strings
a.add("A");
a.add('a');       //error ArrayList can store Strings
a.add(7.77);    //error ArrayList can store Strings
a.add(2,'N');     //error ArrayList can store Strings
System.out.println("size of ArrayList " + a.size());
System.out.println("elements  of ArrayList " + a);

}

## Display elements of an ArrayList using Iterator
 Iterator it =  a.iterator();
while(it.hasNext())


## Display using array
Object a[] = a.toArray();
for (int i =0 ; i < a.length ; i++)


## for-each loop
for(String str : a);             // for(Object obj : a)

9) LinkedList Class: Implements List interface
Constructor:  LinkedList()
                      LinkedList(Collection c);


Methods:
               void addFirst(object obj);
                Object getFirst()
                Object getLast()
                 Object removeFirst/Last()

10) HashSet: Class implements Set interface it create s collection that use hash table for storage. No specific  way of order of elements
Constructor: HashSet()      //load factor .75    and capacity with 16
                     HashSet(Collection c)     //load factor .75
                     HashSet(int capacity)     //load factor .75
                     HashSet(int capacity , float fillRatio) //




11) LinkedHashSet: extends HashSet, it does not implements any other methods but store elements in  inserted order.

12) TreeSet: it provide implementation of SortedSet interface.
Constructor: TreeSet()
                     TreeSet(Collection c)
                      TreeSet(Comparator c)

13) Map Interface : A map is an object which store value with respect to key. Some map may accepts null key and null values.

Methods:
clear();
containKey(Object Key) // Return true if map have that key.
containValue(Obect Value)
entrySet()  //Return set that contain the entries in a map.
keySet()  // Return a set that contain the keys
get(Object key) // Return a value at that value
isEmpty() //true if map dont have key-value mapping
put(Object key, Object key)
remove(Object key)
size()  // Returns the number of key-value mappings in a map
values() // Returns a collection view of the values contained in this map.


14) SortedMap: extends Map and ensure  that the entries are in ascending order.
firstKey() // return first key
lastKey()

15) Map.Entry:
getKey()  // return a  key for that entry
getValue()



###Map Class
16) HashMap Class: use hash table to implement the Map Interface.
Constructor: HashMap()     //empty HashMap with default initial capacity 16 with .75 load factor.
                     HashMap(int  capacity)   //
                     HashMap(ini capacity, load factor)
                     HashMap(Map m);


17) LinkedHashMap Class: Extend the HashMap Class

18) TreeMap Class:  Implements ShortedMap interface by using tree.

Constructor: TreeMap();
                      TreeMap(Comparator c);
                      TreeMap(Map m);

### The Legacy Classes and Interface : all classes are synchronized but none of the collection classes is synchronized but we can make synchronize using synchronized block.

19) The Enumeration Interface:
hasMoreElements();
nextElement() // return the next element of this enumeration

20) Vector Class: Implements dynamic array, it is similar to ArrayList() with two difference  a) Vector is synchronize and contain legacy methods.

Constructor: Vector()   // data array with capacity 10;
                      Vector( Collection c)
                       Vector( capacity)
                         Vector(int capacity , int capacityIncriment)

Methods:
addElements(Object obj)  //add to the end of  this vector and increase size by one.
capacity()          // Return the current capacity of this Vector
copyInto(Object[] anArray) // copies the entries into specified  index.
elementAt(int index)           // return element at this index
elements()  // return an enumeration of this vector
ensureCapacity( int minCapacity)
indexOf(Object element)  
insertElementAt(Object obj, int index)
isEmpty();
removeAllElements() //remove all and set to size to zero
removeElement(Object obj)   // remove first element
removeElementAt(int index)
size();
toArray()  //Return an array containing all elements in this array
toString()  //Return  a string repersentation of this vector
trimToSize() //capacity is equal to number of elements.

21) Stack Class: LIFO stack
boolean empty();
Object peek();
Object pop();
push(Object obj);

22) Dictionary Class: Its a abstract class operates much like a Map.
Enumeration elements() // Return an enumeration of values in this dictionary
get(Object key)
isEmpty()
keys()
put(Object key, Object values)
remove(Object key)  // key and corresponding value
size();

23) HashTable Class: Its like a HashMap but its synchronize .
Constructor:
   Hashtable()            // capacity 11 and load factor .75
    HashMap(int  capacity)   //
    HashMap(ini capacity, load factor)
     HashMap(Map m);

Methods:
clear();
containsKey();
containsValue(Object values)
elements()
entrySet()
get(Object key)
isEmpty();
keys();
keySet()
put(Object key, Object values);
remove (object key)
size()
toString();
values()   //Return a Collection view


24) Properties Class: Its sub-Class of Hashtable. maintain a list of values in which keys and values both are string

Methods: along with Hashtable methods
String getProperty(String key) // Return value
String getProperty(String key, String default) // If key is not present then it send default value.
Enumeration propertyNames();   // Enumeration of all the keys
Object  setProperty(String key, String values);

Comments

Popular posts from this blog

Introduction to Java

Creational Design Pattern: Factory Design Pattern

Design Patterns: The Secret Weapon of Expert Developers