UniqueList extends AbstractList<T> implements List<T> { @Override public void add(final int index, final T element) { final String elementName = element.getName(); if (this.elementByName.containsKey(elementName)) { throw new IllegalArgumentException("Element " + element + " already in list"); } this.elementByName.put(elementName, element); this.list.add(index, element); } UniqueList(); @Override void add(final int index, final T element); @Override T get(final int index); T getByName(final String name); Set<String> nameSet(); @Override T remove(final int index); @Override boolean remove(final Object o); T removeByName(final String name); @Override T set(final int index, final T element); @Override int size();  }
@Test() public final void testDuplicate() { final List<FirstLetter> ul = new UniqueList<FirstLetter>(); ul.add(new FirstLetter("FastODS")); this.thrown.expect(IllegalArgumentException.class); this.thrown.expectMessage("already in list"); ul.add(new FirstLetter("FastODS2")); }