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

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

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

问题

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

  1. 问题jdk 1.8之前需要解决的问题
  2. 我有一个字符串S="aab"所有的字符都根据它们的频率放入了HashMap现在声明一个带有比较器的PriorityQueue以便所有字符都可以按照频率降序存储在PriorityQueue但是我得到了以下错误--
  3. Map<Character, Integer> dicT = new HashMap<>();
  4. for(int i = 0; i < S.length(); i++) {
  5. dicT.put(S.charAt(i), dicT.getOrDefault(S.charAt(i), 0) + 1);
  6. }
  7. PriorityQueue<Character> maxHeap = new PriorityQueue(
  8. new Comparator<Map.Entry<Character, Integer>>() {
  9. public int compare(Map.Entry<Character, Integer> entry1,
  10. Map.Entry<Character, Integer> entry2) {
  11. return entry2.getValue().compareTo(entry1.getValue());
  12. }
  13. });
  14. maxHeap.addAll(dicT.keySet());
  15. 错误仅使用较新的jdk演示问题目标jdk版本为jdk 1.8之前---
  16. >java.lang.ClassCastException: class java.lang.Character
  17. 无法转换为类java.util.Map$Entry
  18. java.lang.Characterjava.util.Map$Entry位于加载器'bootstrap'java.base模块中在第14
  19. Solution$1.compare在第659
  20. java.base/java.util.PriorityQueue.siftUpUsingComparator在第636
  21. java.base/java.util.PriorityQueue.siftUp在第329
  22. java.base/java.util.PriorityQueue.offer在第310
  23. java.base/java.util.PriorityQueue.add在第187
  24. java.base/java.util.AbstractQueue.addAll在第22
  25. Solution.reorganizeString在第54
  26. __DriverSolution__.__helper__在第84
  27. __Driver__.main
  28. 但是如果我使用lambda表达式它就可以正常工作--
  29. PriorityQueue<Character> maxHeap = new PriorityQueue(
  30. (x, y) -> dicT.get(y) - dicT.get(x));
  31. maxHeap.addAll(dicT.keySet());
  32. 请帮我如何在不使用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--

  1. Map&lt;Character, Integer&gt; dicT = new HashMap&lt;&gt;();
  2. for(int i = 0;i &lt; S.length(); i++) {
  3. dicT.put(S.charAt(i), dicT.getOrDefault(S.charAt(i), 0) + 1);
  4. }
  5. PriorityQueue&lt;Character&gt; maxHeap = new PriorityQueue(
  6. new Comparator&lt;Map.Entry&lt;Character,Integer&gt;&gt;() {
  7. public int compare(Map.Entry&lt;Character, Integer&gt; entry1,
  8. Map.Entry&lt;Character, Integer&gt; entry2) {
  9. return entry2.getValue().compareTo(entry1.getValue());
  10. }
  11. });
  12. 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 --

  1. PriorityQueue&lt;Character&gt; maxHeap = new PriorityQueue(
  2. (x, y) -&gt; dicT.get(y) - dicT.get(x));
  3. 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>

  1. Comparator<Character> order = Comparator.comparingInt(dict::get).reversed();
  2. 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:

  1. Comparator&lt;Character&gt; order = Comparator.comparingInt(dict::get).reversed();
  2. 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 --
  1. PriorityQueue<Character> maxHeap = new PriorityQueue(new Comparator<Character>(){
  2. public int compare(Character c1, Character c2){
  3. return dicT.get(c2) - dicT.get(c1);
  4. }
  5. });
  1. 使用 Lambda --
  1. PriorityQueue<Character> maxHeap = new PriorityQueue((x, y) -> dicT.get(y) - dicT.get(x));
  1. 使用方法引用--
  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 --

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

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

    1. 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:

确定