Showing posts with label java Collection interface. Show all posts
Showing posts with label java Collection interface. Show all posts

  • TreeSet is class under Set interface of collection framework.
  • It implements NavigableSet interface which extends SortedSet interface.
  • Elements are ordered by a Comparator which is provided at set creation time or by natural ordering.
  • It maintains the ascending sorting order.
  • TreeSet is not synchronized so if multiple threads accessing it concurrently and any one modifies set entry then it must be synchronized externally.
  • Element's access and retrieval time is very fast from TreeSet so it will much useful when you wants to store large information in ascending order and retrieve any element quickly.
Java TreeSet Class hierarchy

  • SortedSet interface extends Set interface in collection framework of java.
  • As name suggest, It provides total ordering on elements of Set.
  • Set elements will be ordered using their natural ordering or using a comparator interface.
  • All the elements entries of SortedSet must implement Comparable interface.
  • All the elements of SortedSet must be comparable. So e1.compareTo(e2) must not throw ClassCastException.
  • In collection framework, TreeSet class is an implementation for the SortedSet interface.
Java SortedSet Interface hierarchy

  • ArrayDeque is one of the collection framework member which implements Deque, Cloneable and Serializable interfaces.
  • ArrayDeque provides Resizable-array implementation so it has no capacity restrictions and it will grow as per requirement.
  • In the absence of external synchronization, they are not thread safe. So they do not allow multiple threads to access it concurrently.
  • Also ArrayDequenot allow to store null elements.
  • ArrayDeque Class is faster than LinkedList when used as a queue and faster than Stack when used as a stack.
  • If ArrayDeque is modified after the iterator creation and if it is not modified by iterators own method then it will throw ConcurrentModificationException. So in concurrent modification, Iterator will fail.
Java ArrayDeque Class hierarchy

  • If you look at collection hierarchy, Queue extends collection interface.
  • Main purpose of collection is to hold the elements prior to processing.
  • Queue interface provides some additional operations like insertion, inspection and extraction besides collection interface operations.
  • All these three operations exist in 2 different forms. One return special value(null or false depending on the operation) if operation fails else throws an exception if operation fails.
  • Queue follows FIFO(first-in-first-out) so it will insert element at the end of the queue when you insert new element and element will be removed from the beginning of the queue when you remove element.
  • As Queue interface is sub type of collection interface, all methods of collection interface are also available in Queue interface.
Queue Interface hierarchy

  • Stack class in java is one of the collection interface class which is subclass of Vector class.
  • Stake class supports usual push and pop operations.
  • In contrast to queue, Stack class has last-in first-out(LIFO) data structure. So item which is inserted at top will be available first.
  • Stack class extends Vector class of List interface so it includes all methods of vector class and also it has it's own several methods to perform push and pop operations like looking top item from stack, removing top item from stack, push new item at top of stack, searching item from stack and check whether stack is empty.
java stack class example

  • Vector class in java implements List interface of collection framework.
  • Vector class is synchronized.
  • If you don't know size of array then you can use vector class as size of vector can grow and shrink as per adding and removing items.
  • As vector class is synchronized, It will give poor performance on add, delete, update and search operations.
  • Elements of vector can be accessed using it's integer index.
vector class hierarchy in java

  • LinkedList class extends AbstractList class.
  • LinkedList class implements List and Deque interfaces.
  • LinkedList class is using doubly linked list to store elements in list.
  • LinkedList can hold duplicate elements in list.
  • LinkedList is non synchronized.
  • Manipulation is faster in linkedlist as shifting is not required when new element is inserted or deleted from list.
  • You can use it as list, stack or queue as it implements List and Deque interfaces of collection framework.

What is Java Collections Framework?
Java Collections Framework is group of different interfaces, classes and algorithms. In simple word, Collection means single unit of objects and framework means good architecture. It is best architecture to store group of reusable objects and manipulate them. Collection framework has many different interfaces, their sub interfaces and classes, their classes and sub classes. All the interfaces and classes has different purpose. You can perform add, edit, delete, search, sort, etc operations on group of objects as per class's methods and implementation.

Java Collections Framework

Main Advantages Of Collection Framework
  • Main advantage of collection framework is it will reduces your programming efforts as there are ready made interfaces, classes and their methods which you can use directly in your program to perform operation on object collection. 
  • Collection is well structure framework so you code quality and speed will be increased.
  • Anyone can use collection very easily as it's interfaces, classes and methods very easy to understand and use.
Interfaces and Classes of Collection Framework
Collection, Set, List, Queue, Deque, SortedSet, Map and SortedMap are core interfaces of collection framework. Detailed description of collection framework's interfaces and it's classes is given on below given pages.

  • List interface in java is ordered collection so it can store elements in sequence.
  • List interface is sub interface and it extends to Collection interface.
  • List interface can hold duplicate elements so you can insert duplicate elements too.
  • You can access elements from list by integer index.
  • Collection framework has many interfaces in java and list interface is one of them.
  • List interface provides ListIterator to traverse through elements list in backward and forward directions.
List interface java

  • Collection is an interface in java.
  • Collection interface extends Iterable interface.
  • In hierarchy, Collection interface is root of List, Queue and Set interfaces.

Java Collection Interface

  • It represents unit of its elements. i.e. group of objects.
  • Some collections do not allow duplicate elements but some allows duplicate elements.
  • Collection interface is base on which collection framework is built.