java_primer
Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| java_primer [2020/08/18 16:04] – jrseti | java_primer [2020/11/19 17:19] (current) – [LinkedHashMap] jrseti | ||
|---|---|---|---|
| Line 17: | Line 17: | ||
| * But, **keys are ordered**! | * But, **keys are ordered**! | ||
| * No null keys | * No null keys | ||
| + | * keySet()and values() return in ascending order | ||
| ====LinkedHashMap==== | ====LinkedHashMap==== | ||
| + | |||
| + | **An OrderedDict!** | ||
| Same as hashMap but preserves **insertion order**. O(1) insertion and lookup. | Same as hashMap but preserves **insertion order**. O(1) insertion and lookup. | ||
| Line 28: | Line 31: | ||
| map.getOrDefault(key, | map.getOrDefault(key, | ||
| + | ====HashSet==== | ||
| + | |||
| + | < | ||
| + | HashSet< | ||
| + | |||
| + | h.add" | ||
| + | h.add(" | ||
| + | Iterator< | ||
| + | while (i.hasNext()) { | ||
| + | | ||
| + | }</ | ||
| + | |||
| + | contains(), add(), isEmpty(), size(), clone() | ||
| ====Map Iteration===== | ====Map Iteration===== | ||
| Line 90: | Line 106: | ||
| </ | </ | ||
| + | =====Stacks===== | ||
| + | |||
| + | < | ||
| + | Stack< | ||
| + | </ | ||
| + | |||
| + | pop(), peek(), push(), empty(), search() | ||
| + | |||
| + | =====Queue===== | ||
| + | |||
| + | < | ||
| + | import java.util.LinkedList; | ||
| + | import java.util.Queue; | ||
| + | Queue< | ||
| + | </ | ||
| + | |||
| + | =====PriorityQueue===== | ||
| + | |||
| + | https:// | ||
| + | |||
| + | poll(), remove(), add() | ||
| + | |||
| + | |||
| + | |||
| + | < | ||
| + | class The_Comparator implements Comparator< | ||
| + | public int compare(String str1, String str2) | ||
| + | { | ||
| + | String first_Str; | ||
| + | String second_Str; | ||
| + | first_Str = str1; | ||
| + | second_Str = str2; | ||
| + | return second_Str.compareTo(first_Str); | ||
| + | } | ||
| + | } | ||
| + | |||
| + | PriorityQueue< | ||
| + | | ||
| + | </ | ||
| =====DFS===== | =====DFS===== | ||
| Line 101: | Line 156: | ||
| while(cur != null || | while(cur != null || | ||
| </ | </ | ||
| + | |||
| + | |||
| + | =====NOTES===== | ||
| + | |||
| + | * Vector is the same as ArrayList, but Vector is synchronized. Removing from Vector or ArrayList in O(n). | ||
| + | * LinkedList has O(1) remove, LinkedList is a double linked list. ArrayList is single. | ||
| + | * LinkedList is better with add(), remove. | ||
| + | * ArrayList better with get(), set(). | ||
| + | |||
| + | * Char to digit: s.charAt(num2Idx) - ' | ||
| + | |||
| + | |||
| + | =====TRICKS===== | ||
| + | |||
| + | ====Log and division==== | ||
| + | |||
| + | * a / b = Math.exp(Math.log(a) - Math.log(b)) | ||
| + | |||
| + | ====Random==== | ||
| + | |||
| + | < | ||
| + | //Random number from 0 to 5 | ||
| + | Random rand = new Random(); | ||
| + | int r = rand.nextInt(6); | ||
| + | </ | ||
| + | |||
| + | or | ||
| + | |||
| + | < | ||
| + | //Number between 0.0 and 1.0; | ||
| + | Math.random(); | ||
| + | </ | ||
| + | |||
java_primer.1597766655.txt.gz · Last modified: 2020/08/18 16:04 by jrseti