Cheat Sheet: Constructing Various Collections
Cheat Sheet: Constructing Various Collections
Stack<E> Methods
peek() returns the top value from the stack without removing it
pop() removes the top value from the stack and returns it;
peek/pop throw an EmptyStackException if the stack is empty
push(value) places the given value on top of the stack
Queue<E> Methods
add(value) places the given value at the back of the queue
peek() returns the front value from the queue without removing it;
returns null if the queue is empty
remove() removes the value from the front of the queue and returns it;
throws a NoSuchElementException if the queue is empty
CHEAT SHEET
Map<K, V> Methods (11.3)
containsKey(key) true if the map contains a mapping for the given key
get(key) the value mapped to the given key (null if none)
keySet() returns a Set of all keys in the map
put(key, value) adds a mapping from the given key to the given value
putAll(map) adds all key/value pairs from the given map to this map
remove(key) removes any existing mapping for the given key
toString() returns a string such as "{a=90, d=60, c=70}"
values() returns a Collection of all values in the map
String Methods (3.3, 4.4)
charAt(i) the character in this String at a given index
contains(str) true if this String contains the other's characters inside it
endsWith(str) true if this String ends with the other's characters
equals(str) true if this String is the same as str
equalsIgnoreCase(str) true if this String is the same as str, ignoring capitalization
indexOf(str) first index in this String where given String begins (-1 if not found)
lastIndexOf(str) last index in this String where given String begins (-1 if not found)
length() number of characters in this String
startsWith(str) true if this String begins with the other's characters
substring(i, j) characters in this String from index i (inclusive) to j (exclusive)
toLowerCase(), toUpperCase() a new String with all lowercase or uppercase letters
Random Methods (5.1)
nextBoolean() random true/false result
nextDouble() random real number between 0.0 and 1.0
nextInt() random integer
nextInt(max) random integer between 0 and max