Java8 adds a new method removeif in the collection

I remember in my previous job search experience, I met an interviewer who asked me a very basic question. The question is: there are 10 elements in a list. Now I want to delete 3 elements from it. What can I do? I didn't think about it at that time. I just said that list has its own remove method, which can be used directly. He said, please explain in detail. I said to write a for loop, the number of cycles is the length of list, and then directly delete the elements you want to delete in the loop.

At that time, I also thought that I would ask such a simple question. The interviewer said, you can go back and try it yourself. Do you think it will be wrong to write as you said. Then I was confused. Although this is a simple question, I really didn't pay attention to this small detail in daily coding, and then the interview results can be imagined.

After I went back, I really tried once and reported an error. It turned out that the list operation was not modified during traversal, whether it was deleted or added, because if new elements were added to the collection all the time during traversal, it would cause an endless loop. In addition, if elements were deleted during traversal, it would cause problems such as out of bounds in the table below the array. The general operation mode is realized through the addall method and removeAll method.

For example, the following

The printing result is: [2 Yang "," 2 sun "," 2 Zhao]

This is the real way of operation. But what I want to say today is that the new collection method in Java 8, like the above, first creates a temporary collection, then puts the elements to be removed into the temporary collection through traversal, and finally deletes them from the original collection as a whole. In this way, you need to write five or six lines of code, which can be done with one line of code in Java 8. This is the following line of code:

This code means to remove the elements in the format of the removeif parameter, so if you print the testlist after this line of code, the elements starting with 1 will not be printed.

In fact, these small details are accumulated in the daily coding process. There are many pits encountered, and you will pay attention to them when writing later. For example, when using equals in Java, the known constants are always placed in front of equals to prevent null pointer exceptions. When using lambda expressions in the collection, you should use objects Nonnull() first determines whether the collection is null. When printing an object, do not directly call the tostring() method of the object, but pass the object to the toString method of objects, so that even if the object is null, it can be printed. Objects is a new tool class in Java 7.

Original text from: java8's newly added method removeif in the collection

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>