I remember that during my previous job search, I encountered an interviewer who asked me a very basic question. The question is: There are 10 elements in a List, and I want to delete 3 elements from it now. How do I do it? I didn't think about it at the time, so I just said that List has its own remove method, which can be used directly. He said, please explain it in detail. I said that I would like to write a for loop, the number of loops is the length of the List, and then directly delete the elements you want to delete in the loop.
At that time, I thought, and asked such a simple question. The interviewer said, "You can try it yourself when you go back. See if you will report an error if you write as you said. Then I was confused. Although this was a simple question, I had never paid attention to this small detail in daily coding, and the interview results can be imagined.
After I went back, I tried it once and it really reported an error. It turned out that the List operation was not modified during the traversal, whether it was deleted or added, because if new elements were added to the collection during the traversal, it would cause a dead loop. Also, if elements were deleted during the traversal, it would cause problems such as crossing the bounds of the following table in the traversal. The general operation method is the function implemented through the addAll method and the removeAll method.
For example, the following
@Testpublic void myTestLearnMore(){ List<String> testList = new ArrayList<>(); testList.add("1 Yang"); testList.add("1 Li"); testList.add("1 Wang"); testList.add("1 Zhang"); testList.add("2 Yang"); testList.add("2 Sun"); testList.add("2 Zhao"); List<String> temAddList = new ArrayList<>(); for(String test : testList) { if(test.startsWith("1")) { temAddList.add(test); } } testList.removeAll(temAddList); System.out.println(JSON.toJSONString(testList));}The printing result is: ["2 Yang", "2 Suns", "2 Zhao"]
This is the real way to operate. However, what I want to talk about today is actually the new collection method of Java 8. For example, first create a temporary collection and then put the elements that need to be removed into the temporary collection through traversal, and finally delete them from the original collection as a whole. In this way, you need to write five or six lines of code, and you can do it with one line of code in Java 8. This is the following line of code:
testList.removeIf(test->test.startsWith("1"));The meaning of this code is to remove elements that conform to the removeIf parameter format, so if you print the testList after this line of code, you will not print out elements starting with 1.
These small details are actually accumulated in the daily coding process. If there are too many pitfalls, you will pay attention to it when writing it later. Just like when using equals in Java, you always put known constants in front of equals to prevent null pointer exceptions. When using lambda expressions for collections, you must first judge whether the collection is null through Objects.nonNull(). When printing objects, do not directly call the object's toString() method. You must pass the object toString method of Objects, so that even if the object is null, you can print it out. Objects class is a new tool class added to Java7.
Summarize
The above is the newly added method of java8 in the Collection introduced by the editor to removeIf. I hope it will be helpful to everyone. If you have any questions, please leave me a message. The editor will reply to everyone in time!