PriorityQueue Example in java

PriorityQueue(Since Java 1.5) : It is an unbounded priority queue based on priority heap. The elements of the priority queue are ordered according to the constructor which you use. PriorityQueue pq = new PriorityQueue() It creates empty priority queue with default initial capacity 11. Priority order is natural sorting order. PriorityQueue pq = new PriorityQueue(int […]

TreeSet in java

TreeSet(Since Java 1.2) Underlying data structure is balanced tree. Duplicates are not allowed. Insertion order is not preserved since objects will be inserted based on some sorting technique. Null insertion is not possible in a non-empty TreeSet. We will get NullPointerException if we add. Heterogeneous objects are not allowed.  We will get ClassCastException if we […]

HashSet and LinkedHashSet

HashSet (Since Java 1.2):  HashSet is achild interface of Set.  Underlying data structure is HashTable.(Internally it’s a HashMap. See HashTable code)  Insertion order is not preserved.  Duplicates are not allowed.  Allows null element ( only one since set will not allow duplicates).  Default initial capacity is 16.  Default load factor is 0.75f.  Threshold = capacity […]

ArrayList,LinkedList,Vector and Stack

ArrayList (Since Java 1.2): Grow able Array implementation of List interface. Insertion order is preserved. Duplicate elements are allowed. Multiple null elements of insertion are allowed. Default initial capacity of an ArrayList is 10. The capacity grows with the below formula, once ArrayList reaches its max capacity. newSize/newCapacity= (oldCapacity * 3)/2 + 1

Output: […]