Tuesday, 2 October 2018

Java collections framework Overview


  • Java collections framework belongs to java.util package       
  •          Collection's works a bit like arrays, except their size can change dynamically, and they have more advanced behaviour than arrays.
  •      There are two "groups" of interfaces: Collection's and  Map's


  1.    Collection
    1. List
      1. The java.util.List interface is a subtype of the java.util.Collection interface
      2. It represents a n ordered list of objects, meaning you can access the elements of a List in a specific order, and by an index too.
      3. You can add an element anywhere in the list, change an element anywhere in the list, or remove an element from any position in the list.
      4. You can also add the same element more than once to a List.
      5. The List Implementations are,
        1. Arraylist
        2. Vector
        3. Linkedlist
    2. Set
      1. It represents set of objects, meaning each element can only exists once in a Set.
      2. Internal datas structure of set is hashmap and unique key feature of hashmap made set accept only unique objects 
      3. The List Implementations are,
        1. HashSet
        2. LinkedHashSet
        3. TreeSet
    3. Queue
      1. A queue is also ordered, but you'll only ever touch elements at one end. All elements get inserted at the "end" and removed from the "beginning" (or head) of the queue. You can find out how many elements are in the queue, but you can't find out what, say, the "third" element is. You'll see it when you get there.
      2. The Queue Implementations are,
        1. Linkedlist
        2. PriorityQueue 
    4. Dequeue
      1. Deque is short for "double ended queue". With an ordinary queue, you add things to one end and take them from the other. With a double ended queue, you can add things to either end, and take them from either end.
      2. The Dequeue Implementations are
        1. Linkedlist
        2. ArrayDeque
    5. Stack
      1.  Stack is liner type data structure, i.e. they are arranging in liner manner. In stack whatever is stored first it comes out last. It works in LIFO manner(Last in first out)
      2. In stack you can’t add element in between. They are like a stack of coins, i.e. if you want to take the last coin, then all the upper coins have to be removed one by one.
  2.    Map
    1. Map Interface Store data as key value pair
      1. Hashmap
      2. Hashtable
      3. Treemap
      4. Linked Hashmap
  • Arraylist
    • ArrayList is implemented as a resizable array. It's elements can be accessed directly by using the get and set methods, since ArrayList is essentially an array.
    • When an element is inserted into an ArrayList, the object will need to expand its internal array if it runs out of room. the ArrayList increases its array size by 50 percent.
  • Vector
    • The chief difference from ArrayList is that its methods are synchronized (ArrayList's are not). That means it is easier to use in multi-threaded environments, but it does incur the synchronization overhead.
    • When an element is inserted into  a Vector, the object will need to expand its internal array if it runs out of room. A Vector defaults to doubling the size of its array
  • Linkedlist
    •  A linked list is a data structure consisting of a group of nodes which together represent a sequence. Each     node is composed of a data and a reference to the next node in the sequence. This structure allows for efficient insertion or removal of elements from any position in the sequence. 
    • In a singly linked list each node has only one link which points to the next node in the list.
  • HashSet
    • It makes no guarantees about the sequence of the elements when you iterate them
  • LinkedHashSet
    • LinkedHashSet differs from HashSet by guaranteeing that the order of the elements during iteration is the same as the order they were inserted into the LinkedHashSet.
  • TreeSet
    • Guarantees the order of the elements when iterated, but the order is the sorting order of the elements.
  • PriorityQueue 
    • Stores its elements internally according to their natural order (if they implement Comparable), or according to a Comparator passed to the PriorityQueue.
  • ArrayDeque
    • ArrayDeque stores its elements internally in an array. If the number of elements exceeds the space in the array, a new array is allocated, and all elements moved over. In other words, the ArrayDeque grows as needed, even if it stores its elements in an array
  • Stack
    • A Stack is a data structure where you add elements to the "top" of the stack, and also remove elements from the top again.
    • This is also referred to as the "Last In First Out (LIFO)" principle.
  • Hashmap
    • HashMap maps a key and a value. It does not guarantee any order of the elements stored internally in the map
  • Hashtable
    • Hashtable is synchronized
    • Hashtable does not allow null keys or values.
  • Treemap
    • TreeMap also maps a key and a value. Furthermore it guarantees the order in which keys or values are iterated - which is the sort order of the keys or values.
  • Linked Hashmap
    • Having order when iterate

No comments:

Post a Comment

Search This Blog

Contact us

Name

Email *

Message *