How does Java delete eligible data from list
- 2021-12-12 08:36:30
- OfStack
Delete eligible data from list
In the use of Java language, we often encounter the need to remove some data from list, but beginners often encounter the following pits:
Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 5, Size: 4
at java.util.ArrayList.rangeCheck(Unknown Source)
at java.util.ArrayList.get(Unknown Source)
at Main.remove3(Main.java:44)
at Main.main(Main.java:18)
So in this summary 1, there are 1 methods to delete data from list:
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class Main {
public static void main(String[] args) {
ArrayList<String> strs = new ArrayList<>();
strs.add("1");
strs.add("32");
strs.add("3");
strs.add("4");
strs.add("5");
strs.add("36");
// remove1(strs);
// remove2(strs);
// remove3(strs);
remove4(strs);
System.out.println("after");
printList(strs);
}
// Use iterator This is java And Android Often used in source code 1 Method, so it is most recommended
public static void remove1(List<String> list) {
Iterator<String> sListIterator = list.iterator();
while (sListIterator.hasNext()) {
String str = sListIterator.next();
if (str.contains("3")) {
sListIterator.remove();
}
}
}
// Delete in reverse order to prevent data subscripts from changing due to deleting intermediate items, which makes data error
public static void remove2(List<String> list) {
for (int i = list.size() - 1; i >= 0; i--) {
if (list.get(i).contains("3")) {
list.remove(i);
}
}
}
// Sequential deletion, but the subscript and index are processed
public static void remove3(List<String> list) {
for (int i = 0, len = list.size(); i < len; i++) {
if (list.get(i).contains("3")) {
list.remove(i);
len--;
i--;
}
}
}
// Do not directly manipulate the original during traversal list
public static void remove4(List<String> list) {
List<String> temp = new ArrayList<>();
for (String str : list) {
if (str.contains("3")) {
temp.add(str);
}
}
list.removeAll(temp);
}
public static void printList(List<String> list) {
for (String str : list) {
System.out.println(str);
}
}
}
If there are other good ways to supplement.
Delete a specific element in list
When the database queried out the data, found that some data is not needed, so here to deal with 1, add a loop list, according to the judgment conditions to delete, there will be an exception.
So the new feature of java8 is used here to delete it.
List<UserFictitious> list = new ArrayList<>();
for (int i = 0; i < 50; i++){
UserFictitious userFictitious = new UserFictitious();
userFictitious.setUserFictitiousName(String.valueOf(i));
list.add(userFictitious);
}
list.removeIf(item -> item.getUserFictitiousName().equals("1"));
System.out.println(list);
The underlying source code is deleted according to the characteristics of the iterator, and java8 is only encapsulated into removeIf, simplifying the code.
default boolean removeIf(Predicate<? super E> filter) {
Objects.requireNonNull(filter);
boolean removed = false;
final Iterator<E> each = iterator();
while (each.hasNext()) {
if (filter.test(each.next())) {
each.remove();
removed = true;
}
}
return removed;
}