Show Menu
Cheatography

Java Coding Interview Cheat Sheet Cheat Sheet by

Main Java types, collections, and methods that are used in programming during white board coding interview

String

Declar­ation
String s = "­​​Hello world";
String s = new String­("Hello world!­​​");
String s = new String­​​(­c​h­​a​­rA​­​rray);
Methods
char c = s.char​​At​(​i​ndex);
String subs = s.subs​​­tr​­i​n​g(​​sta­​rtIdx, endIdx);
bool hasRed = s.cont​​­ai​n​s​("r­​​e­d");
String newStr = s.repl​​ac​e​('a', 'b');
String lowerC­​​a­seStr = s.toLo​​­we​­r​C​ase();
String upperC­​​a­seStr = s.toUp​​­pe​­r​C​ase();
Convert
String s = String.va​​lu­​e​O​f(​​num­​​ber);
String[ ] arrayOfStr = s.spli​​t(​"​­,​");
char[ ] charArray = s.toCh​​­ar​­A​r​ray();
int number = Intege​​r.p​​a​r­s​​­eIn​​t(​"​­1​2­​3");

ArrayList

Declar­ation
ArrayL­ist­<St­rin­g> cars = new ArrayL­ist­<St­rin­g>();
Methods
cars.add("Vo­lvo­");
cars.get(0);
cars.set(0, "­Ope­l");
cars.remove(0);
cars.clear(); //remove all
cars.size();
Sort & Iterate
Collec­tio­ns.s­or­t(c­ars);
for ( int i = 0; i < cars.s­ize(); i++ ) { }
for ( String car : cars ) { }
Iterator it = cars.iterator();
while ( it.has­Next() ) { it.next(); };

Array

Declar­ation
int[ ] array= {1, 2, 3, 4, 5, 6};
int[ ] array = new int[10];
int[ ][ ] twoDArray = new int[n][m];
Methods
int n = array.l­ength;
Sort & Iterate
Arrays.so­rt(­array);
Arrays.so­rt(­array, (a,b) -> Intege­r.c­omp­are­(a,b))

Character

Declar­ation & Methods
char myGrade = 'B';
(char) (1 + 'a') -> 'b'
Convert to other types
int a = ch - '0';
char myVar1 = 65; // myVar1 = 'A'
String s = String.valueOf(ch);

LinkedList

Declar­ation
Linked­Lis­t<S­tri­ng> cars = new Linked­Lis­t<S­tri­ng>();
Method
cars.add("Vo­lvo­");
cars.addFirst("Ma­zda­");
cars.addLast("Fo­rd");
cars.remove­First();
cars.removeLast();
cars.getFirst();
cars.getLast();
cars.size();
Iterate
for ( int i = 0; i < cars.s­ize(); i++ ) { }
for ( String car: cars ) { }

HashMap

Declar­ation
HashMa­p<S­tring, Intege­r> map = new HashMa­p<>();
Methods
map.put("ke­y1", 10);
map.contai­nsKey("ke­y2")
map.contai­nsValue(2));
map.get("ke­y1");
map.remove("ke­y1");
map.clear(); //remove all items
map.put("ke­y1", 5); //update key1
map.size();
Iterate
for ( String key : map.ke­ySet() ) { }
for ( Integer i : map.va­lues() ) { }
 

Queue (Linke­dList implem­ent­ation)

Declar­ation
Queue<­Str­ing> queue = new Linked­Lis­t<>();
Methods
queue.add("ap­ple­");
queue.remove();
queue.peek(); //get first
queue.size();

LinkedList vs Priori­tyQueue

LinkedList preserves the insertion order, Priori­tyQueue does not. The elements of the priority queue are ordered according to their natural ordering, or by a Comparator provided at queue constr­uction time.

Stack

Declar­ation
Stack<­Int­ege­r> stack = new Stack<­Int­ege­r>();
stack.push(1);
stack.peek();
stack.pop();
stack.isEmpty()
int index = stack.search(2);
Iterate
for ( Integer item : stack ) { }
Iterator it = stack.iterator();
while ( it.has­Next() ) { it.next(); };

HashSet

Declar­ation
HashSe­t<S­tri­ng> cars = new HashSe­t<S­tri­ng>();
Methods
cars.add("Vo­lvo­");
cars.contains("Ma­zda­");
cars.remove("Vo­lvo­");
cars.clear();
cars.size();
Iterate
for ( String car : cars ) { }
Iterat­or<­Int­ege­r> it = cars.iterator();
while ( it.has­Next() ) { it.nex­t();};

Queue (Prior­ity­Queue implem­ent­ation)

Declar­ation
Queue<­Int­ege­r> pQueue = new Priori­tyQ­ueu­e<I­nte­ger­>();
Methods
pQueue.add(10);
pQueue.peek(); //return top element
pQueue.poll(); //return and remove
pQueue.remove(2); //remove first 2
Iterate
Iterator it = pQueue.iterator();
while ( it.has­Next() ) { it.next(); };
Compar­artor

Bitwise Operators

OR
0101 | 0111 = 0111
AND
0101 & 0111 = 0101
XOR
0101 ^ 0111 = 0010
NOT
~ 0101 = 1010
Left shift
000110 << 1 = 001100
Right shift
10110101 >> 1 = 11011010
Unsigned Right Shift
10110101 >>> 1 = 01011010

Other

Min integer
Intege­r.M­IN_­VALUE
Max integer
Intege­r.M­AX_­VALUE
 

Comments

No comments yet. Add yours below!

Add a Comment

Your Comment

Please enter your name.

    Please enter your email address

      Please enter your Comment.

          Related Cheat Sheets

          Selenium WebDriver Cheat Sheet Cheat Sheet
          Cypressio Cheat Sheet
          ISTQB Test Automation Engineering Cheat Sheet