# 01-Collections.md (Part 2)

# Question 2 - Reverse a HashMap

## Problem
Reverse Map<K,V> into Map<V,K>.

### Solution 1 - Basic

```java
public static <K,V> Map<V,K> reverse(Map<K,V> input){

    Map<V,K> result = new HashMap<>();

    for(var entry : input.entrySet()){
        result.put(entry.getValue(), entry.getKey());
    }

    return result;
}
```

Time: O(n)

### Solution 2 - Streams

```java
public static <K,V> Map<V,K> reverse(Map<K,V> input){

    return input.entrySet()
            .stream()
            .collect(Collectors.toMap(
                    Map.Entry::getValue,
                    Map.Entry::getKey));
}
```

Follow-up:
- What if duplicate values exist?
- Use Map<V,List<K>>

---

# Question 3 - Frequency Counter

```java
Map<String,Integer> freq = new HashMap<>();

for(String word : words){
    freq.merge(word,1,Integer::sum);
}
```

Streams

```java
Map<String,Long> result =
        words.stream()
                .collect(Collectors.groupingBy(
                        Function.identity(),
                        Collectors.counting()));
```

---

# Question 4 - Merge Two Maps

```java
Map<String,Integer> map = new HashMap<>(a);

b.forEach((k,v)->
        map.merge(k,v,Integer::sum));
```

---

# Question 5 - Top K Largest Elements

```java
PriorityQueue<Integer> heap =
        new PriorityQueue<>();

for(int n : nums){

    heap.offer(n);

    if(heap.size()>k){
        heap.poll();
    }
}
```

Complexity O(n log k)

---

# Question 6 - LRU Cache

```java
class LRU<K,V> extends LinkedHashMap<K,V>{

    private final int capacity;

    LRU(int capacity){
        super(16,0.75f,true);
        this.capacity = capacity;
    }

    @Override
    protected boolean removeEldestEntry(
            Map.Entry<K,V> eldest){

        return size()>capacity;
    }
}
```

---

# Question 7 - PriorityQueue

```java
PriorityQueue<Integer> pq =
        new PriorityQueue<>();

pq.offer(30);
pq.offer(10);
pq.offer(20);

while(!pq.isEmpty()){
    System.out.println(pq.poll());
}
```

Output

10
20
30

---

# Question 8 - TreeSet

```java
TreeSet<Integer> set =
        new TreeSet<>();

set.add(5);
set.add(1);
set.add(3);

System.out.println(set);
```

Output

[1,3,5]

Complexity O(log n)

---

# Question 9 - ConcurrentHashMap

```java
ConcurrentHashMap<String,Integer> map =
        new ConcurrentHashMap<>();

map.compute("A",
        (k,v)->v==null?1:v+1);
```

Why not HashMap?
- Thread-safe
- High concurrency
- No global synchronization

---

# Question 10 - CopyOnWriteArrayList

```java
CopyOnWriteArrayList<String> list =
        new CopyOnWriteArrayList<>();

list.add("Java");

for(String s:list){

    list.add("Redis");

}
```

Safe iteration.

Used for read-heavy workloads.

---

More questions to follow:
11. WeakHashMap
12. IdentityHashMap
13. EnumMap
14. EnumSet
15. ArrayDeque
16. BlockingQueue
17. Comparable
18. Comparator
19. Binary Search
20. Collections.sort()
21. Fail-fast iterator
22. Immutable collections
23. Queue vs Deque
24. HashSet internals
25. LinkedHashSet
26. UnmodifiableList
27. SynchronizedList
28. Producer Consumer
29. Interview MCQs
30. JUnit Tests
