Java 不可变列表

发布于 2024-11-19 09:34:30 字数 818 浏览 3 评论 0原文

我目前正在构建一个 LRU 缓存,我需要在其中存储最后 N 个插入的项目。项目将被频繁插入(即许多写入操作),并且读取操作通常会返回大量事件始终严格按顺序,尽管从缓存中的任意点开始。例如,假设缓存包含事件:

[1, 2, 3, 4, 5, 6]

合法的读取操作是返回事件 [2, 3, 4] 上的迭代器。

由于读取操作可能是长期存在的,我想使用一种数据结构,在其中我可以安全地迭代每次读取尝试的序列的逻辑副本,从而防止缓存读取停滞任何后续写入。但是,使用普通 Java ArrayListLinkedList 意味着制作完整副本的开销很大。

我的问题:是否有任何第 3 方 Java 库提供类似于 Scala 的不可变数据结构,尝试修改数据结构会返回一个新的不可变副本(实际上是基于原始数据结构和因此复制操作非常快)?显然,数据结构不符合 Java Collections API,因为像 add(T) 这样的操作需要返回新集合(而不是 void)。

(请不要评论/回答将此视为过早优化的情况。)

提前致谢。

注意

Guava 的 ImmutableList 几乎达到了我的需要:它允许您调用 copyOf ,其中副本通常引用原始副本(避免执行实际副本) 。不幸的是,您不能采取其他方式将项目添加到列表中并取回包含新元素的副本。

I am currently building an LRU cache where I need to store the last N inserted items. Items will be inserted frequently (i.e. many write operations) and read operations will typically return a large number of events always strictly in sequence albeit starting at an arbitrary point in the cache. For example, suppose the cache contains events:

[1, 2, 3, 4, 5, 6]

A legal read operation would be to return an iterator over events [2, 3, 4].

Due to read operations potentially being long-lived I'd like to use a data structure where I can safely iterate over a logical copy of the sequence for each read attempt, thus preventing a cache read from holding up any subsequent writes. However, using a vanilla Java ArrayList or LinkedList implies a large overhead in making a full copy.

My question: Are there any 3rd party Java libraries that provide immutable data structures similar to Scala, whereby attempts to modify the data structure return a new immutable copy (which is in fact based on the original data structure and hence the copy operation is very fast)? Obviously the data structure could not conform to the Java Collections API as operations like add(T) would need to return the new collection (rather than void).

(Please no comments / answers citing this as a case of premature optimisation.)

Thanks in advance.

Note

Guava's ImmutableList nearly achieves what I need: It allows to you call copyOf where the copy typically references the original (avoiding performing an actual copy). Unfortunately you can't go the other way and add an item to the list and get back a copy that includes the new element.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

软甜啾 2024-11-26 09:34:31

函数式 Java 以库(而不是不同的语言)的形式出现,并提供不可变的集合。不确定它是否适合您的需求,但值得一试。

Functional Java comes in the form of a library (not a different language) and provides immutable collections. Not sure if it'll fit your needs but worth a try.

亽野灬性zι浪 2024-11-26 09:34:31

Guava

Google Guava 可以满足您的需求。

当您想要更新缓存时,请使用 Guava 的构建器模式从旧缓存创建新缓存,然后删除旧缓存。

要更新缓存,请创建一个 ImmutableList.Builder() 并使用现有的 ImmutableList。通过Builder界面修改列表。然后调用.build()来获取新的ImmutableList,并删除旧的缓存。新的缓存将重用所有旧的对象,因此这是一个非常轻量级的操作。

当有人想要缓存(或其项目之一)的不可变副本时,返回 copyOf(),他们将可以访问不可变的快照。

警告,如果您正在使用线程,请确保将列表包装在一个对象中并同步它的 get() 和 get() 。 insert() 方法。

您可以访问Guava 网站了解更多信息。

Guava

Google Guava can meet your needs.

When you want to update the cache, use Guava's builder pattern to create a new cache from the old one, then delete the old cache.

To update the cache, create an ImmutableList.Builder() and initialize it with your existing ImmutableList. Modify the list through the Builder interface. Then call .build() to get a new ImmutableList, and delete the old cache. The new cache will reuse all the old objects, so this is a very lightweight operation.

When someone wants an immutable copy of the cache (or one of its items), return copyOf(), and they'll get access to an immutable snapshot.

Caveat, if you're working with threads, make sure you wrap the list in an object and synchronize it's get() & insert() methods.

You can read more at the Guava site.

鹤舞 2024-11-26 09:34:31

JDK 9 有新的 of() 方法工厂。例如,您可以有一个 immutable Set 因为

Set<Integer> intSet = Set.of(1, 2, 3);

您可以对列表执行相同的操作,例如

List<String> stringList = List.of("A", "B", "C");

Map

Map<String, String> doubleMap = Map.of("key1", "val1", 
                                       "key2", "val2");

JDK 9 has new of() method factories. E.g. you can have an immutable Set as

Set<Integer> intSet = Set.of(1, 2, 3);

You can do the same with a List, e.g.

List<String> stringList = List.of("A", "B", "C");

and a Map:

Map<String, String> doubleMap = Map.of("key1", "val1", 
                                       "key2", "val2");
装纯掩盖桑 2024-11-26 09:34:31

您看过 CopyOnWriteArrrayList 吗?对列表的每次更改都会将所有内容复制到新的支持数组中,而您可能正在迭代的当前数组不受影响。

Have you looked at the CopyOnWriteArrrayList? Each mutation to the list will copy all contents to a new backing array leaving the current array you may be iterating on uneffected.

纸短情长 2024-11-26 09:34:31

看起来您想在这里实现一个单链表,然后可以由不同的包装对象共享。您是否想删除元素,或者只添加新元素?

如果只添加而不删除,我想可以使用 CopyOnWriteArrayList 的更简单的变体,它只在旧数组已满时才进行复制。然后,sublist() 方法将简单地创建一个新的包装对象。

/**
 * A list which only supports appending objects.
 */
public class OnlyAppendingList<E> extends AbstractList<E> {

    private Object[] data;
    private int len;

    public int size() {
        return this.len;
    }

    public E get(int index) {
        if(index >= this.len)
           throw new IndexOutOfBoundsException(index + " >= " + this.len);
        @SuppressWarnings("unchecked")
        E res = this.data[index];
        return res;
    }

    public boolean add(E element) {
        if(len == data.length) {
             this.resize();
        }
        this.data[this.len] = element;
        this.len++;
        return true;
    }

    private void resize() {
        this.data = Arrays.copyOf(data, data.length * 2 +2);
    }

    public void add(int index, E element) {
       if(index > this.len) {
          throw new IndexOutOfBoundsException(index + " > " + len);
       }
       if(index < this.len) {
           throw new UnsupportedOperationException("we only support appending, not insertion!");
       }
       this.add(element);
    }


    /**
     * Returns an immutable sublist of this list.
     */
    public List<E> subList(final int fromIndex, final int toIndex) {
        // TODO: bounds checks
        return new SubList<E>(this.data, fromIndex, fromIndex - toIndex);
    }

    private static class SubList<E> extends AbstractList<E> {
        private Object[] data;
        private int start;
        private int len;

        SubList(Object[] data, int start, int len) {
            this.data = data; this.start = start; this.len = len;
        }

        public int size() {
            return this.len;
        }

        public E get(int index) {
            if(index >= this.len)
               throw new IndexOutOfBoundsException(index + " >= " + this.len);
            if(index < 0)
               throw new IndexOutOfBoundsException(index + " < 0");

            @SuppressWarnings("unchecked")
            E res = this.data[index + start];
            return res;
        }
        public List<E> subList(int from, int to) {
            // TODO: bounds check
            return new SubList(data, start + from, to - from);
        }
    }
}

我认为,如果这是由多个线程修改的,则应该使 add 方法同步,并使 len 变量 volatile 同步。 (我没有完全检查它是否是线程安全的。)

It looks like you want to implement a singly-linked list here, which can then be shared by different wrapper objects. Do you ever want to remove elements, or do you only append new ones?

If there is only adding and no removal, I suppose a simpler variant of CopyOnWriteArrayList, which only makes the copy whenever the old array is full, could do. The sublist() methods then would simply create a new wrapper object.

/**
 * A list which only supports appending objects.
 */
public class OnlyAppendingList<E> extends AbstractList<E> {

    private Object[] data;
    private int len;

    public int size() {
        return this.len;
    }

    public E get(int index) {
        if(index >= this.len)
           throw new IndexOutOfBoundsException(index + " >= " + this.len);
        @SuppressWarnings("unchecked")
        E res = this.data[index];
        return res;
    }

    public boolean add(E element) {
        if(len == data.length) {
             this.resize();
        }
        this.data[this.len] = element;
        this.len++;
        return true;
    }

    private void resize() {
        this.data = Arrays.copyOf(data, data.length * 2 +2);
    }

    public void add(int index, E element) {
       if(index > this.len) {
          throw new IndexOutOfBoundsException(index + " > " + len);
       }
       if(index < this.len) {
           throw new UnsupportedOperationException("we only support appending, not insertion!");
       }
       this.add(element);
    }


    /**
     * Returns an immutable sublist of this list.
     */
    public List<E> subList(final int fromIndex, final int toIndex) {
        // TODO: bounds checks
        return new SubList<E>(this.data, fromIndex, fromIndex - toIndex);
    }

    private static class SubList<E> extends AbstractList<E> {
        private Object[] data;
        private int start;
        private int len;

        SubList(Object[] data, int start, int len) {
            this.data = data; this.start = start; this.len = len;
        }

        public int size() {
            return this.len;
        }

        public E get(int index) {
            if(index >= this.len)
               throw new IndexOutOfBoundsException(index + " >= " + this.len);
            if(index < 0)
               throw new IndexOutOfBoundsException(index + " < 0");

            @SuppressWarnings("unchecked")
            E res = this.data[index + start];
            return res;
        }
        public List<E> subList(int from, int to) {
            // TODO: bounds check
            return new SubList(data, start + from, to - from);
        }
    }
}

If this is modified by multiple threads, you should make the add method synchronized and the len variable volatile, I think. (I did not completely check that it then is threadsafe.)

陌生 2024-11-26 09:34:31

Pure4J

通过 Clojure 持久集合类的副本提供不可变集合。它可能不完全是您所追求的,因为它是关于在 java 程序(子集)上强制执行纯函数语义。

另一方面,它对元素和集合都有不变性保证。当您从集合中添加/删除元素时,您会得到一个新集合,并且原始集合保持不变。

Pure4J

Provides immutable collections via copies of the Clojure persistent collections classes. It might not be exactly what you are after, as it is about enforcing pure functional semantics on (subsets of) java programs.

On the other hand, it has immutability guarantees for both elements and collections. When you add/remove an element from a collection you get back a new collection and the original remains unchanged.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文