Download CHEAT SHEET and more Summaries Computer Programming in PDF only on Docsity! CHEAT SHEET Constructing Various Collections List<Integer> list = new ArrayList<Integer>(); Queue<Double> queue = new LinkedList<Double>(); Stack<String> stack = new Stack<String>(); Set<String> words = new HashSet<String>(); Map<String, Integer> counts = new TreeMap<String, Integer>(); Methods Found in ALL collections (Lists, Stacks, Queues, Sets, Maps) clear() removes all elements of the collection equals(collection) returns true if the given other collection contains the same elements isEmpty() returns true if the collection has no elements size() returns the number of elements in the collection toArray() returns an array of the elements in this collection toString() returns a string representation such as "[10, -2, 43]" Methods Found in both Lists and Sets (ArrayList, LinkedList, HashSet, TreeSet) add(value) adds value to collection (appends at end of list) contains(value) returns true if the given value is found somewhere in this collection iterator() returns an Iterator object to traverse the collection's elements remove(value) finds and removes the given value from this collection removeAll(collection) removes any elements found in the given collection from this one retainAll(collection) removes any elements not found in the given collection from this one List<E> Methods (10.1) add(index, value) inserts given value at given index, shifting subsequent values right indexOf(value) returns first index where given value is found in list (-1 if not found) get(index) returns the value at given index lastIndexOf(value) returns last index where given value is found in list (-1 if not found) remove(index) removes/returns value at given index, shifting subsequent values left set(index, value) replaces value at given index with given value subList(from, to) returns sub-portion at indexes from (inclusive) and to (exclusive) 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