-1

In Java, initialization of a hash map with a min heap as the value:

Map<Integer, PriorityQueue<Integer>> map = new HashMap<>();

Initialization of a max heap using lambda can be like:

PriorityQueue<Integer> pq = new PriorityQueue<>((a, b) -> b - a);

However, how to initialize a hash map with a max heap using lambda?

This definitly doesn't work.

Map<Integer, PriorityQueue<Integer>((a, b) -> b - a)> map = new HashMap<>();
mmast
  • 21
  • 1
  • 4
    Your question doesn't make sense. You can't add values to a map without keys. – shmosel Jul 10 '23 at 07:09
  • @shmosel but in the background Java has allocated a default capacity of 16 HashMap entries. @mmast can do `Map> map = new HashMap<>();` which in the background would allocate space for 16 HashMap entries and 11 PriorityQueue entries for each of those HashMap entries but they are trying to override the default behaviour using lambda functions. – Puddler Jul 10 '23 at 07:26
  • 1
    @Puddler since Java 8 constructing a new `HashMap` instance does **not** create the `table` - this is only done on first use (i.e. first `put()` operation). But even if it did allocate the `table` field it would only allocate an array full of `null` values. The `HashMap` only ever allocates its internal objects, it neither allocates the keys nor the values. – Thomas Kläger Jul 10 '23 at 07:36
  • @ThomasKläger and that's why I only commented and didn't give an answer since my Java is clearly very rusty., thanks for the correction. – Puddler Jul 10 '23 at 07:54
  • Your terminology is somewhat deviated here. In your examples you are just instantiating objects which utilize a dynamic heap allocation, in terms of memory. Are you referencing another coding language? – Reilas Jul 10 '23 at 11:48

1 Answers1

4

This does not "initialise of a hash map with a min heap as the value".

Map<Integer, PriorityQueue<Integer>> map = new HashMap<>();

This creates a HashMap, whose keys are integers, and whose values are PriorityQueue<Integer>s.

A PriorityQueue<Integer> can act like a max heap or a min heap, depending on the Comparator<Integer> that you pass into its constructor. This means that map above can contain both max heaps and min heaps.

map.put(1, new PriorityQueue<>(Comparator.naturalOrder())); // a min heap is associated with the key 1
map.put(2, new PriorityQueue<>(Comparator.reverseOrder())); // a max heap is associated with the key 2

Note that you should not use subtraction (i.e. (a, b) -> b - a) to compare integers. See this post for more info.

With that cleared out of the way, we can make a HashMap that only contains min heaps, or only contains max heaps, by declaring our own MinHeap and MaxHeap types.

MinHeap and MaxHeap can inherit from PriorityQueue, and calling PriorityQueue's constructor with an appropriate Comparator argument.

class MinHeap<T extends Comparable<T>> extends PriorityQueue<T> {
    public MinHeap() {
        super(Comparator.naturalOrder());
    }
}

class MaxHeap<T extends Comparable<T>> extends PriorityQueue<T> {
    public MaxHeap() {
        super(Comparator.reverseOrder());
    }
}

Now, a HashMap<Integer, MinHeap<Integer>> only contains min heaps as values, and a HashMap<Integer, MaxHeap<Integer>> only max heaps as values.

Map<Integer, MinHeap<Integer>> minHeapMap = new HashMap<>();
Map<Integer, MaxHeap<Integer>> maxHeapMap = new HashMap<>();
Sweeper
  • 213,210
  • 22
  • 193
  • 313