List

An ordered collection. Lists can contain duplicate elements.Lists typically allow multiple null elements if they allow null elements at all.

ArrayList

public class ArrayListTest {
    private List<String> list;

    @Before
    public void setUp() throws Exception {
        list = new ArrayList<>();
    }

    @After
    public void tearDown() throws Exception {
        list.clear();
    }

    @Ignore
    @Test
    public void itHasOrderedElements() throws Exception {
        // arrange
        String[] result = new String[3];

        // act
        list.add("1");
        list.add("2");
        list.add("3");

        // assert
        int index = 0;
        Iterator<String> iterator = list.iterator();
        do {
            result[index] = iterator.next();
            index++;
        } while (iterator.hasNext());
        assertThat("it order elements to 123", result, arrayContaining("1", "2", "3"));
    }

    @Ignore
    @Test
    public void itNotSortElementsByDefault() throws Exception {
        // act
        list.add("B");
        list.add("A");
        list.add("C");

        // assert
        String result = "";
        for (String element: list) {
            result += element;
        }
        assertThat("it not sort elements from BAC to ABC or CBA", result, is(not(oneOf("ABC", "CBA"))));
    }

    @Test
    public void itCanHaveDuplicateElement() throws Exception {
        // act
        list.add("fake");
        list.add("fake");

        // assert
        assertThat("it should have 2 elements", list.size(), is(2));
        assertThat("its first element is same as second", list.get(0), is(list.get(1)));
        assertThat("its first element is same instance as second", list.get(0), sameInstance(list.get(1)));
    }

    @Test
    public void itCanHaveNullElement() throws Exception {
        // act
        list.add(null);

        // assert
        assertThat("it should have 1 elements", list.size(), is(1));
        assertThat("it has null element", list.get(0), is(nullValue()));
    }
}

LinkedList

public class LinkedListTest {
    private LinkedList<String> list;

    @Before
    public void setUp() throws Exception {
        list = new LinkedList<>();
    }

    @After
    public void tearDown() throws Exception {
        list.clear();
    }

    @Ignore
    @Test
    public void itHasOrderedElements() throws Exception {
        // arrange
        String[] result = new String[3];

        // act
        list.add("1");
        list.add("2");
        list.add("3");

        // assert
        int index = 0;
        Iterator<String> iterator = list.iterator();
        do {
            result[index] = iterator.next();
            index++;
        } while (iterator.hasNext());
        assertThat("it order elements to 123", result, arrayContaining("1", "2", "3"));
    }

    @Ignore
    @Test
    public void itNotSortElementsByDefault() throws Exception {
        // act
        list.add("B");
        list.add("A");
        list.add("C");

        // assert
        String result = "";
        for (String element: list) {
            result += element;
        }
        assertThat("it not sort elements from BAC to ABC or CBA", result, is(not(oneOf("ABC", "CBA"))));
    }

    @Test
    public void itCanHaveDuplicateElement() throws Exception {
        // act
        list.add("fake");
        list.add("fake");

        // assert
        assertThat("it should have 2 elements", list.size(), is(2));
        assertThat("its first element is same as second", list.get(0), is(list.get(1)));
        assertThat("its first element is same instance as second", list.get(0), sameInstance(list.get(1)));
    }

    @Test
    public void itCanHaveNullElement() throws Exception {
        // act
        list.add(null);

        // assert
        assertThat("it should have 1 elements", list.size(), is(1));
        assertThat("it has null element", list.get(0), is(nullValue()));
    }
}
$ java LinkedListTest
Does LinkedList have ordered elements? true
Does LinkedList sort elements by default? false
Can LinkedList has duplicate elements? true
Can LinkedList has Null element? true

results matching ""

    No results matching ""