Map
An object that maps keys to values. A map cannot contain duplicate keys; each key can map to at most one value.
TreeMap
public class TreeMapTest {
private TreeMap<String, String> collection;
@Before
public void setUp() throws Exception {
collection = new TreeMap<>();
}
@After
public void tearDown() throws Exception {
collection.clear();
}
@Ignore
@Test
public void itNotGuaranteesToTheIterationOrder() throws Exception {
}
@Ignore
@Test
public void itSortElementsByDefault() throws Exception {
}
@Test
public void itCanHaveDuplicateElement() throws Exception {
// arrange
String firstKey = "key";
String secondKey = "key2";
// act
collection.put(firstKey, "fake");
collection.put(secondKey, "fake");
// assert
assertThat("it should have 2 elements", collection.size(), is(2));
assertThat("its first element is same as second", collection.get(firstKey), is(collection.get(secondKey)));
assertThat("its first element is same instance as second", collection.get(firstKey), sameInstance(collection.get(secondKey)));
}
@Test
public void itCanHaveNullElement() throws Exception {
// arrange
String firstKey = "key";
// act
collection.put(firstKey, null);
// assert
assertThat("it should have 1 elements", collection.size(), is(1));
assertThat("it has null element", collection.get(firstKey), is(nullValue()));
}
@Test(expected = NullPointerException.class)
public void itCanNotHaveNullKey() throws Exception {
// act
collection.put(null, "element");
}
}
ArrayMap
public class ArrayMapTest {
private ArrayMap<String, String> collection;
@Before
public void setUp() throws Exception {
collection = new ArrayMap<>();
}
@After
public void tearDown() throws Exception {
collection.clear();
}
@Ignore
@Test
public void itNotGuaranteesToTheIterationOrder() throws Exception {
}
@Ignore
@Test
public void itSortElementsByDefault() throws Exception {
}
@Test
public void itCanHaveDuplicateElement() throws Exception {
// arrange
String firstKey = "key";
String secondKey = "key2";
// act
collection.put(firstKey, "fake");
collection.put(secondKey, "fake");
// assert
assertThat("it should have 2 elements", collection.size(), is(2));
assertThat("its first element is same as second", collection.get(firstKey), is(collection.get(secondKey)));
assertThat("its first element is same instance as second", collection.get(firstKey), sameInstance(collection.get(secondKey)));
}
@Test
public void itCanHaveNullElement() throws Exception {
// arrange
String firstKey = "key";
// act
collection.put(firstKey, null);
// assert
assertThat("it should have 1 elements", collection.size(), is(1));
assertThat("it has null element", collection.get(firstKey), is(nullValue()));
}
@Test
public void itCanHaveNullKey() throws Exception {
// arrange
String element = "element";
// act
collection.put(null, element);
// assert
assertThat("it should have 1 elements", collection.size(), is(1));
assertThat("it has element with null key", collection.get(null), is(element));
}
}
HashMap
public class HashMapTest {
private HashMap<String, String> collection;
@Before
public void setUp() throws Exception {
collection = new HashMap<>();
}
@After
public void tearDown() throws Exception {
collection.clear();
}
@Ignore
@Test
public void itNotGuaranteesToTheIterationOrder() throws Exception {
}
@Ignore
@Test
public void itSortElementsByDefault() throws Exception {
}
@Test
public void itCanHaveDuplicateElement() throws Exception {
// arrange
String firstKey = "key";
String secondKey = "key2";
// act
collection.put(firstKey, "fake");
collection.put(secondKey, "fake");
// assert
assertThat("it should have 2 elements", collection.size(), is(2));
assertThat("its first element is same as second", collection.get(firstKey), is(collection.get(secondKey)));
assertThat("its first element is same instance as second", collection.get(firstKey), sameInstance(collection.get(secondKey)));
}
@Test
public void itCanHaveNullElement() throws Exception {
// arrange
String firstKey = "key";
// act
collection.put(firstKey, null);
// assert
assertThat("it should have 1 elements", collection.size(), is(1));
assertThat("it has null element", collection.get(firstKey), is(nullValue()));
}
@Test
public void itCanHaveNullKey() throws Exception {
// arrange
String element = "element";
// act
collection.put(null, element);
// assert
assertThat("it should have 1 elements", collection.size(), is(1));
assertThat("it has element with null key", collection.get(null), is(element));
}
}
LinkedHashMap
public class LinkedHashMapTest {
private LinkedHashMap<String, String> collection;
@Before
public void setUp() throws Exception {
collection = new LinkedHashMap<>();
}
@After
public void tearDown() throws Exception {
collection.clear();
}
@Ignore
@Test
public void itNotGuaranteesToTheIterationOrder() throws Exception {
}
@Ignore
@Test
public void itSortElementsByDefault() throws Exception {
}
@Test
public void itCanHaveDuplicateElement() throws Exception {
// arrange
String firstKey = "key";
String secondKey = "key2";
// act
collection.put(firstKey, "fake");
collection.put(secondKey, "fake");
// assert
assertThat("it should have 2 elements", collection.size(), is(2));
assertThat("its first element is same as second", collection.get(firstKey), is(collection.get(secondKey)));
assertThat("its first element is same instance as second", collection.get(firstKey), sameInstance(collection.get(secondKey)));
}
@Test
public void itCanHaveNullElement() throws Exception {
// arrange
String firstKey = "key";
// act
collection.put(firstKey, null);
// assert
assertThat("it should have 1 elements", collection.size(), is(1));
assertThat("it has null element", collection.get(firstKey), is(nullValue()));
}
@Test
public void itCanHaveNullKey() throws Exception {
// arrange
String element = "element";
// act
collection.put(null, element);
// assert
assertThat("it should have 1 elements", collection.size(), is(1));
assertThat("it has element with null key", collection.get(null), is(element));
}
}