User Tools

Site Tools


java_primer

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
java_primer [2020/08/18 16:02] jrsetijava_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, default_value); map.getOrDefault(key, default_value);
  
 +====HashSet====
 +
 +<code>
 +HashSet<String> h = new HashSet<String>(); 
 +
 +h.add"(a");
 +h.add("b");
 +Iterator<String> i = h.iterator(); 
 +while (i.hasNext()) {
 +   System.out.println(i.next()); 
 +}</code>
 +
 +contains(), add(), isEmpty(), size(), clone()
 ====Map Iteration===== ====Map Iteration=====
  
Line 89: Line 105:
      }      }
 </code> </code>
 +
 +=====Stacks=====
 +
 +<code>import java.util.*
 +Stack<Integer> stack = new Stack<>();;
 +</code>
 +
 +pop(), peek(), push(), empty(), search()
 +
 +=====Queue=====
 +
 +<code>
 +import java.util.LinkedList; 
 +import java.util.Queue;
 +Queue<Integer> q = new LinkedList<>();
 +</code>
 +
 +=====PriorityQueue=====
 +
 +https://www.geeksforgeeks.org/priority-queue-class-in-java-2/
 +
 +poll(), remove(), add()
 +
 +
 +
 +<code>
 +class The_Comparator implements Comparator<String>
 +    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<String> queue = new PriorityQueue<String>(new The_Comparator()); 
 +        
 +</code>
 +=====DFS=====
 +
 +====Traversing====
 +
 +<code>
 +TreeNode cur = root;
 +int min = Integer.MAX_VALUE;
 +int max = Integer.MIN_VALUE;
 +
 +while(cur != null ||
 +</code>
 +
 +
 +=====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) - '0'
 +
 +
 +=====TRICKS=====
 +
 +====Log and division====
 +
 +  * a / b = Math.exp(Math.log(a) - Math.log(b))
 +
 +====Random====
 +
 +<code>
 +//Random number from 0 to 5
 +Random rand = new Random(); 
 +int r = rand.nextInt(6); 
 +</code>
 +
 +or
 +
 +<code>
 +//Number between 0.0 and 1.0;
 +Math.random(); 
 +</code>
 +
java_primer.1597766532.txt.gz · Last modified: 2020/08/18 16:02 by jrseti