简体   繁体   English

无法在实现Comparator的类中调用自定义方法

[英]Can't call custom method in class that implements Comparator

I am having this issue where I have a PiorityBlockingQueue to sort the items in it. 我在使用PiorityBlockingQueue对项目进行排序时遇到了这个问题。 The are several options the user can sort the items being added into the queue. 用户可以使用以下几个选项对要添加到队列中的项目进行排序。

The one I'm stuck at is trying to order the queue by the most occurences of an Item . 我遇到的问题是尝试按出现次数最多的Item对队列进行排序。

The choice of the comparison is determined in the constructor of MyQueue . 比较的选择在MyQueue的构造函数中MyQueue But the counts of (eg. Low, Medium, High) isnt determined until later. 但是直到下一个时候才确定计数(例如,低,中,高)。 When it is determined, I wanted to call the update(String lst) method from ItemComparator to update the hashmap so that the sorting is correct. 确定后,我想从ItemComparator调用update(String lst)方法来更新hashmap以便排序正确。

So my issue is I can't call that method. 所以我的问题是我不能调用该方法。 I know I'm missing something but I can't figure it out. 我知道我遗漏了一些东西,但我无法弄清楚。 Any help? 有什么帮助吗? Maybe there a better design than what I doing now? 也许有比我现在做的更好的设计?

 public class ItemComparator implements Comparator<Item>
    {        
         public void update(String lst){
             test = lst;
         }

         public int compare(Item o1, Item o2) {
            HashMap<String,Integer> priority = new HashMap<>();
            priority.put("LOW", 1);
            priority.put("MEDIUM", 2);
            priority.put("HIGH", 3);

            if (priority.get(o1.getPriority()) > priority.get(o2.getPriority())) {
                return -1;
            }
            if (priority.get(o1.getPriority()) < priority.get(o2.getPriority())) {
                return 1;
            }
            return 0;
         }    
    }

This statement wont work from this class comparator.update(aString); 该语句在此类比较器中不起作用。

public class MyQueue implements AQueue{

    private Comparator<Ticket> comparator;
    private PriorityBlockingQueue<Ticket> listOfTickets;
    private String policy;

    BlockingQImpl(String processingPolicy) throws InvalidDataException {
        setPolicy(processingPolicy.toUpperCase());
        setComparator(policy);
    }

    private void setComparator(String policy) throws InvalidDataException {
        if (policy.equals("THIS")) {
            comparator = new ItemComparator(countString);
        } 
        listOfTickets = new PriorityBlockingQueue<>(10, comparator);
    }

    public void addList(int id) {
        ticks.add(id)
        comparator.update(aString);
    }
}

I think your problem is the compilation error at comparator.update(aString); 我认为您的问题是comparator.update(aString);的编译错误comparator.update(aString); right? 对?

It is because you have declared comparator as Comparator<Ticket> , that means, you are "seeing" it as a Comparator , and in a Comparator , there is no update() method. 这是因为您已将comparator声明为Comparator<Ticket> ,这意味着您正在将其视为Comparator ,并且在Comparator中没有update()方法。

You should declare it as ItemComparator 您应该将其声明为ItemComparator

ie

private ItemComparator comparator;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM