Last active
September 6, 2017 08:01
-
-
Save uOOOO/b43213455271ba94c81abfa1ab7f764f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class RemoveSpecificElementFromListTest { | |
| private static final String DEL = "r"; | |
| private static final int EXPECTED_SIZE = 7; | |
| private List<String> stringList; | |
| @Before | |
| public void setup() { | |
| stringList = new ArrayList<>(Arrays.asList("A", "r", "r", "a", "y", "l", "i", "s", "t")); | |
| } | |
| @Test(expected = IndexOutOfBoundsException.class) | |
| public void listElementRemoveTest0() throws Exception { | |
| final int size = stringList.size(); | |
| for (int i = 0; i < size; i++) { | |
| final String s = stringList.get(i); | |
| if (DEL.equals(s)) { | |
| stringList.remove(s); | |
| } | |
| } | |
| System.out.println("size = " + stringList.size()); | |
| System.out.println("toString = " + stringList.toString()); | |
| Assert.assertNotSame(EXPECTED_SIZE, stringList.size()); | |
| } | |
| @Test | |
| public void listElementRemoveTest1() throws Exception { | |
| for (int i = 0; i < stringList.size(); i++) { | |
| final String s = stringList.get(i); | |
| if (DEL.equals(s)) { | |
| stringList.remove(s); | |
| } | |
| } | |
| System.out.println("size = " + stringList.size()); | |
| System.out.println("toString = " + stringList.toString()); | |
| Assert.assertNotSame(EXPECTED_SIZE, stringList.size()); | |
| } | |
| @Test(expected = ConcurrentModificationException.class) | |
| public void listElementRemoveTest2() throws Exception { | |
| for (String s : stringList) { | |
| if (DEL.equals(s)) { | |
| stringList.remove(s); | |
| } | |
| } | |
| System.out.println("size = " + stringList.size()); | |
| System.out.println("toString = " + stringList.toString()); | |
| Assert.assertEquals(EXPECTED_SIZE, stringList.size()); | |
| } | |
| @Test | |
| public void listElementRemoveTest3() throws Exception { | |
| for (Iterator<String> iterator = stringList.iterator(); iterator.hasNext(); ) { | |
| final String s = iterator.next(); | |
| if (DEL.equals(s)) { | |
| iterator.remove(); | |
| } | |
| } | |
| System.out.println("size = " + stringList.size()); | |
| System.out.println("toString = " + stringList.toString()); | |
| Assert.assertEquals(EXPECTED_SIZE, stringList.size()); | |
| } | |
| @Test | |
| public void listElementRemoveTest4() throws Exception { | |
| stringList.removeIf(DEL::equals); | |
| System.out.println("size = " + stringList.size()); | |
| System.out.println("toString = " + stringList.toString()); | |
| Assert.assertEquals(EXPECTED_SIZE, stringList.size()); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment