Note that there are some explanatory texts on larger screens.

plurals
  1. POSuitable collection class for event listeners in Java
    primarykey
    data
    text
    <p>Related: <a href="https://stackoverflow.com/questions/1391918/does-java-have-a-linkedconcurrenthashmap-data-structure">Does java have a &quot;LinkedConcurrentHashMap&quot; data structure?</a></p> <hr> <p>I am looking for a collection class to hold references to event listeners.</p> <p>Ideally I would like the collection to have the following properties (in order of priority):</p> <ol> <li>Maintains insertion order. The earlier listeners may cancel the event, preventing it from being delivered to listeners added later. This will break if using a class such as <code>HashSet</code> whose iterator may return elements in the wrong order.</li> <li>Uses <code>WeakReference</code>s so that the listener list does not prevent the listeners from being garbage-collected.</li> <li>The collection is a <code>Set</code>, so duplicates are automatically removed.</li> <li>The <code>Iterator</code> is a thread-safe snapshot of the collection, unaffected by the addition of new listeners. Also allows events to be delivered on multiple threads. (This is not essential - I could iterate over a clone of the set instead.)</li> </ol> <p>I am aware of some classes that satisfy some but not all of these criteria. Examples:</p> <ul> <li><code>java.util.LinkedHashSet</code> (#1 and #3)</li> <li><code>java.util.WeakHashMap</code>, wrapped by <code>Collections.newSetFromMap</code> (#2 and #3)</li> <li><code>javax.swing.event.EventListenerList</code> (needs some extra synchronization) (#1 and #4)</li> <li><code>java.util.concurrent.CopyOnWriteArraySet</code> (#1, #3 and #4)</li> </ul> <p>But nothing with both #1 and #2. Does class like this exist in a library somewhere?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload