Common implementation for queue
Queue<Integer> q = new PriorityQueue<>(); Queue<Integer> linkedList = new LinkedList<>();
Methods to add and remove items from the queue
Implementation for a stack
Stack<Integer> stack = new Stack<>();</Integer>
Methods:
- from Stack class
- push/pop
- peek - look w/o popping
Whats a TreeMap
Implement a comparator
Map<Integer, List<Set<Integer>>> profitToDayIndexMap = new TreeMap<>(</Integer>
// descending order
new Comparator<Integer>() {</Integer>
@Override
public int compare(Integer o1, Integer o2) {
return o2 > o1 ? 1 : o2 == o1 ? 0 : -1;
}
} );Think this way. You are given two items. Return 1 for the item you want the comparator to return first when it’s greater than the other.
For descending order, return 1 when b > a.
Convert array to list and vise verse
// init array
Integer[] array = new Integer[]{1, 2, 3};
// convert array to list
List<Integer> list = Arrays.asList(array);
// convert list to array
array = list.toArray(new Integer[0]);What Set implementation to use to
1. maintain the insertion order
2. maintain the natural ordering of the elements
Whats the limits to recursion in java
How to sort an array in place
int[] nums = new int[]{2, 3, 5, 1};
Arrays.sort(nums);
How to sort an array w/o modifying the array
int[] nums2 = new int[]{2, 3, 5, 1};
int[] sorted = Arrays.stream(nums2).sorted().toArray();
How to sort a list
List<Integer> nums = Arrays.asList(1, 4, 3, 2);
List<Integer> sorted = nums.stream().sorted().collect(Collectors.toList());</Integer></Integer>
Name some common methods on the Arrays class
These modifies the array in place
- Arrays.sort(array)
- Arrays.binarySearch(sortedArray, value)
- return position of the key or -1 if not found
- Arrays.fill(array, valueToFill)
These allow the array to be viewed and operated on as a list
- Arrays.stream(array)…. // more manipulation
- Arrays.asList(array);
- Arrays.asList(1,2,3);
int[] array = new int[]{1,2,3};
// static method for search
Arrays.sort(nums);
int pos = Arrays.binarySearch(nums, 1);
// static method to fill
Arrays.fill(nums, 1);
// static methods for sort
Arrays.sort(array); // sorts in place
// allow arrays to be viewed as lists
Arrays.stream(array)... // view array as a list
Arrays.asList(1,2,3); // inits a list with value
Arrays.asList(array); // inits a list with an array
What are some highlights of the Arrays class
How to convert list <-> array with primitive types
// with primitive types
int[] primitiveArray = new int[]{1, 2, 3};
// convert array to list (where array is with primitive type)
List<Integer> list2 = Arrays.stream(primitiveArray).boxed().collect(Collectors.toList());
// convert list to array
primitiveArray = list2.stream().mapToInt(Integer::intValue).toArray();
primitiveArray = list2.stream().mapToInt(i -> i).toArray();
Collection, Collections, Collector, Collectors, etc
What are the high level differences
For Collection, Collections, Collector, Collectors, etc
What’s “Collectors” (with s)
Name some common operations and how to call it.
Collectors (class)
For Collection, Collections, Collector, Collectors, etc
What’s “Collector” (w/o s)
Collector (interface)
For Collection, Collections, Collector, Collectors, etc
What’s “Collections” (w/ s)
Collections (class)
What are some useful utility methods on the Collections class
List<Integer> list = Arrays.asList(4, 1, 2, 3);
Collections.sort(list);
Collections.fill(list, 3);For Collection, Collections, Collector, Collectors, etc
What’s “Collection” (w/o s)
Collection (interface)
- interface for List, Set, Queue, Map etc
What’re some differences between Deque vs Stack
Deque vs Stack
- Deque is an interfaces implements Queue
- Deque supports insertion/removal from both ends, deque means double ended queue
- Implementing class includes LinkedList
- Stack is a class implementing Vector
- Stack is synchronized (thread-safe) while Dequeue is not
- Dequeue is faster because it’s not synchronized
Common methods for Deque
Common methods for Deque
- addFirst/addLast, removeFirst/removeLast
- offerFirst/offerLast, pollFirst/pollLast
- peakFirst/peakLast, getFirst/getLast
Stream interface and Streams class
Differences/similarities in methods between Arrays vs Collections