Introduction to Java programming commons Lang component

With Java The functions of Lang package are similar. The common Lang API also provides some basic and general operations and processing, such as automatically generating the results of tostring(), automatically implementing hashcode() and equals() methods, array operations, enumeration, date and time processing, etc.

All package names for this set of APIs are defined as org apache. commons. Lang starts with the following 8 packages:

org. apache. commons. lang

org. apache. commons. lang.builder

org. apache. commons. lang.enum

org. apache. commons. lang.enums

org. apache. commons. lang.exception

org. apache. commons. lang.math

org. apache. commons. lang.mutable

org. apache. commons. lang.time

Lang.enum is no longer recommended. Instead, it is the lang.enums package that follows. Lang packages are mainly util classes that can be highly reused; The lang. builder package contains a set of constructors for generating toString (), hashcode (), equals (), CompareTo () and other methods commonly used in each Java class; The lang.enums package is used to handle enumerations as the name suggests; Lang.exception package is used to handle exceptions in Java standard API, and provides nested exception function for versions before 1.4; The lang.math package is used to process numbers; Lang.mutable is used to wrap value type variables; The lang.time package provides the ability to process dates and times.

Since there are many packages and classes in commons, it is impossible to talk about them one by one. In the next special article, I will only talk about the packages and common usages of Lang, lang.builder, lang.math and lang.time respectively. For others, we can temporarily refer to Javadoc when we use them. The location is at the of the installation path

…\commons-lang-2.1\docs\api\index. html

Let's start with org apache. commons. Lang package, which provides some useful util classes containing static methods. In addition to 6 exception classes and 2 numeric classes that have been deprecated, commons The Lang package contains 17 practical classes:

Arrayutils C is used for array operations, such as add, find, delete, subarray, reverse order, element type conversion, etc;

Bitfield C is used to operate bits and provides some convenient and safe methods;

Boolean utils C is used to operate and convert Boolean or Boolean and corresponding arrays;

Charencoding C contains the character encoding supported by Java environment, and provides the judgment of whether a certain encoding is supported;

Charrange C is used to set the character range and check accordingly;

Charset C is used to set a group of characters as the range and check accordingly;

Charsetutils C is used to operate charset;

Charutils C is used to manipulate char values and character objects;

Classutils C is used to operate Java classes without reflection;

Objectutils C is used to operate Java objects and provide null safe access and other functions;

Randomstringutils C is used to generate random strings;

Serializationutils C is used to process object serialization and provides higher processing power than general Java serialization;

Stringescapeutils C is used to correctly handle escape characters and generate correct Java, JavaScript, HTML, XML and SQL codes;

Stringutils C handles the core class of string and provides quite a lot of functions;

Systemutils C in Java Provide more convenient access based on lang. system, such as user path, java version, time zone, operating system, etc;

Validate C provides validation operations, which are somewhat similar to assert assertions;

Wordutils C is used to handle word case, line breaks, and so on.

Here are the two most commonly used classes

ArrayUtils

Array is a data structure that we often need to use, but because Java itself does not provide good API support, many operations are actually quite cumbersome, so that we even use the collections API at the expense of performance in actual coding. Of course, using collection can easily solve our problems, But do we have to pay for performance? Arrayutils helped us solve most of the problems in dealing with similar situations. Let's take an example:

Here are the running results: intraarray1: {2,16} intraarray2: {1,2}, {2,4}, {3,8}, {4,16} notamap: {a, 100.0}, {B, 80.0}, {C, 60.0}, {D, 40.0}, {e, 20.0} intraarray1 contains' 8 '? true intArray1 index of '8'? 2 intArray1 last index of '8'? 2 intArray3: {2,16} intArray3 reversed: {16,2} integerArray1: {2,16} get 'C' from map: 60.0

The following are the running results:

intArray1: {2,16}

intArray2: {{1,16}}

notAMap: {{A,20.0}}

intArray1 contains '8'? true

intArray1 index of '8'? two

intArray1 last index of '8'? two

intArray3: {2,16}

intArray3 reversed: {16,2}

integerArray1: {2,16}

get 'C' from map: 60.0

This code shows how we can easily use the arrayutils class to help us print, find, clone, reverse order, and convert between value type / object arrays.

StringUtils

Text processing should be a routine for Java applications. Before 1.4, the APIs provided by Java itself were very limited, such as string, stringtokenizer and StringBuffer, and the operation was relatively simple. Nothing more than finding substrings, decomposing, merging, and so on. With the advent of 1.4, it can be said that Java word processing has reached a new level because it supports regular expression. This is a heavyweight and convenient thing. The disadvantage is that it is too complex and difficult to learn. In contrast, the stringutils and wordutils provided by Jakarta commons still maintain the simple and powerful beauty and are easy to use. Let's take an example:

The output results are as follows: ======================================== is STR1 blank? true Is str2 blank? true Is str3 blank? true Is str4 blank? true ============================== Is str5 numeric? true Is str6 numeric? false ============================== str6: ABCDEFG str6 reversed: GFEDCBA str7: It feels good to use Jakarta Commons. str7 reversed whole words : Commons. Jakarta use to good feels It ============================== print header: ================================================== %%%%%%%%%%%%%%% Customised Header %%%%%%%%%%%%%%%% ==================================================

The output results are as follows:

==============================

Is str1 blank? true

Is str2 blank? true

Is str3 blank? true

Is str4 blank? true

==============================

Is str5 numeric? true

Is str6 numeric? false

==============================

str6: ABCDEFG

str6 reversed: GFEDCBA

str7: It feels good to use Jakarta Commons.

str7 reversed whole words :

Commons. Jakarta use to good feels It

==============================

print header:

==================================================

%%%%%%%%%%%%%%% Customised Header %%%%%%%%%%%%%%%%

==================================================

From the code, we can roughly understand the simple and powerful processing power of this stringutils class. From checking empty strings (handling null cases appropriately), to splitting substrings, to generating formatted strings, the use is very concise and straightforward.

summary

The above is the whole content of this article. I hope it will be helpful to you.

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