英文:
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<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());
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<Character> maxHeap = new PriorityQueue(
(x, y) -> 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<Character>
which uses a Comparator<Character>
to define priority. But you are providing a Comparator<Map.Entry<...>>
hence the error. Give it a Comparator<Character>
instead:
Comparator<Character> order = Comparator.comparingInt(dict::get).reversed();
PriorityQueue<Character> maxHeap = new PriorityQueue<>(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
- 没有 Lambda --
PriorityQueue<Character> maxHeap = new PriorityQueue(new Comparator<Character>(){
public int compare(Character c1, Character c2){
return dicT.get(c2) - dicT.get(c1);
}
});
- 使用 Lambda --
PriorityQueue<Character> maxHeap = new PriorityQueue((x, y) -> dicT.get(y) - dicT.get(x));
- 使用方法引用--
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.
-
Without Lambda --
PriorityQueue<Character> maxHeap= new PriorityQueue(new Comparator<Character>(){ public int compare(Character c1, Character c2){ return dicT.get(c2) - dicT.get(c1); } });
-
With Lambda --
PriorityQueue<Character> maxHeap =new PriorityQueue((x,y)->dicT.get(y)-dicT.get(x));
-
With Method Reference--
PriorityQueue<Character> maxHeap = new PriorityQueue(Comparator.comparingInt(dicT::get).reversed());
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论