HashMap && heap
- Type argument cannot be primitive type
HashMap<Integer,boolean> hashMap = new HashMap<Integer, boolean>();
it should use Boolean instead of boolean;
HashMap<Integer,Boolean> hashMap = new HashMap<Integer, Boolean>();
hashMap.setEntry 是一个包装了每一个hashmap key-value的一个entry - languages.entrySet() - returns the set view of all the entries.
- languages.keySet() - returns the set view of all the keys.
- languages.values() - returns the set view of all the values.
for里面的内容左边是类型 这个函数的返回值类型为 Map.Entry<Integer,Boolean>
SO:this method return a Set view of the mapping contained in the map(hashmap)
Syntax:Set<Map.Entry<K,V>> entrySet()
``
HashMap<Integer, Boolean> hashMap = new HashMap<Integer, Boolean>();
hashMap.put(1,hashMap.containsValue(2));
hashMap.put(4,hashMap.containsValue(2));
for(Map.Entry<Integer,Boolean> entry : hashMap.entrySet()){
System.out.println(entry.getKey()+":"+entry.getValue());
}
``
map can have linkedhashmap this datastructure can solve the LRU question:
THIS is a usage of get the first element of LinkedHashMap:
- 1: transfor LinkedHashMap to set() (encapsulate the key-value) map.entrySet()
- 2:get the iterator method (I think just to have a pointor)
- 3:get the key=value (1=2). next()
- 4:getKey() or getValue()
``
Map<Integer,Integer> map = new LinkedHashMap<>();
map.put(1,2);
map.put(3,4);
map.put(5,6);
System.out.println(map.entrySet().iterator().next().getKey());
System.out.println(map.entrySet().iterator().next().getValue());
``
heap
it is a Collection framework's iterface "set"
heap like a queue just have the priority
like min heap and max heap
so if you declare max heap like :
``
PriorityQueue<Integer> maxPQ = new PriorityQueue<>(Collections.reverseOrder());
maxPQ.add(1);
maxPQ.add(2);
maxPQ.add(0);
for(Integer i : maxPQ){
System.out.println(i);
}
``
and you can also declare min heap:
PriorityQueue<Integer> maxPQ = new PriorityQueue<>();
- Ascending order priority queue: In ascending order priority queue, a lower priority number is given as a higher priority in a priority. For example, we take the numbers from 1 to 5 arranged in an ascending order like 1,2,3,4,5; therefore, the smallest number, i.e., 1 is given as the highest priority in a priority queue.
Priority Queue.
- Descending order priority queue: In descending order priority queue, a higher priority number is given as a higher priority in a priority. For example, we take the numbers from 1 to 5 arranged in descending order like 5, 4, 3, 2, 1; therefore, the largest number, i.e., 5 is given as the highest priority in a priority queue.
Priority Queue