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:23] – [Queue] 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 92: Line 108:
 =====Stacks===== =====Stacks=====
  
-<code>import java.util.*</code>+<code>import java.util.* 
 +Stack<Integer> stack = new Stack<>();; 
 +</code>
  
 pop(), peek(), push(), empty(), search() pop(), peek(), push(), empty(), search()
Line 108: Line 126:
 https://www.geeksforgeeks.org/priority-queue-class-in-java-2/ 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===== =====DFS=====
  
Line 124: Line 160:
 =====NOTES===== =====NOTES=====
  
-  * Vector is the same as ArrayList, but Vector is synchronized.+  * 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.1597767834.txt.gz · Last modified: 2020/08/18 16:23 by jrseti