1 | package testcode;
|
---|
2 |
|
---|
3 | import java.util.ArrayList;
|
---|
4 | import java.util.Arrays;
|
---|
5 | import java.util.HashSet;
|
---|
6 | import java.util.List;
|
---|
7 | import java.util.Set;
|
---|
8 |
|
---|
9 | import org.eclipse.jdt.annotation.NonNull;
|
---|
10 |
|
---|
11 | /**
|
---|
12 | * Test various remove, removeall, etc.
|
---|
13 | */
|
---|
14 | public class Remove {
|
---|
15 |
|
---|
16 | static public void main(String[] args) {
|
---|
17 | testRemoveFromList();
|
---|
18 | testRemoveFromSet();
|
---|
19 | }
|
---|
20 |
|
---|
21 | private static void testRemoveFromList() {
|
---|
22 | final @NonNull List<@NonNull String> lst = new ArrayList<>(
|
---|
23 | Arrays.asList("a", "b", "c", "b"));
|
---|
24 | boolean result = lst.remove("b");
|
---|
25 | if (!result)
|
---|
26 | throw new IllegalStateException("removing b should have succeeded");
|
---|
27 | result = lst.remove("b");
|
---|
28 | if (!result)
|
---|
29 | throw new IllegalStateException("removing b should have succeeded");
|
---|
30 | result = lst.remove("b");
|
---|
31 | if (result)
|
---|
32 | throw new IllegalStateException("removing b should have failed");
|
---|
33 | System.out.println("ok0");
|
---|
34 | }
|
---|
35 |
|
---|
36 | private static void testRemoveFromSet() {
|
---|
37 | final @NonNull Set<@NonNull String> st = new HashSet<>(
|
---|
38 | Arrays.asList("a", "b", "c", "b"));
|
---|
39 | // note, duplicate b, one will be removed as this is a set.
|
---|
40 | boolean result = st.remove("b");
|
---|
41 | if (!result)
|
---|
42 | throw new IllegalStateException("removing b should have succeeded");
|
---|
43 | result = st.remove("b");
|
---|
44 | if (result)
|
---|
45 | throw new IllegalStateException("removing b should have failed");
|
---|
46 | System.out.println("ok1");
|
---|
47 | }
|
---|
48 |
|
---|
49 | }
|
---|