如何将数组的数据直接放入PriorityQueue

时间:2014-08-01 19:40:07

标签: java collections queue

如何将数组的数据直接放入实现PriorityQueue接口的Queue

ContestantFilter [] contestantFilter = new ContestantFilter [50];
contestantFilter [0] = new ContestantFilter("Agnesy Pricillia","IF110026");
contestantFilter [1] = new ContestantFilter("Ellen Shie ","IF110103");
contestantFilter [2] = new ContestantFilter("Wilma Yue Gunawan","EL110021");
.................
.................

Queue<ContestantFilter> queue = new PriorityQueue<ContestantFilter>(1,
                new Comparator<ContestantFilter>() {
                     @Override
                    public int compare(ContestantFilter t1, ContestantFilter t2) {
                        return t1.getID().compareTo(t2.getID());}});

我试过

for(ContestantFilter c : contestantFilter ){ queue.add(c);}

但它很慢。

类似的东西:

queue = ((Queue<ContestantFilter >) Arrays.asList(contestantFilter );

施法不起作用。

1 个答案:

答案 0 :(得分:2)

创建队列实例后使用PriorityQueue#addAll方法:

//... is a replacement for your custom Comparator<ContestantFilter>
Queue<ContestantFilter> queue = new PriorityQueue(...);
queue.addAll(Arrays.asList(contestantFilter));