Linked list that support modifications during iteration Update:

Fixed last item is not removed during iteration
Fixed clear() during iteration does nothing
Fixed addition of removed item is ignored

The lasted code can be found here. Unitest can be found here.

As part of my Signals library, I needed to create a custom Linked List with the below properties

It should allow &quot;addition&quot; and &quot;removal&quot; of items during iteration, aka when iterator() is called, like in the for loop.
It should ignore-Addition of duplicated items.
It should ignore-Removal of nonexisting items.
it should be a thread-safe

I ended up with the below implementation. Please tell me what do you think in terms of

clean code
bugs
performance

package com.gazman.signals

import java.util.*

internal class ListenersList&lt;T&gt; : Iterable&lt;T?&gt; {
    private val none = Node&lt;T&gt;(null)
    private var head: Node&lt;T&gt;? = null
    private var tail: Node&lt;T&gt;? = null
    private var map = IdentityHashMap&lt;T, Node&lt;T&gt;&gt;()

    fun isNotEmpty(): Boolean {
        return map.isNotEmpty()
    }

    fun add(listener: T) {
        synchronized(this) {
            if (map.containsKey(listener)) {
                return
            }
            val node = Node(listener)
            map[listener] = node

            if (tail == null) {
                head = node
                tail = node
            } else {
                node.previous = tail
                tail?.next = node
                tail = node
            }
        }
    }

    fun remove(listener: T) {
        synchronized(this) {
            val node = map[listener]
            node?.previous?.next = node?.next
            node?.next?.previous = node?.previous
        }
    }

    override fun iterator(): Iterator&lt;T?&gt; {
        return object : Iterator&lt;T?&gt; {
            var node: Node&lt;T&gt;? = none

            override fun hasNext() = node != null &amp;&amp; node != tail

            override fun next(): T? {
                node = if (node == none) {
                    this@ListenersList.head
                } else {
                    node?.next
                }
                return node?.value
            }

        }
    }

    fun clear() {
        synchronized(this) {
            head = null
            tail = null
            map.clear()
        }
    }
}

Node
package com.gazman.signals

internal class Node&lt;T&gt;(val value: T?) {
    var previous: Node&lt;T&gt;? = null
    var next: Node&lt;T&gt;? = null
}
I haven't done much Kotlin, however something looked wrong and I was curious, so I wrote some tests.  I had a couple of failures, so you may have issues, or it may be that I'm expecting the wrong behaviour.  There's three scenarios to consider.  I've defined a list of string to make it easy to test.
val list = ListenersList&lt;String&gt;()

clear
You've said that you want the code to be tolerant of adding/removing items during iteration.  I assumed that it would therefor also be tolerant of clear being called (it seems equivalent to calling remove on every item).  So, I would expect it to iterate up to the point of the clear being called.  This isn't what happens.  Instead, it iterates past the end of the list.  So, for example:
@Test
fun clearMiddleWorks() {
    list.add(&quot;a&quot;)
    list.add(&quot;b&quot;)
    list.add(&quot;c&quot;)

    var items = &quot;&quot;

    for (item in list) {
        if(item == &quot;a&quot;)
            list.clear()
        items += item
    }

    assertEquals(&quot;a&quot;, items)
}

This test fails, because instead of exiting the loop after &quot;a&quot;, it continues building up the string, and fails on &quot;a&quot; != &quot;abcnull&quot;
end removal
This issue exists in the code you've posted, however it looks like you've cleaned it up in your latest github version.  Removing the last item in the list during iteration, fails to terminate as expected:
@Test
fun removalOfEndWorks() {
    list.add(&quot;a&quot;)
    list.add(&quot;b&quot;)
    list.add(&quot;c&quot;)

    var items = &quot;&quot;

    for (item in list) {
        if(item == &quot;b&quot;) {
            list.remove(&quot;c&quot;)
        }
        items += item
    }

    assertEquals(&quot;ab&quot;, items)
}

This fails with &quot;ab&quot; != &quot;abnull&quot;
remove and add conflict
This may actually be expected behaviour, however it seems odd to me.  If I remove an item from the list, whilst I'm iterating over it, then add an item with the same value, I'd expect that item to be treated the same was as if I'd added a new item, however it isn't.  So, this test passes:
@Test
fun removalAdditionOtherInMiddleWorks() {
    list.add(&quot;a&quot;)
    list.add(&quot;b&quot;)
    list.add(&quot;c&quot;)

    var items = &quot;&quot;


    for (item in list) {
        if(item == &quot;a&quot;) {
            list.remove(&quot;b&quot;)
            list.add(&quot;d&quot;)
        }
        items += item
    }

    assertEquals(&quot;acd&quot;, items)
}

And this test fails:
@Test
fun removalAdditionOfMiddleWorks() {
    list.add(&quot;a&quot;)
    list.add(&quot;b&quot;)
    list.add(&quot;c&quot;)

    var items = &quot;&quot;


    for (item in list) {
        if(item == &quot;a&quot;) {
            list.remove(&quot;b&quot;)
            list.add(&quot;b&quot;)
        }
        items += item
    }

    assertEquals(&quot;acb&quot;, items)
}

It fails because the added in &quot;b&quot; is ignored, so &quot;acb&quot; != &quot;ac&quot;.  Is this expected?