Java functional programming (8): string and method reference

Chapter 3 string, comparator and filter

Some methods introduced by JDK are very helpful for writing functional code. We are already familiar with some classes and interfaces in the JDK library, such as string. In order to get rid of the old style we used to, we have to take the initiative to find opportunities to use these new methods. Similarly, when we need to use an anonymous inner class with only one method, we can now replace it with a lambda expression instead of writing it as cumbersome as before.

In this chapter, we will use lambda expressions and method references to traverse strings, implement the comparator interface, view files in directories, and monitor changes to files and directories. Some methods introduced in the previous chapter will continue to appear here to help us better complete these tasks. These new technologies you've learned will help make tedious code concise, not only fast to implement, but also easy to maintain.

Traversal string

The chars () method is a new method in the string class, which is part of the charsequence interface. If you want to quickly traverse the character sequence of string, it is a very useful tool. With this internal iterator, we can easily manipulate each character in the string. Try using it to process a string first. Here, by the way, several ways of using method references are introduced.

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
分享
二维码
< <上一篇
下一篇>>