简体   繁体   English

去除元素的最佳集合

[英]best collection for removing element

In my program i have a collection of edges ,they have to be ordered by the weight. 在我的程序中,我有一组边缘,它们必须按重量排序。

Somewhere in the program i have to process the collection , and each time i have to remove the maximum of the collection. 我必须在程序中的某个地方处理集合,并且每次必须删除集合的最大值。

I have already used an ArrayList but i'm looking for a better solution(time efficiency): 我已经使用了ArrayList,但是我正在寻找更好的解决方案(时间效率):

public class Edge implements Comparable<Edge> {
private int weight;
public void setWeight(int weight) {
    this.weight = weight;**
}

@Override
public int compareTo(Edge o) {
    return o.weight - this.weight;
}

} }

what i did : 我做了什么 :

    private ArrayList<Edge> listOfEdges = new ArrayList<>();
    // i suppose here adding some edges in the list

    Collections.sort(listOfEdges);
    for (int i = 0; i < listOfEdges.size(); i++) {
        System.out.println(listOfEdges.get(i).getWeight() + "   ");
    }

how can i get&remove the maximum of the list. 我如何获取和删除列表的最大值。 I have tested a treeSet but the edges can have the same weight, So what is the perfect Sorted Collection that accept a duplicate values. 我已经测试了treeSet,但是边缘可以具有相同的权重,那么接受重复值的完美Sorted Collection是什么。

Thank you 谢谢

In my program i have a collection of edges ,they have to be ordered by the weight... I have already used an ArrayList but i'm looking for a better solution(time efficiency): 在我的程序中,我有一组边缘,它们必须按权重排序...我已经使用了ArrayList,但是我正在寻找更好的解决方案(时间效率):

A binary tree like structure, such as a heap or priority queue, is what you are looking for. 您正在寻找二进制树之类的结构,例如或优先级队列。 Once you have your object ordering (by the Comparable interface) specified, the maximum can be obtained in O(1) time, and removed in O(log n) time for n edges. 一旦指定了对象排序(通过Comparable接口),就可以在O(1)时间中获取最大值,并在O(log n)时间中删除O(log n) n边。

how can i get&remove the maximum of the list. 我如何获取和删除列表的最大值。

peek and pop are the respective methods of a queue object implementation peek和pop是队列对象实现的相应方法

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

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