这是一段没有写完的哈夫曼树压缩解压缩文件的代码,但是其中的PriorityBlockingQueue数据结构中的comparator接口出问题了。。。
package Controller.Huffman_tree;
import javax.swing.tree.TreeNode;
import java.util.Comparator;
import java.util.Hashtable;
import java.util.Map;
import java.util.Queue;
import java.util.concurrent.PriorityBlockingQueue;
public class buildTree {
public static class TreeNode {
int weight;
Byte data;
TreeNode lClild;
TreeNode rChild;
public void setWeight(int weight) {
this.weight = weight;
}
public void setData(Byte data) {
this.data = data;
}
public TreeNode() {
}
public TreeNode(int weight, Byte data) {
this.weight = weight;
this.data = data;
}
@Override
public String toString() {
return "TreeNode{" +
"weight=" + weight +
", data=" + data.toString() +
", lClild=" + lClild +
", rChild=" + rChild +
'}';
}
}
public static Queue<TreeNode> pq;
public static Byte [] bytes;
public static void main(){
}
public static void creatingLeafs(){
Map<Byte,Integer> map=new Hashtable<>();
pq=new PriorityBlockingQueue<TreeNode>(101,
new Comparator<TreeNode>() {
@Override
public int compare(TreeNode o1, TreeNode o2) {
return o1.weight - o2.weight;
}
}
);
for(Byte b:bytes){
if(map.containsKey(b)){
map.put(b,map.get(b)+1);
}
else{
map.put(b,1);
}
for(Map.Entry<Byte,Integer> it:map.entrySet()){
TreeNode node=new TreeNode(it.getValue(),it.getKey());
pq.add(node);
}
}
}
public static TreeNode buildTree(){
while(pq.size()>1){
TreeNode new_node=new TreeNode();
}
}
}
报错
现在怎么样
有时候idea显示的有问题,你重启一下,或者编译一下就好了
代码没问题的 你继续写吧
我这边没有报错的,估计是想提醒你可以简写lamdba方式:
下面是简写的方式:
public static void creatingLeafs() {
Map<Byte, Integer> map = new Hashtable<>();
pq = new PriorityBlockingQueue<>(101,
Comparator.comparingInt(o -> o.weight)
);
for (Byte b : bytes) {
if (map.containsKey(b)) {
map.put(b, map.get(b) + 1);
} else {
map.put(b, 1);
}
for (Map.Entry<Byte, Integer> it : map.entrySet()) {
TreeNode node = new TreeNode(it.getValue(), it.getKey());
pq.add(node);
}
}
}
望采纳,谢谢
可以设置idea的右小角小人,级别调中等