priority queue add方法修改所有元素,并将最后一个元素添加到其中为什么?

dz6r00yl  于 2021-06-04  发布在  Hadoop
关注(0)|答案(0)|浏览(298)

/在hadoop reduce方法中,我将topfrequency对象添加到priorityqueue中。假设我的reducer类中存在gettotalcount方法。totalcount是doublewritable类型的示例变量/

class TopFrequency{
    private double relativeFrequency;
    private WordPair wordPair;

    @Override
    public int compareTo(TopFrequency arg0){
        int i=this.wordPair.compareTo(arg0.wordPair);
        if(this.relativeCount<arg0.relativeCount){
           return -1;
        }else if(this.relativeCount>arg0.relativeCount){
            return 1;
        }else if(this.relativeCount==arg0.relativeCount){
            return 0;
        }
        return i;
    }
}
class WordPair{
    private Text word;
    private Text neighbor;
}
class Reduce extends Reducer{

@重写受保护的void reduce(字对键、iterable值、上下文){
双计数=0.0;

int count = getTotalCount(values);
        relativeFrequency=((double)count/totalCount.get());
        relativeCount.set((double) count / totalCount.get());

    //Creating an object of TopFrequency

        TopFrequency topFrequency=new TopFrequency(key, relativeFrequency);

    // All the references in reducerQueue point to   recently added object

        reducerQueue.add(topFrequency); 

       System.out.println("After adding to the queue "+reducerQueue);

    }
}

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题