Java Collections and Data Structures Flashcards
what are “ad hoc” classes
before java 2 , java provide Dictionary,Vector,Stack and Properties classes.
What is Collection
Collection is an interface that extends Iterable and extended by List , Queue and Set Interfaces.
Collection package
java.util
Collection hierarchy
https://www.tutorialspoint.com/java/images/hierarchy-of-collection-framework.jpg
Or tutorialspoint website in Collections Framework section ( if website not found ).
Some Collections methods
.sort(),shuffle(),max(),min()
Some List interface methods
add(val)/add(i,val)
addAll(i,collection)/addAll(collection),
remove(i),
set(i,val),
get(i)
What is Queue
Queue interface implements FIFO
What is the objective of Queue ?
it’s holding elements before processing them.After the process, the element is deleted from queue and we pass to next one.
Classes that implements Queue
LinkedList
ArrayDeque
PriorityQueue
Interfaces that extends Queue
Deque
BlockingQueue
BlockingDeque
Some Queue methods
.add()
.peek() : give head (1st element ) of queue
.remove()
.size()
What is Map
Map is an interface which has a unique key object and their values
Map and Collection
Map doesn t implements Collection !!!!!!!!!!!!!!!!!
Some Map Exception
*NullPointerException : is thrown when we try to use a null object ( which is not allowed in map )
*ClassCastException : is thrown when an object is incompatible with the map elements type.
Classes that implements Map
HashMap
LinkedHashMap
TreeMap
Interface that implements Map
SortedMap
some Map methods
put(key,val)
remove(key)
getOrDefault(key,defaultValue)
containsKey(key)
containsValue(val)
keySet()
entrySet()
values()
size()
SortedMap Interface
SortedMap extends Map interface , it ensures that entries are maintained in ascending key order.
Some SortedMap Exception
*NoSuchElementException : when no items in the invoking map
*Others exceptions of Map : look above
Class that implements SortedMap
TreeMap
SortedMap Disadvantage
every time we add an item it does resort so it affects performance negatively
Why we use Set Interface ?
Set interface can’t contains duplicates
Some Set methods
contains()
add()
clear()
remove()
size()
isEmpty()
SortedSet
it’s like SortedMap in Map
LinkedHashMap vs TreeMap
LinkedHashSet vs TreeSet
LinkedHashMap maintain in which items are inserted in Map but TreeMap sort entries based on sorting key in ascending order
=> Same goes for LinkedHashSet vs TreeSet
Data Structure
Enumeration ( it’s not a data structure , but important in the context of DS )
BitSet
Stack
Vector
Dictionary
HashTable
Properties
What is Enumeration ?
Enumeration is an interface that is used to retrieve successive elements from DS
Some Enumeration methods
elements()
keys()
hasMoreElements()
nextElement()
What is BitSet ?
BitSet class used for set of Boolean values
What is Vector ?
Vector is a class similar to array except it can increase and decrease its size dynamically depending on how much elements
What is Stack ?
Stack is a class that implements LIFO