1. Delete the specified element (four methods)
(1) Enhanced for (avoid exceptions)
//If you delete an element that can be divided into 3ArrayList<Integer> list = new ArrayList<>((1, 2, 3, 4, 5,6,7,8,9,10)); //Before deletion:(list); for (Integer integer : new ArrayList<>(list)) { if (integer % 3 == 0){ (integer); } } //After deletion:(list);
result:
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
[1, 2, 4, 5, 7, 8, 10]
Notice:When deleting elements in the above code, I created a copy of the collection object when traversing, traversing the copy, and deleting the original array. This is a way. I can't directly traverse the list and delete it, so the following error will appear:
D:\java\jdk1.8.0_321\bin\ "-javaagent:D:\softlocation\idea 2019\IntelliJ IDEA 2019.3.1\lib\idea_rt.jar=51701:D:\softlocation\idea 2019\IntelliJ IDEA 2019.3.1\bin" -=UTF-8 -classpath [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] Exception in thread "main" at $(:911) at $(:861) at (:49)
It is a runtime exception in Java. It occurs when we try to modify a collection object in a single-threaded environment, while another thread is also modifying the same collection object.
To avoid this exception,We have the following solutions:
1. In a single-threaded environment, use the remove() method of Iterator to delete elements instead of directly modifying the collection object.
2. In a multi-threaded environment, use thread-safe collection classes, such as ConcurrentHashMap, CopyOnWriteArrayList, etc.
3. In a single-threaded environment, create a copy of the collection object and modify the copy.
4. Use Java 8's Lambda expressions and stream operations instead of explicitly using iterators.
(2) Iterator
//If you delete an element that can be divided into 3ArrayList<Integer> list = new ArrayList<>((1, 2, 3, 4, 5,6,7,8,9,10)); //Before deletion:(list); Iterator<Integer> iterator = (); while (()){ Integer next = (); if (next % 3 ==0){ (); } } //After deletion:(list);
result:
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
[1, 2, 4, 5, 7, 8, 10]
(3) stream
//If you delete an element that can be divided into 3ArrayList<Integer> list = new ArrayList<>((1, 2, 3, 4, 5,6,7,8,9,10)); //Before deletion:(list); List<Integer> integers = ().filter(e -> e % 3 != 0).collect(()); for (Integer integer : new ArrayList<>(list)) { if (integer % 3 == 0){ (integer); } } //After deletion:(integers);
result:
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
[1, 2, 4, 5, 7, 8, 10]
(4)removeIf()
//If you delete an element that can be divided into 3ArrayList<Integer> list = new ArrayList<>((1, 2, 3, 4, 5,6,7,8,9,10)); //Before deletion:(list); (integer -> integer % 3 == 0); //After deletion:(list);
result:
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
[1, 2, 4, 5, 7, 8, 10]
2. Remove the element containing list1 from list2
(1) for loop
Pay attention to the inability to operate the same data by traversing and deleting to avoid problems caused by concurrent modifications.
ArrayList<Integer> list1 = new ArrayList<>((1,2,3,6)); ArrayList<Integer> list2 = new ArrayList<>((1,2,3,1,2,3,4,5)); //Before deletion("list1 element:" + list1); ("list2 before deletion:" + list2); for (Integer integer : new ArrayList<>(list2)) { for (Integer integer1 : new ArrayList<>(list1)) { if ((integer, integer1)){ (integer1); } }
result:
list1 element: [1, 2, 3, 6]
Before list2 is deleted: [1, 2, 3, 1, 2, 3, 4, 5]
After list2 is deleted: [4, 5]
(2)removeAll()
ArrayList<Integer> list1 = new ArrayList<>((1,2,3,6)); ArrayList<Integer> list2 = new ArrayList<>((1,2,3,1,2,3,4,5)); //Before deletion("list1 element:" + list1); ("list2 before deletion:" + list2); (list1); ("list2 deleted:" + list2);
result:
list1 element: [1, 2, 3, 6]
Before list2 is deleted: [1, 2, 3, 1, 2, 3, 4, 5]
After list2 is deleted: [4, 5]
(3)removeIf()
ArrayList<Integer> list1 = new ArrayList<>((1,2,3,6)); ArrayList<Integer> list2 = new ArrayList<>((1,2,3,1,2,3,4,5)); //Before deletion("list1 element:" + list1); ("list2 before deletion:" + list2); (list1); (list1::contains); ("list2 deleted:" + list2);
result:
list1 element: [1, 2, 3, 6]
Before list2 is deleted: [1, 2, 3, 1, 2, 3, 4, 5]
After list2 is deleted: [4, 5]
(4) stream
Use Java 8 and above Stream API: First use stream() to get the stream, then use filter to filter out unnecessary elements, and finally use collect method to collect the results.
ArrayList<Integer> list1 = new ArrayList<>((1,2,3,6)); ArrayList<Integer> list2 = new ArrayList<>((1,2,3,1,2,3,4,5)); //Before deletion("list1 element:" + list1); ("list2 before deletion:" + list2); List<Integer> collect = ().filter(e -> !(e)).collect(()); ("list2 deleted:" + collect);
result:
list1 element: [1, 2, 3, 6]
Before list2 is deleted: [1, 2, 3, 1, 2, 3, 4, 5]
After list2 is deleted: [4, 5]
3. Delete the specified subscript element
(1) Remove(index) method
This is a method provided by the List interface, which can directly delete elements at the specified subscript position. Using this method, you need to pass the subscript of the element to be deleted.
ArrayList<Integer> list = new ArrayList<>((1,2,3,1,2,3,4,5)); ("list before deletion:" + list); //Delete the element with list subscript 1(1); ("After list deleted:" + list);
result:
Before list deletion: [1, 2, 3, 1, 2, 3, 4, 5]
After list deleted: [1, 3, 1, 2, 3, 4, 5]
(2) subList(int fromIndex,int toIndex) method
For example: Delete the elements from Tables 1 to 3 below: This method returns a sublist of the source list, containing all elements from fromIndex (included) to toIndex (not included). If fromIndex and toIndex are the same, an empty sublist is returned.
ArrayList<Integer> list = new ArrayList<>((1,2,3,1,2,3,4,5)); ("list before deletion:" + list); //Delete elements with subscripts of 1 to 3(1,3).clear(); ("After list deleted:" + list);
result:
Before list deletion: [1, 2, 3, 1, 2, 3, 4, 5]
After list delete: [1, 1, 2, 3, 4, 5]
Summarize
This is the article about the operation guide for removing elements of Java list. For more information about removing elements of Java list, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!