如何在java中维护有序链表和添加、删除元素

如何在java中维护有序链表和添加、删除元素,java,linked-list,iterator,listiterator,Java,Linked List,Iterator,Listiterator,我想维护一个名为“ClientStatus”的对象列表,其中只包含客户端标识(id)和一些时间字段,即与此客户端相关的对象。此列表应根据此时间字段按递增顺序排序。我希望支持的操作包括: * peek and remove an entry from the beginning * Search the list for an entry and if found, remove it * Add an entry to this list 我希望每个新条目都有time>=列表中的最后一个条目

我想维护一个名为“ClientStatus”的对象列表,其中只包含客户端标识(id)和一些时间字段,即与此客户端相关的对象。此列表应根据此时间字段按递增顺序排序。我希望支持的操作包括:

* peek and remove an entry from the beginning 
* Search the list for an entry and if found, remove it
* Add an entry to this list
我希望每个新条目都有time>=列表中的最后一个条目,但有可能出现争用条件,我可能会得到无序值。因此,我认为从末尾迭代列表将是最节省时间的解决方案

以下是我使用的DS:

  • LinkedList和ListIterator作为ListIterator允许您从头开始迭代元素,并在迭代过程中添加新条目。但代码看起来很混乱:

    假设我的列表有值:2 3 6 7 9,我想加5

    ListIterator<Integer> it = list.listIterator(list.size());
    
    while (it.hasPrevious()) {
        if (it.previous().intValue() <= 5) {    
            it.next();
            break;
        }
    }
    
    ListIterator it=list.ListIterator(list.size());
    while(it.hasPrevious()){
    
    如果(it.previous().intValue()我认为您要查找的类型是一个
    SortedSet
    ,它可以通过
    TreeSet
    实现

    排序集使用
    compare(…)
    实现对集中的所有条目进行排序

    集合中的所有元件必须实现
    比较器
    接口,以便分拣集正常工作

    您可能更喜欢使用类似方式的
    SortedMap
    。您可以将客户端id设置为值,将时间字段设置为键,映射将使用时间字段比较来保持排序

    您还谈到了竞争条件。使用TreeMap的javadoc对此进行了讨论,并提到必须使用
    集合对映射进行外部同步。synchronizedSortedMap(…)

    我还没有尝试过同步的实现,所以我不能对它添加太多内容

    有关更多信息,请查看文档:

    我希望每个新条目都有时间>=列表中的最后一个条目 但有一种可能的比赛条件,我可能会退出 顺序值。所以我认为从最后开始迭代列表 将是最节省时间的解决方案


    如果您有并发访问的可能性,您必须同步LinkedList的迭代和变异。您不仅会使项目无序,最好的情况是迭代会导致ConcurrentModificationException,最坏的情况是数据的不确定丢失。

    您需要允许重复条目吗?我认为de>SortedSet
    可能是一种方法。但是如果您想使用
    链接列表
    ,通过计算索引,您似乎走上了正确的轨道,但是使用
    添加
    方法将在列表中插入一个新条目。请参阅。
    private static class ClientStatus {
        public long id;
        public long time;
    
        public ClientStatus(final long id, final long time) {
            this.id = id;
            this.time = time;
        }
    
        @Override
        public boolean equals(final Object o) {
            if ((o == null) || (getClass() != o.getClass())) {
                return false;
            }
            if (this == o) {
                return true;
            }
            ClientStatus obj = (ClientStatus) o;
            return this.id == obj.id;
        }
    }
    
    public static void main(String[] args) {
    
        SortedSet<ClientStatus> active_client_set = new TreeSet<ClientStatus>(
                new Comparator<ClientStatus>() {
                    @Override
                    public int compare(final ClientStatus o1, final ClientStatus o2) {
                        if (o1.getClass() != o2.getClass()) {
                            return -1;
                        }
    
                        if (o1 == o2 || o1.id == o2.id) {
                            return 0;
                        }
                        return (o1.time - o2.time) < 0 ? -1 : +1;
                    }
                }
        );
    }