ArrayList - 添加“相同"对象(相同 => 等于,hashCode),线程 [英] ArrayList - add "same" objects (same => equals, hashCode), Threads

查看:72
本文介绍了ArrayList - 添加“相同"对象(相同 => 等于,hashCode),线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个问题.当我尝试将相同"对象两次添加到 ArrayList 时会发生什么.相同"是指单个类的对象,它与 equals() 和 hashCode() 相同.对于大多数成员变量,它具有不同的值,并且可能是从不同的线程创建的,但对于 equals() 和 hashCode() 而言,它是相同的".那么第二个对象会替换第一个对象吗?

Ive got one question. What happens when I try to add the "same" object twice to an ArrayList. With "the same" I mean an object of an individual class, which is identified as the same with equals() and hashCode(). It has different values for most of the member variables and was created from maybe different threads, but for equals() and hashCode() its the "same". Does the second object then replace the first object?

另外,如果两个线程试图同时将这些对象添加到 ArrayList 会发生什么?这甚至可能吗?如果是,会发生什么?

Also, what happens if two threads try to add those objects exactly at the same time to the ArrayList? Is this even possible? If yes, what happens?

谢谢!:-)

感谢所有的回答!我应该使用 synchronizedList 而不是使用synchronize(list){}"?-->我阅读了文档,即使使用synchronizedList,也应该使用synchronize(list)

Thanks for all the answers! Should I use synchronizedList then rather then using "synchronize(list){}"? --> I read the docs, even with synchronizedList, for iterating synchronize(list) shall be used

可以将 synchronizedList 声明为成员变量吗?我试过了,但没有奏效.

Can a synchronizedList be declared as a member variable? I tried, but it didnt work.

推荐答案

不,ArrayList 根本不会尝试检测重复项 - 您可以使用 ArrayList完全相同的引用出现多次.如果你想要一个集合来避免重复,你需要一个 Set 实现 - 如果你想要保留插入顺序,你可能想要 LinkedHashSet.

No, ArrayList doesn't attempt to detect duplicates at all - you can have an ArrayList with the exact same reference appearing multiple times. If you want a collection to avoid duplicates, you need a Set implementation - and if you also want to preserve insertion order, you probably want LinkedHashSet.

但是请注意,在没有锁定 ArrayList 的情况下,首先应该从多个线程中进行变异 - 它并不意味着是线程安全的集合道路.多个线程可以在不同步的情况下从 ArrayList读取,但不能对其进行变异.来自文档:

Note, however, that without locking ArrayList should not be mutated from multiple threads in the first place - it's simply not meant to be a thread-safe collection in that way. Several threads can read from an ArrayList without synchronization, but not mutate it. From the docs:

请注意,此实现不是同步的.如果多个线程同时访问一个 ArrayList 实例,并且至少有一个线程在结构上修改了列表,则必须在外部进行同步.(结构修改是添加或删除一个或多个元素,或显式调整后备数组大小的任何操作;仅设置元素的值不是结构修改.)这通常是通过同步一些自然封装的对象来完成的列表.如果不存在这样的对象,则应使用 Collections.synchronizedList 方法包装"该列表.这最好在创建时完成,以防止对列表的意外不同步访问

Note that this implementation is not synchronized. If multiple threads access an ArrayList instance concurrently, and at least one of the threads modifies the list structurally, it must be synchronized externally. (A structural modification is any operation that adds or deletes one or more elements, or explicitly resizes the backing array; merely setting the value of an element is not a structural modification.) This is typically accomplished by synchronizing on some object that naturally encapsulates the list. If no such object exists, the list should be "wrapped" using the Collections.synchronizedList method. This is best done at creation time, to prevent accidental unsynchronized access to the list

如果你想在不锁定的情况下从多个线程改变一个集合,我建议你查看java.util.concurrent.

If you want to mutate a collection from multiple threads without locking, I suggest you look at the collections in java.util.concurrent.

这篇关于ArrayList - 添加“相同"对象(相同 => 等于,hashCode),线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆