优先队列,按照映射值排序,无需使用 Lambda(较早版本的 Java)

huangapple go评论80阅读模式
英文:

PriorityQueue Sort By Map Value with out Lambda (older version of Java)

问题

以下是您提供的代码的翻译部分:

问题在jdk 1.8之前需要解决的问题
我有一个字符串S="aab"所有的字符都根据它们的频率放入了HashMap中现在声明一个带有比较器的PriorityQueue以便所有字符都可以按照频率降序存储在PriorityQueue中但是我得到了以下错误--

Map<Character, Integer> dicT = new HashMap<>();        
for(int i = 0; i < S.length(); i++) {           
   dicT.put(S.charAt(i), dicT.getOrDefault(S.charAt(i), 0) + 1);
}

PriorityQueue<Character> maxHeap = new PriorityQueue(
    new Comparator<Map.Entry<Character, Integer>>() {
        public int compare(Map.Entry<Character, Integer> entry1, 
                           Map.Entry<Character, Integer> entry2) {
            return entry2.getValue().compareTo(entry1.getValue());
        }
    });
    
maxHeap.addAll(dicT.keySet());

错误仅使用较新的jdk演示问题目标jdk版本为jdk 1.8之前---
        
>java.lang.ClassCastException: class java.lang.Character  
   无法转换为类java.util.Map$Entry  
   java.lang.Character和java.util.Map$Entry位于加载器'bootstrap'的java.base模块中在第14行  
   Solution$1.compare在第659行  
   java.base/java.util.PriorityQueue.siftUpUsingComparator在第636行  
   java.base/java.util.PriorityQueue.siftUp在第329行  
   java.base/java.util.PriorityQueue.offer在第310行  
   java.base/java.util.PriorityQueue.add在第187行  
   java.base/java.util.AbstractQueue.addAll在第22行  
   Solution.reorganizeString在第54行  
   __DriverSolution__.__helper__在第84行  
   __Driver__.main


但是如果我使用lambda表达式它就可以正常工作--

PriorityQueue<Character> maxHeap = new PriorityQueue(
    (x, y) -> dicT.get(y) - dicT.get(x));
maxHeap.addAll(dicT.keySet());

请帮我如何在不使用lambda的情况下编写这段代码块
英文:

Following problem require solution before jdk 1.8.
I have a string S="aab". All the character is put into HashMap based on their frequency. Now declaring a PriorityQueue with Comparator. So that all characters can be stored in the PriorityQueue in descending order base on the frequency. But I am getting the following error--

    Map&lt;Character, Integer&gt; dicT = new HashMap&lt;&gt;();        
    for(int i = 0;i &lt; S.length(); i++) {           
       dicT.put(S.charAt(i), dicT.getOrDefault(S.charAt(i), 0) + 1);
    }

    PriorityQueue&lt;Character&gt; maxHeap = new PriorityQueue(
        new Comparator&lt;Map.Entry&lt;Character,Integer&gt;&gt;() {
            public int compare(Map.Entry&lt;Character, Integer&gt; entry1, 
                               Map.Entry&lt;Character, Integer&gt; entry2) {
                return entry2.getValue().compareTo(entry1.getValue());
            }
        });
        
    maxHeap.addAll(dicT.keySet());

Error(Using newer jdk to demonstrate problem only, the target jdk version is before jdk 1.8) ---

>java.lang.ClassCastException: class java.lang.Character
cannot be cast to class java.util.Map$Entry
(java.lang.Character and java.util.Map$Entry are in module
java.base of loader 'bootstrap') at line 14,
Solution$1.compare at line 659,
java.base/java.util.PriorityQueue.siftUpUsingComparator at line 636,
java.base/java.util.PriorityQueue.siftUp at line 329,
java.base/java.util.PriorityQueue.offer at line 310,
java.base/java.util.PriorityQueue.add at line 187,
java.base/java.util.AbstractQueue.addAll at line 22,
Solution.reorganizeString at line 54,
DriverSolution.helper at line 84,
Driver.main

But if I use lambda then it's working fine --

PriorityQueue&lt;Character&gt; maxHeap = new PriorityQueue(
        (x, y) -&gt; dicT.get(y) - dicT.get(x));
    maxHeap.addAll(dicT.keySet());

Please help me how can I write the block of code with out lambda?

答案1

得分: 3

你正在声明一个使用Comparator<Character>来定义优先级的PriorityQueue<Character>,但你提供了一个Comparator<Map.Entry<...>>,因此出现了错误。改为使用Comparator<Character>

Comparator<Character> order = Comparator.comparingInt(dict::get).reversed();
PriorityQueue<Character> maxHeap = new PriorityQueue<>(order);

你也可以使用匿名类来替代Comparator的静态方法,尽管我实在看不出你为什么要这么做。

英文:

You are declaring a PriorityQueue&lt;Character&gt; which uses a Comparator&lt;Character&gt; to define priority. But you are providing a Comparator&lt;Map.Entry&lt;...&gt;&gt; hence the error. Give it a Comparator&lt;Character&gt; instead:

Comparator&lt;Character&gt; order = Comparator.comparingInt(dict::get).reversed();
PriorityQueue&lt;Character&gt; maxHeap = new PriorityQueue&lt;&gt;(order);

You can use an anonymous class instead of the Comparator static methods, though I really can't see any reason why you would.

答案2

得分: 0

  1. 没有 Lambda --
PriorityQueue<Character> maxHeap = new PriorityQueue(new Comparator<Character>(){           
    public int compare(Character c1, Character c2){
        return dicT.get(c2) - dicT.get(c1);
    }            
});
  1. 使用 Lambda --
PriorityQueue<Character> maxHeap = new PriorityQueue((x, y) -> dicT.get(y) - dicT.get(x));
  1. 使用方法引用--
PriorityQueue<Character> maxHeap = new PriorityQueue(Comparator.comparingInt(dicT::get).reversed());
英文:

I really appreciate for you help. This is not about my IDE. My work place does not allow us to use all the Oracle libraries. And project is too old. Using older version version of jdk. So we are not able to use any lambda or method reference. But I am able to figure out the solution.

  1. Without Lambda --

    PriorityQueue&lt;Character&gt; maxHeap= new PriorityQueue(new Comparator&lt;Character&gt;(){           
    public int compare(Character c1, Character c2){
    return dicT.get(c2) - dicT.get(c1);
    }            
    });
    
  2. With Lambda --

    PriorityQueue&lt;Character&gt; maxHeap =new PriorityQueue((x,y)-&gt;dicT.get(y)-dicT.get(x));
    
  3. With Method Reference--

    PriorityQueue&lt;Character&gt; maxHeap = new PriorityQueue(Comparator.comparingInt(dicT::get).reversed());
    

huangapple
  • 本文由 发表于 2020年10月6日 11:52:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/64219111.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定