List: List Names New Linkedlist : O (1) (Size, Get, Set, ... ), O (N) - Add Operation

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 7

List: List<String> names = new LinkedList<>();

<Insertion order preserved; duplicates allowed>


(1) ArrayList [O(1) (size, get, set, ...), O(n) - add operation ]
Dynamic array(Initial capacity: 10, then raise it by 50%)
Adding/deletion is between is costly
Fetching is fast

(2) LinkedList [all operation O(1) (including add() ), except for retrieving n-th element which is O(n).]
Adding/deletion is not expensive
Double LinkedList

(3) Vector  Stack

Set: Set<String> set = new HashSet<>();


< Insertion order NOT preserved, duplicates are NOT allowed>
(1) Hashset  LinkedHashSet
(2) SortedSet  TreeSet [all operations O(log(N)). size() operation takes O(log(n)) ]

Queue:
(1) Priority Queue
(2) Dequeue

Map: Map<String, String> myMap = new ConcurrentHashMap<String,String>

(1) Hashmap  LinkedHashMap


Not synchronized
One null key, any no of null values
Fast & uses less memory

(2) Hashtable
Synchronized
No null key / Value
Slow & more memory
(3) SortedMap: Key based sorting
(4) ConcurrentHashMap

ArrayList - O(1) (size, get, set, ...), O(n) - add operation.


LinkedList - all operation O(1) (including add() ), except for retrieving n-th element which
is O(n). I assume size() operation runs in O(1) as well, right?
TreeSet - all operations O(lg(N)). size() operation takes O(log(n)), right?
HashSet - all operations O(1) if proper hash function is applied.
HashMap - all operations O(1), analogous to HashSet.

List

A list is an ordered collection of elements.

Add Remove Get Contains Data  Structure


ArrayList  O(1) O(n) O(1) O(n) Array
LinkedList  O(1) O(1) O(n) O(n) Linked List
CopyonWriteArrayList  O(n) O(n) O(1) O(n) Array

Set

A collection that contains no duplicate elements.

Add Contains Next Data Structure


HashSet O(1) O(1) O(h/n) HashMap
Key Value
(hashset entry) (Always present)
“Tom” PRESENT
“Peter” PRESENT
“Sam” PRESENT

LinkedHashSet O(1) O(1) O(1) Hash Table + Linked List


EnumSet O(1) O(1) O(1) Bit Vector
TreeSet O(log O(log n) O(log n) Red-black tree
n)
CopyonWriteArraySet O(n) O(n) O(1) Array
ConcurrentSkipList O(log O(log n) O(1) Skip List
n)

Queue

A collection designed for holding elements prior to processing.

Offer Peak Poll Size Data Structure


PriorityQueue O(log n ) O(1) O(log n) O(1) Priority Heap
LinkedList  O(1) O(1) O(1) O(1) Array
ArrayDequeue  O(1) O(1) O(1) O(1) Linked List
ConcurrentLinkedQueue  O(1) O(1) O(1) O(n) Linked List
ArrayBlockingQueue  O(1) O(1) O(1) O(1) Array
PriorirityBlockingQueue O(log n) O(1) O(log n) O(1) Priority Heap
SynchronousQueue O(1) O(1) O(1) O(1) None!
DelayQueue O(log n) O(1) O(log n) O(1) Priority Heap
LinkedBlockingQueue O(1) O(1) O(1) O(1) Linked List

Map

An object that maps keys to values. A map cannot duplicate keys; each key can map to at most one
value.

Get ContainsKey Next Data Structure


HashTable Initial Capacity = 11,
Load Factor = 0.75
HashMap O(1) O(1) O(h / n) Hash Table
Initial Capacity = 16,
Load Factor = 0.75
LinkedHashMap O(1) O(1) O(1) Hash Table + Linked List
IdentityHashMap O(1) O(1) O(h / n) Array
WeakHashMap O(1) O(1) O(h / n) Hash Table
EnumMap O(1) O(1) O(1) Array
TreeMap O(log n) O(log n) O(log n) Red-black tree
ConcurrentHashMap O(1) O(1) O(h / n) Hash Tables
[Default Concurrency level = 16]
ConcurrentSkipListMap O(log n) O(log n) O(1) Skip List

Method to calculate hash : [Division method, Folding method, Mid Square method,
module multiplication method]

Division method: h(ki) = ki % m [h(ki)=hash function, ki =key, m=size of HashMap]


e.g.
ki = 0, m = 16  0%16 = 0
ki = 5, m = 16  5%16 = 5
ki = 10, m = 16  10%16 = 10
ki = 15, m = 16  15%16 = 5
ki = 16, m = 16  16%16 = 0
ki = 150, m = 16  150%16 = 6

java.util.HashMap :
 Not thread safe, need to use synchronised operation when manipulating form
multiple threads
 Iteration not guaranteed in insertion order

java.util.LinkedHashMap
 Insertion order is preserved
 Doubly linked list is used internally, which means extra space

java.util.TreeMap
 Implementation of SortedMap & NavigableMap
 “Natural sorted” order of key, i.e. keys are sorted in ASC order.
 Red black tree (Specific type of balanced search tree / self-balancing binary
search tree) based implementation
 Key should implement Comparable interface
o If not then ClassCast Exception will be thrown
o Or provide an explicit Comparator in constructor

java.util.IdentityHashMap

java.util.EnumMap
 All keys must come from a single Enum type
 Null key not permitted, not synchronized

Java.util.WeakHashMap

ConcurrentHashMap
While one thread is Iterating the HashMap object, if another thread tries to add/modify
the contents of the Object then we will get a Run-time exception saying 
ConcurrentModificationException. Whereas In ConcurrentHashMap we won’t get any
exception while performing any modification at the time of Iteration.

In HashMap, null values are allowed for key and values, whereas in
/ZX/?/ConcurrentHashMap null value is not allowed for key and value, otherwise, we will
get a Run-time exception saying NullPointerException.

Is there any reason to have 8 on


TREEIFY_THRESHOLD in Java Hashmap?
A linked list is faster than a tree until log(N) < N/2, which happens at N = 8

 If the bucket contains a small number of nodes (< 8), it uses a LinkedList for all of
this bucket's nodes.
 If the bucket contains a large number of nodes (=> 8), it dynamically replaces it with
an ad-hoc implementation of treemap, for all of this bucket's nodes.
ArrayList:
How to Synchronize (ThreadSafe) ArrayList in Java

1. Collection.synchornizedList() – method – returns synchronised list

2. copyOnWriteArrayList – class – ThreadSafe variant of ArrayList


Remove duplicate form array list:

You might also like