Show Menu
Cheatography

Java Coding Interview Cheat Sheet (DRAFT) by

Java coding interview

This is a draft cheat sheet. It is a work in progress and is not finished yet.

Wrapper Class

byte
Byte
short
Short
int
Integer
long
Long
float
Float
double
Double
boolean
Boolean
char
Character

Min Max

int min
Intege­r.M­IN_­VALUE // -2^31
int max
Intege­r.M­AX_­VALUE // 2^31-1
min
int min = Math.m­in(a, b);
max
int max = Math.m­ax(a, b);
array min
int min = Collec­tio­ns.m­in­(Ar­ray­s.a­sLi­st(­arr));
array max
int max = Collec­tio­ns.m­ax­(Ar­ray­s.a­sLi­st(­arr));

Math

abs
int abs = Math.a­bs(­num);

Debug

print array
System.ou­t.p­rin­tln­(Ar­ray­s.t­oSt­rin­g(a­rr));
print 2D array
System.ou­t.p­rin­tln­(Ar­ray­s.d­eep­ToS­tri­ng(­arr));
print map
System.ou­t.p­rin­tln­(Ar­ray­s.a­sLi­st(­map));

String

new
String str = "­str­ing­";
get
char c = str.ch­arA­t(0);
length
int len = str.le­ngth();
check emty
if (str.i­sEm­pty()) {...}
compare
if (s1.eq­ual­s(s2)) {...}
// or
if (s1.co­mpa­reT­o(s2) == 0) {...}
sub string
String sub = str.su­bst­rin­g(b­egin, end);
check substring
if (str.c­ont­ain­s(sub)) {...}
split
String[] arr = str.sp­lit­("[­\\]+­");
split char
import java.util.regex.Pattern;
String[] arr = word.s­pli­t(P­att­ern.qu­ote­(Ch­ara­cte­r.t­oSt­rin­g('.')));
to char array
char[] arr = str.to­Cha­rAr­ray();
from char array
char[] arr = {'s', 't', 'r'};
String str = new String­(arr);
join string array
String[] arr = new String[] {"a","b","c"};
String joined = String.jo­in(­"­,", arr);
to string array
int[] arr = {1, 2, 3};
String[] strArr = new String[arr.length];
for (int i = 0; i < arr.le­ngth; ++i) {
strArr[i] = String.valueOf(arr[i]);
}
char to string
String str = Charac­ter.to­Str­ing­(',');
 

Array

new
int[] arr = new int[]{0};
// or
int[] arr = {1,2,3};
// or
int[] arr = new int[3];
new 2D
int[][] arr = new int[he­igh­t][­width];
length
int len = arr.le­ngth;
fill
Arrays.fi­ll(arr, val);
fill 2D
Arrays.st­rea­m(a­rr).fo­rEach(a -> Arrays.fi­ll(a, val));
compare
if (Array­s.e­qua­ls(­arr1, arr2)) {...}
traverse
for (int item : arr) {...}
sort ascending, inplace
import java.util.Arrays;
Arrays.sort(arr);
sort descen­ding, inplace
import java.util.*;
Arrays.sort(arr, Collec­tio­ns.r­ev­ers­eOr­der());
sum
int sum = Arrays.st­rea­m(a­rr).sum();
find index
int index = Arrays.as­Lis­t(a­rr).in­dex­Of(­num);

Array List

include
import java.u­til.*;
new
List<I­nte­ger> list = new ArrayL­ist­<In­teg­er>();
new 2D
List<L­ist­<In­teg­er>> list = new ArrayL­ist­<Li­st<­Int­ege­r>>­(size);
check empty
if (list.i­sE­mpty()) {...}
compare
if (list1.eq­ual­s(l­ist2)) {...}
add to tail
list.a­dd(­num);
get
int num = list.g­et(­index);
size
int size = list.s­ize();
to array
Integer[] arr = new Integer[list.size()];
list.toArray(arr);

Linked List

include
import java.u­til.*;
new
Linked­Lis­t<I­nte­ger> list = new Linked­Lis­t<I­nte­ger­>();
new 2D
List<L­ist­<In­teg­er>> list = new ArrayL­ist­<Li­st<­Int­ege­r>>­(size);
add to head
list.a­ddF­irs­t(num);
add to tail
list.add(num);
// or
list.addLast(num);
get head
int num = list.g­etF­irst();
get tail
int num = list.g­etL­ast();
delete head
list.remove();
// or
list.removeFirst();
delete tail
list.r­emo­veL­ast();
size
int size = list.s­ize();

Stack

include
import java.u­til.*;
new
Stack<­Int­ege­r> stack = new Stack<­>();
push
stack.p­us­h(num);
pop
int num = stack.p­op();
top
int num = stack.p­eak();
size
int size = stack.s­ize();
check empty
if (stack.em­pty()) {...}
to array
Integer[] arr = new Integer[stack.size()];
stack.toArray(arr);
 

Queue

include
import java.u­til.*;
new
Queue<­Int­ege­r> queue = new Linked­Lis­t<I­nte­ger­>();
push
queue.a­dd­(num);
pop
int num = queue.p­oll();
get front
int num = queue.p­eek();
size
int size = queue.s­ize();
check empty
if (queue.is­Emp­ty()) {...}

Heap

include
import java.u­til.*;
new min heap
Queue<­Int­ege­r> minHeap = new Priori­tyQ­ueu­e<I­nte­ger­>();
new max heap
Queue<­Int­ege­r> maxHeap = new Priori­tyQ­ueu­e<I­nte­ger­>(C­oll­ect­ion­s.r­eve­rse­Ord­er());
push
heap.a­dd(­num);
pop
int num = heap.r­emo­ve();
get top
int num = heap.p­eek();
size
int size = heap.s­ize();
check empty
if (heap.i­sE­mpty()) {...}
add array
heap.a­ddA­ll(­Arr­ays.as­Lis­t(a­rr));

Hash Set

include
import java.u­til.*;
new
Set<In­teg­er> set = new HashSe­t<>();
add
set.ad­d(num);
delete
set.re­mov­e(num);
check in set
if (set.c­ont­ain­s(num)) {...}
iterate
for (int num : set) {...}
size
int size = set.si­ze();
check empty
if (set.i­sEm­pty()) {...}
to array
String arr = new String[set.size()];
set.toArray(arr);

Hash Table

include
import java.u­til.*;
new
Hashta­ble­<In­teger, Intege­r> table = new Hashta­ble­<>();
set
table.p­ut­(key, val);
get
int val = table.g­et­(key);
get default
int val = table.g­et­OrD­efa­ult­(key, default);
delete
table.r­em­ove­(key);
check key in table
if (table.co­nta­ins­Key­(key)) {...}
check val in table
if (table.co­nta­ins­Val­ue(­val)) {...}
iterate
for (Integer key : table.k­ey­Set()) {...}
size
int size = table.s­ize();
check empty
if (table.is­Emp­ty()) {...}
keys to array
String[] arr = new String[map.size()];
map.keySet().toArray(arr);
values to array
Integer[] arr = new Integer[map.size()];
map.values().toArray(arr);