Friday, March 10, 2017

Collections, Wrapper Classes


Collection framework is a architecture in JAVA to represent and manipulate collections in standard way. Collections frame work consist of following parts
  1. Interfaces: Collections framework provides some important abstract data types to represent collections.
    It contains some of important method such as add(), clear(), remove(), size(), iterator(), that every class must implement and some other important interfaces are List, Map, Queue, Set.
  2. Implementation classes: Collections framework provides more implementation classes such, ArrayList, LinkedList, TreeMap, HashMap, TreeSet



When to use List, Set and Map in Java


1) If you need to access elements frequently by using index, than List is a way to go. Its implementation e.g. ArrayList provides faster access if you know index.

2) If you want to store elements and want them to maintain an order on which they are inserted into collection then go for List again, as List is an ordered collection and maintain insertion order.

3) If you want to create collection of unique elements and don't want any duplicate than choose any Set implementation e.g. HashSetLinkedHashSet or TreeSet. All Set implementation follow there general contract e.g. uniqueness but also add addition feature e.g. TreeSet is a SortedSet and elements stored on TreeSet can be sorted by using Comparator or Comparable in Java. LinkedHashSet also maintains insertion order.

4) If you store data in form of key and value than Map is the way to go. You can choose from HashtableHashMapTreeMap based upon your subsequent need. In order to choose between first two see difference between HashSet and HashMap in Java.

List Interface : There are various classes in collection framework which implements the List interface some of them are

class ArrayList<T> //Implements  List, Random access{

  Implements the Random access, 
  Access time is fast //array 
  Arraylist is not threadsafe all methods are not synchronised
  Insertion and Deletion is slower compare to linkedlist

}


class Vector {

}





 Wrapper Classes

The main objectives of the wrapper  classes are, to wrap primitive into object form so we can handle the 
primitives also just like objects.