Commons-lang record

Source: Internet
Author: User

Commons-lang record:
One of the most commonly used tools, as a supplement to jdk, it is necessary to take a look at the source code ~~

Chapter 1 -- package: lang .*
There are many Utils classes in Lang. * and several static methods are provided for calling, covering string operations, character operations, JVM interactive operations, classification, exception and bit field verification.

First look at the string processing class StringUtils
All static methods, StringUtils inherited from Object. Is a null safe operation. Null safe is a string object that is null. quietly is also used to process it, without the NullPointerException exception. If this exception exists, it is a bug of this type ~~

Let's take a look at the comparison of StringUtils methods.

Compare the isEmpty and isBlank methods. IsEmpty returns false when determining a string like "", while isBlank returns true. This is obvious. empty is only valid for null and "", while blank is also valid ~~

The Clean method and trim method both encapsulate the String trim method. The only difference is that clean processes null as "", while trim processes null as null.

A series of methods starting with Strip can basically be seen as extensions of the trim method, but more powerful is the ability to process various stripChars in the front and back, not limited to blank spaces.

The Equal series methods are added with null judgment. If all the methods are null, true is returned.

IndexOf is similar to the indexOf String. It adds null judgment and adds an ordinalIndexOf method to locate the position where the nth String appears. There is also an indexOfIgnoreCase that can be used to determine location matching regardless of Case sensitivity. The actual code calls the string regionMatches method, but selects true on the ignorecase parameter.

The implementation of Contains is the same, but the null judgment is added. The remaining indexOfAny and containsAny methods are both good, And the set matching is completed ~~ It contains the parameters of a character array. It can detect whether the string contains any element in the character array. The algorithm has no special features. The complexity is O (MN ~~ In comparison, I think the containsOnly and containsNone OF THE contains series are more useful.

Methods of the Substring series, in addition to the substring encapsulation of the string substring to complete the null safe check, the left, right, and mid methods are also added. As the name suggests, left can obtain several leftmost characters as strings. In fact, str is called. substring (0, len); right also calls str. substring (str. length ()-len); there are also some substringBefore and substringAfter, and the usage is similar. SubstringBetween can be used to locate the substring between two strings.

Split is a noteworthy change. Originally, a large number of string encapsulation methods were used. split is an exception. As you know, the string split method uses a regular expression. Although it is powerful, but sometimes it is prone to errors. The simplified version is not provided for string. The split provided by StringUtils changes this situation and starts to use the complete string as the parameter rather than regex. In addition, for StringTokenizer of jdk versions with similar functions, the internal method splitWorker has a section comment: Direct code is quicker than StringTokenizer. That is to say, this is a faster tool ~~

For the reverse join Operation of split, A StrBuilder class under the lang. text package is used. Append several object arrays to the buffer. Then the last toString is good.

Delete and remove can be used to Delete the content in the string. For example, deleteSpaces can remove all spaces in the string ("" t "r" n "B"); remove is more powerful, removeStart and removeEnd are supported. Of course, remove can delete any string in the string.

Replace. Similarly, replace here is not based on regular expressions like string, which is easier to use. ReplaceOnce and replaceEach. There is also an auxiliary overlay method that can be replaced directly ~~

Chomp and chop I think it is a better function. Is this function necessary to remove the last character or tail string of a string?

Repeat copies existing strings n times.

Pad is an interesting method. It can expand an existing string by n characters and fill in specific characters. For example, StringUtils. rightPad ("bat", 5, z) = "batzz ". Pad calls the concat method of string.

There is no need to talk about Case conversion operations. Both upper and lower are the same. In addition, the capitalize series methods are very considerate.

Add countMatches, a method that is easy to misunderstand, and record the number of times a sub string appears in str. Why is it easy to misunderstand how many "aa" are there in "aaaa? The answer obtained using this method is 2 ~~~ Everyone understands

Is a series of methods are very powerful, can judge whether the string conforms to a certain format, such as isAlpha to determine whether all are letters, isNumeric to determine whether all are numbers and so on.

After the Reverse function was launched, the first thing that came to mind was that all the tests, such as a pile of text-return string flipped at the time of the first test interview, had to be completed.

I think the Abbreviate method is a very practical method encapsulation, which is common in various applications ......" Is the best application of this method.

The Difference method returns different parts of two strings. It can be said that it returns the substring starting from the first different position of the second string. IndexOfDifference returns different locations.

The getCommonPrefix method is also good. You can find the public prefix of a group of strings. Of course, only the indexOfDifference method is called.

Then there is ArrayUtils.
ArrayUtils is a class that performs special processing on arrays. Of course, the Arrays in jdk have some functions, and Array also provides some methods for dynamic access to java Arrays. The ArrayUtils extension here provides more functions.

The first method is toMap, which converts a two-dimensional array into a HashMap. Of course, the input parameters are strictly required and various exceptions are thrown. The NullToEmpty method is a representative of a defensive programming method. The null array is directly changed to an empty array (with a length of 0. The Subarray method provides a copy-based sub-array generation method. The Reverse method provides an array flip algorithm. The indexOf method is a linear array lookup Method for a specified object. It also provides a series of binning methods (toPrimitive and toObject), that is, to convert an object type such as Integer to int, and vice versa. The addAll method provides array merging Based on Array copying, that is, merging array 1 and array 2 into a new array to return. Of course, although the add method only adds one element, it also needs to copy arrays (array efficiency !!!). In the same principle, the remove (delete) method is also based on the array copy. It defines the elements to be deleted and copies the sub-arrays before and after it twice.

Then, I want to add something.
Extract some interesting and possibly useful interface methods.

The RandomStringUtils class has a random method that can generate a random string with a fixed length. Java. util. Random is used. The comment mentioned the processing method of surrogate that cannot be processed in Unicode. If unfortunately random to that location (D800-DBFF, DC00-DFFF), the algorithm will supplement count to provide a chance to re-random.

Another interesting class is StopWatch, which is a StopWatch class. The start method is used to start timing and the split method is used to cut off the timing of each segment. The suspend method can pause the StopWatch and resume the timing of resume. After the last stop, you can get the total time through getTime. Of course, you can use getSplitTime to obtain the segment timing after split. In terms of technical implementation, several States are defined, and the timing effect is expressed by the difference between each state transition and the system time.

References:

Html> http://commons.apache.org/lang/userguide.html

 

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.