Detailed description of the newly added methods removeIf and java8removeif in Java 8 Collection

Source: Internet
Author: User

Detailed description of the newly added methods removeIf and java8removeif in Java 8 Collection

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

At that time, I also thought that the interviewer asked such a simple question. The interviewer said, you will know if you will report an error when you go back and try it. Then I did. Although this is a simple problem, I did not pay attention to this small detail in the daily coding. Then I can imagine the interview results.

After I went back, I tried it once and reported an error. The List operation was not modified in the traversal process, whether it was delete or add, if an element is added directly to the set during traversal, it will lead to an infinite loop. If an element is deleted during the traversal process, the following table of the array will be out of bounds. The general operation method is implemented through the addAll method and removeAll method.

For example

@ Testpublic void myTestLearnMore () {List <String> testList = new ArrayList <> (); testList. add ("1 Yang"); testList. add ("1 Li"); testList. add ("1 King"); testList. add ("1 piece"); 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 printed result is: ["2 Yang", "2 sun", "2 Zhao"]

This is the real operation method. But what I want to talk about today is actually the newly added Set Method of Java 8. For example, create a temporary set first and then traverse it to put the elements to be removed into the temporary set, finally, the entire object is deleted from the original set. In this way, five or six lines of code can be written, and a line of code can be used in Java 8. This is the following line of code:

testList.removeIf(test->test.startsWith("1"));

This Code removes elements that conform to the removeIf format. Therefore, testList is printed after this line of code, and no elements starting with 1 are printed.

These small details are actually accumulated in the daily coding process, and many pitfalls are encountered, so you will pay attention to them later, just like when using equals in java, A known constant is always placed before equals to prevent null pointer exceptions. When using a lambda expression in a set, Objects is used. nonNull () first checks whether the set is null. When printing an object, do not directly call the toString () method of the object. Instead, pass the object to the toString method of Objects, in this way, even if the object is null, it can be printed. Objects is a new tool class in Java 7.

Summary

The above is the newly added removeIf method of Java 8 in Collection introduced by xiaobian. I hope it will be helpful to you. If you have any questions, please leave a message and I will reply to you in time!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.