Detail the new addition of java8 to Collection

  • 2021-01-02 21:53:33
  • OfStack

I remember in my previous job hunting experience, An interviewer once asked me a very basic question. The question is: There are 10 elements in one List, and I want to remove 3 elements from it now. How do I do that? I didn't think about it at that time, so I just said that List has its own remove method, which can be used directly. He said, please explain in detail 1. I said to write an for loop, the number of loops is the length of List, and then delete the elements in the loop directly.

At that time, I thought, such a simple question is also asked, the interviewer said, you go back to try to find out, you see if you can write as you said error. And Then I was like, this is a simple question, but I haven't really noticed this little detail in everyday coding, and you can imagine the interview results.

I go back after I really tried 1 times also really complains, the original is wrong in the process of traversal List operation changes, whether delete and add, because if in traverse 1 straight to set new elements, can cause death cycle, and if in the process of traversing the delete elements, can cause an array cross-border issues such as listed in the table below. The 1-like mode of operation is achieved through the addAll method and removeAll method.

Take the following example


@Test
public void myTestLearnMore()
{
 List<String> testList = new ArrayList<>();
 testList.add("1 Yang ");
 testList.add("1 li ");
 testList.add("1 The king ");
 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 print result is: [" 2 Yang ", "2 Sun", "2 Zhao"]

That's really how it works. But what I'm going to talk about today is Java8's new collection methods, such as above, which first create a temporary collection, then iterate over the elements to be removed, and finally delete them from the original collection as a whole. That's 56 lines of code, and one line in java8. Here is the following line of code:


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

This code removes elements that conform to the removeIf parameter format, so printing testList after this line of code does not print elements that begin with a 1.

These little details are accumulated in daily life during encoding, and much of the pit, will write later will pay attention to, like when using equals java, has always been known to put front equals constants, prevent a null pointer exception, in the collection using lambda expressions, are through Objects. nonNull () to figure out whether collection null first, at the time of print objects don't directly call the object's toString () method, to pass objects to Objects toString method, So you can print it even if the object is an null. The class Objects is a new tool class added to java7.

conclusion

Above is the site to introduce java8 in Collection new added method removeIf, hope to help you, if you have any questions welcome to give me a message, this site will reply you in time!


Related articles: