Java Programming
arrays
[index]- get/set at index.length- get the size of arrayint[] arr = new int[size]- create array of sizesize
ArrayList
add(value)- add value to endadd(index, value)- add value at indexremove(index)- remove value at indexremove(value)- removevaluefrom listclear()- remove all elementsget(index)- get item at indexset(index, value)- set value for given indexsize()- size of listisEmpty()- check if list is emptycontains(value)- check if value is presentindexOf(value)- get the index of valuelastIndexOf(value)- get the last index of valueremoveIf(lambda: condition)- remove elements which does not match the condition
HashMap (ordering not preserved)
put(key, value)- insert key value pait in hashmapget(key)- get value for given keyremove(key)- remove key value pair from mapcontainsKey(key)- check if key is present in hashmapcontainsValue(value)- check if value exists in hashmapsize()- give the size of mapisEmpty()- check if map is emptymap.keySet()- get list of keysmap.values()- get list of valuesmap.entrySet()- get list of map entriesentry.getKey()entry.getValue()
getOrDefault(key, defaultValue)- get key value, if not present return defaultValue
HashSet
add(value)remove(value)contains(value)isEmpty()size()clear()
Stack
push(value)pop()- removes and returns itemstop()- returns top valueisEmpty()size()
Queue, LinkedList, ArrayDeque
add(item)- add element to tailremove()- remove head element and returnpeek()- return head elementisEmpty()size()contains()clear()
PriorityQueue
add(item)- add item to priority queuepoll()- remove and return the highest elementpeek()- return the highest elementremove(item)- remove item from the priority queuecontains(item)size()clear()-
custom comparator
class PersonAgeComparator implements Comparator<Person> { public int compare(Person p1, Person p2) { return Integer.compare(p1.age, p2.age); } } Comparator<Person> byAge = new PersonAgeComparator();
Short summary
HashSet- implemented using hashingLinkedHashSet- same asHashSetbut it maintains insertion orderTreeSet- implemented using a binary search tree(Red black trees), so it can be used to access items in orderArrayList- like dynamic arraysVector- like array list but synchronize, (legacy)LinkedList- a simple double linked listPriorityQueue- implemented using heapHashMap- implemented using hashingLinkedHashMap- same asHashMapbut it maintains insertion orderTreeMap- implemented using binary search tree, so items can be accessed in order