Java Api
Sorting
Sort Array
-
Arrays.sort(int[] arr)
: sort arr in ascending order -
Arrays.sort(int[] arr, int from, int to)
: sort arr from index from to index to in ascending order -
Arrays.sort(int[] arr, Comparator<Integer> c)
: sort arr in ascending order, using Comparator c
Sort List
-
Collections.sort(List<Integer> list)
: sort list in ascending order -
Collections.sort(List<Integer> list, Comparator<Integer> c)
: sort list in ascending order, using Comparator c
Array
-
int[] arr = new int[10]
orint[] arr = {1, 2, 3}
: initialize array -
arr.length
: get array length -
Arrays.sort(arr)
: sort arr in ascending order -
Arrays.toString(arr)
: convert arr to String -
Arrays.copyOf(arr, int length)
: copy arr to a new array with length -
Arrays.copyOfRange(arr, int from, int to)
: copy arr from index from to index to -
Arrays.fill(arr, int val)
: fill arr with val -
Arrays.asList(arr)
: convert arr to List
String
-
String.toCharArray()
convert String to char[] -
str.length()
get length of str -
str.charAt(int index)
get char at index of str -
str.contains(String str)
check if str contains str -
str.indexOf(String str)
get the first index of str in str, else return -1; -
String.valueOf(Object obj)
convert obj to String -
String.split(String regex)
split str by regex
Generate new String
-
new String(char[] value, int offset, int count)
generate a new String from char[] value, from offset to offset + count -
str.substring(int left, int right)
[left, right)
generate a new String from str, from left to right
String Builder
-
Initialization
: StringBuilder sb = new StringBuilder(); -
sb.append(String str)
: add str to sb -
sb.length()
: get length of sb -
sb.toString()
: convert sb to String -
sb.reverse()
: reverse sb -
sb.charAt(int index)
: get char at index of sb -
sb.insert(int offset, String str)
: insert str to sb at offset -
sb.deleteCharAt(int index)
: delete char at index of sb -
sb.delete(int start, int end)
: delete sb from start to end
Character
-
Character.isDigit(char c)
check if c is a digit -
Character.isLetter(char c)
check if c is a letter -
Character.isLetterOrDigit(char c)
check if c is a letter or digit -
Character.isLowerCase(char c)
check if c is a lower case letter -
Character.isUpperCase(char c)
check if c is a upper case letter -
Character.toLowerCase(char c)
convert c to lower case letter -
Character.toUpperCase(char c)
convert c to upper case letter
List
-
Initialization
: Listlist = new ArrayList<>(); or List list = new LinkedList<>(); -
list.add(Integer val)
: add val to list -
list.size()
: get length of list -
list.get(int index)
: get element at index of list -
list.remove(int index)
: remove element at index of list -
list.remove(E e)
: remove first e in list -
list.subList(int from, int to)
: get subList from from to to -
list.contains(E e)
: check if list contains e -
list.indexOf(E e)
: get the first index of e in list
Set
-
Initialization
: Setset = new HashSet<>(); or Set set = new TreeSet<>(); -
set.add(Integer val)
: add val to set -
set.size()
: get length of set -
set.remove(E e)
: remove e in set -
set.contains(E e)
: check if set contains e -
set.isEmpty()
: check if set is empty
Map
Map.computeIfAbsent(K key, Function<? super K, ? extends V> mappingFunction)
:
// when you want to create (key : List<>) or (key : queue<>) generally you need below:
Deque<Integer> queue;
if (!map.containsKey(nums.get(i))) {
queue = new ArrayDeque<>();
queue.offerLast(i);
map.put(nums.get(i), queue);
} else {
queue = map.get(nums.get(i));
queue.offerLast(i);
}
// now use
Deque<Integer> queue = map.computeIfAbsent(nums.get(i), num -> new ArrayDeque<>());
// if not contains, create a new queue, if contains, return that value;
-
Initialization
: Map<Integer, Integer> map = new HashMap<>(); or Map<Integer, Integer> map = new TreeMap<>(); -
map.put(K key, V value)
: add key-value pair to map -
map.get(K key)
: get value of key in map -
map.getOrDefault(K key, V defaultValue)
: get value of key in map, if key does not exist, return defaultValue -
map.size()
: get length of map -
map.containsKey(K key)
: check if map contains key -
map.containsValue(V value)
: check if map contains value -
map.remove(K key)
: remove key-value pair in map -
map.isEmpty()
: check if map is empty
traverse map
for (Map.Entry<K, V> entry : map.entrySet())
: traverse map,entry.getKey()
get key,entry.getValue()
get value
Queue & Stack & Deque (use Deque)
-
Initialization
: Dequedeque = new ArrayDeque<>(); -
deque.offerFirst(E e)
: add e to deque’s head -
deque.offerLast(E e)
: add e to deque’s tail -
deque.pollFirst()
: remove deque’s head -
deque.pollLast()
: remove deque’s tail -
deque.peekFirst()
: get deque’s head -
deque.peekLast()
: get deque’s tail -
deque.size()
: get length of deque -
deque.isEmpty()
: check if deque is empty
PriorityQueue
-
Initialization
: PriorityQueuepq = new PriorityQueue<>(); PriorityQueue<E> pq = new PriorityQueue<>(int size, new Comparator<E>(){ @Override Public int compare(E e1, E e2){ Return e1.getValue().compareTo(e2.getValue()); } }); // lambda PriorityQueue<E> pq = new PriorityQueue<>(int size, (e1, e2) -> e1.getValue().compareTo(e2.getValue()));
By default, a PriorityQueue is a min heap, to make it a max heap, we need to override the comparator.
-
pq.offer(E e)
: add e to pq -
pq.poll()
: remove pq’s head -
pq.peek()
: get pq’s head
Math
-
Math.abs(int x)
: get x absolute value -
Math.max(int x, int y)
: get max value of x and y -
Math.min(int x, int y)
: get min value of x and y -
Math.pow(int x, int y)
: get x to the power of y -
Math.sqrt(int x)
: get square root of x -
Math.ceil(double x)
: get smallest integer that is greater than x -
Math.floor(double x)
: get largest integer that is smaller than x -
Math.round(double x)
: get the closest integer to x -
Math.random()
: get a random double value between 0 and 1