SafeIterableMap implements Iterable<Map.Entry<K, V>> { public V remove(@NonNull K key) { Entry<K, V> toRemove = get(key); if (toRemove == null) { return null; } mSize--; if (!mIterators.isEmpty()) { for (SupportRemove<K, V> iter : mIterators.keySet()) { iter.supportRemove(toRemove); } } if (toRemove.mPrevious != null) { toRemove.mPrevious.mNext = toRemove.mNext; } else { mStart = toRemove.mNext; } if (toRemove.mNext != null) { toRemove.mNext.mPrevious = toRemove.mPrevious; } else { mEnd = toRemove.mPrevious; } toRemove.mNext = null; toRemove.mPrevious = null; return toRemove.mValue; }  V putIfAbsent(@NonNull K key, @NonNull V v); V remove(@NonNull K key); int size(); @NonNull @Override Iterator<Map.Entry<K, V>> iterator(); Iterator<Map.Entry<K, V>> descendingIterator(); IteratorWithAdditions iteratorWithAdditions(); Map.Entry<K, V> eldest(); Map.Entry<K, V> newest(); @Override boolean equals(Object obj); @Override String toString();  }
@Test public void testRemoveLast() { SafeIterableMap<Integer, Integer> map = from(1, 2, 3, 4).to(10, 20, 30, 40); assertThat(map.remove(4), is(40)); assertThat(map, is(from(1, 2, 3).to(10, 20, 30))); }
@Test public void testRemoveFirst() { SafeIterableMap<Integer, Boolean> map = mapOf(1, 2, 3, 4); assertThat(map.remove(1), is(true)); assertThat(map, is(mapOf(2, 3, 4))); }
@Test public void testRemoveMiddle() { SafeIterableMap<Integer, Integer> map = from(1, 2, 3, 4).to(10, 20, 30, 40); assertThat(map.remove(2), is(20)); assertThat(map.remove(3), is(30)); assertThat(map, is(from(1, 4).to(10, 40))); }
@Test public void testRemoveNotExisted() { SafeIterableMap<Integer, Boolean> map = mapOf(1, 2, 3, 4); assertThat(map.remove(5), is((Boolean) null)); assertThat(map, is(mapOf(1, 2, 3, 4))); }
@Test public void testRemoveSole() { SafeIterableMap<Integer, Integer> map = from(1).to(261); assertThat(map.remove(1), is(261)); assertThat(map, is(new SafeIterableMap<Integer, Integer>())); }
@Test public void testRemoveDuringIteration1() { SafeIterableMap<Integer, Integer> map = from(1, 2, 3, 4).to(10, 20, 30, 40); int index = 0; int[] expected = new int[]{1, 4}; for (Entry<Integer, Integer> i : map) { assertThat(i.getKey(), is(expected[index++])); if (index == 1) { assertThat(map.remove(2), is(20)); assertThat(map.remove(3), is(30)); } } }
@Test public void testRemoveDuringIteration4() { SafeIterableMap<Integer, Boolean> map = mapOf(1, 2); int[] expected = new int[]{1, 2}; int index = 0; for (Entry<Integer, Boolean> entry : map) { assertThat(entry.getKey(), is(expected[index++])); if (index == 1) { map.remove(1); } } assertThat(index, is(2)); }