With java. the role of the lang Package is similar. Commons Lang APIS also provide some basic and common operations and processing, such as automatically generating toString () results and automatically implementing hashCode () and equals () methods, Array Operations, enumeration, processing of dates and times, and so on.
All the package names of this group of Apis start with org. apache. commons. lang. There are eight packages in total:
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
The lang. enum is no longer recommended. Instead, it is followed by the lang. enums package. Lang packages are mainly Util classes that can be highly reused; lang. the builder package contains a set of constructors used to generate methods commonly used in each Java class, such as toString (), hashCode (), equals (), and compareTo (). lang. the enums package is used for processing enumeration; lang. the exception package is used to handle exceptions in Java standard APIs and provides the Nested exception function for earlier versions than 1.4. lang. the math package is used to process numbers; lang. mutable is used to wrap value variables; lang. the time package provides the date and time processing function.
Since there are many commons packages and classes, it is impossible to talk about them one by one. In the next topic article, I will only talk about Lang and Lang respectively. builder, Lang. math and Lang. time packages and common usage. For other packages, We can temporarily refer to javadoc. The location is in the installation path
... // Commons-lang-2.1/docs/API/index.html
Let's take a look at the org. Apache. commons. lang package. This package provides some useful util classes that contain static methods. In addition to 6 exception classes and 2 deprecated numeric classes, the Commons. lang package contains 17 practical classes:
Arrayutils-used for Array operations, such as addition, search, deletion, subarray, reverse order, and element type conversion;
Bitfield-it is used to operate bitwise AND provides some convenient and secure methods;
Booleanutils-used to operate and convert Boolean OR Boolean and corresponding arrays;
Charencoding-contains the character encoding supported by the Java environment and provides whether certain encoding is supported;
Charrange-used to set the character range and check accordingly;
Charset-used to set a group of characters as the range and check accordingly;
Charsetutils-used to operate charset;
Charutils-used to operate char values and character objects;
Classutils-used for Java class operations without reflection;
Objectutils-it is used to operate Java objects and provides null-safe access and other functions;
Randomstringutils-used to generate random strings;
SerializationUtils-used to process Object serialization, providing more advanced processing capabilities than general Java serialization;
StringEscapeUtils-used to correctly process escape characters and generate correct Java, JavaScript, HTML, XML, and SQL code;
StringUtils-core class for String processing, providing a considerable number of functions;
SystemUtils-provides more convenient access based on java. lang. System, such as user path, Java version, time zone, and operating System judgment;
Validate-Provides verification operations, a bit similar to assert assertions;
WordUtils-used to process case and line breaks.
The two most common classes are described below.
ArrayUtils
Arrays are a type of data structure that we often need to use. However, because Java itself does not provide good API support, many operations are actually complicated, so that we will even use the Collections API at the cost of performance in actual encoding. Of course, using Collections can easily solve our problem, but must we take the performance as the cost? ArrayUtils helped us solve most of the problems in similar situations. Let's look at an example:
Package Sean. study. jakarta. commons. lang; </P> <p> Import Java. util. map; </P> <p> Import Org. apache. commons. lang. arrayutils; </P> <p> public class arrayutilsusage {</P> <p> Public static void main (string [] ARGs) {</P> <p>/Data setup </P> <p> int [] intarray1 = {2, 4, 8, 16 }; </P> <p> int [] [] intarray2 = {1, 2}, {2, 4}, {3, 8}, {4, 16 }}; </P> <p> object [] [] notamap = {</P> <p> {"A", new double (100 )}, </P> <p> {"B", new double (80) },</P> <p> {"C", new double (60 )}, </P> <p> {"D", new double (40) },</P> <p> {"E", new double (20 )} </P> <p >}; </P> <p> // printing arrays </P> <p> system. out. println ("intarray1:" + arrayutils. tostring (intarray1); </P> <p> system. out. println ("intarray2:" + arrayutils. tostring (intarray2); </P> <p> system. out. println ("notamap:" + arrayutils. tostring (notamap); </P> <p> // fi Nding items </P> <p> system. Out. println ("intarray1 contains '8 '? "</P> <p> + arrayutils. Contains (intarray1, 8); </P> <p> system. Out. println (" intarray1 index of '8 '? "</P> <p> + arrayutils. indexof (intarray1, 8); </P> <p> system. out. println ("intarray1 last index of '8 '? "</P> <p> + arrayutils. lastindexof (intarray1, 8); </P> <p> // cloning and resversing </P> <p> int [] intarray3 = arrayutils. clone (intarray1); </P> <p> system. out. println ("intarray3:" + arrayutils. tostring (intarray3); </P> <p> arrayutils. reverse (intarray3); </P> <p> system. out. println ("intarray3 reversed:" </P> <p> + arrayutils. tostring (intarray3); </P> <p> // primitive to object array </P> <p> integer [] intege Rarray1 = arrayutils. toobject (intarray1); </P> <p> system. out. println ("integerarray1:" </P> <p> + arrayutils. tostring (integerarray1); </P> <p> // build map from two dimensional array </P> <p> map = arrayutils. tomap (notamap); </P> <p> double res = (double) map. get ("C"); </P> <p> system. out. println ("Get 'c' from map:" + Res ); </P> <p >}</P> <p> The following is the running result: </P> <p> intarray1: {2, 4, 8, 16} </P> <p> intarray2 :{{ 1, 2}, {2, 4}, {3, 8}, {4, 16 }</P> <p> notamap: {A, 100.0}, {B, 80.0}, {C, 60.0 },{ D, 40.0 },{ E, 20.0 }}</P> <p> intarray1 contains '8 '? True </P> <p> intarray1 index of '8 '? 2 </P> <p> intarray1 last index of '8 '? 2 </P> <p> intarray3: {2, 4, 8, 16} </P> <p> intarray3 reversed: {16, 8, 4, 2} </P> <p> integerarray1: {2, 4, 8, 16} </P> <p> Get 'c' from map: 60.0
This code shows how to conveniently use the ArrayUtils class to print, search, clone, reverse order, and convert between value-type/object arrays.
StringUtils
Processing text is a common practice for Java applications. Before the advent of 1.4, Java provided very limited APIs, such as string, stringtokenizer, and stringbuffer, and the operations were relatively simple. It is nothing more than searching for substring, splitting, merging, and so on. The emergence of 1.4 can be said that Java's text processing has taken a step, because it supports regular expression. This is a heavyweight and convenient task. The disadvantage is that it is too complicated and difficult to learn. In comparison, the stringutils and wordutils provided by Jakarta commons are still simple and powerful, and they are easy to use. Let's look at an example:
Package sean. study. jakarta. commons. lang; </p> <p> import org. apache. commons. lang. stringUtils; </p> <p> public class StringUtilsAndWordUtilsUsage {</p> <p> public static void main (String [] args) {</p> <p>/data setup </p> <p> String str1 = ""; </p> <p> String str2 = ""; </p> <p> String str3 = "/t"; </p> <p> String str4 = null; </p> <p> String str5 = "123"; </p> <p> String str6 = "ABCDEFG "; </p> <p> String str7 = "It f Eels good to use Jakarta Commons. /r/n "; </p> <p> // check for empty strings </p> <p> System. out. println ("=============================== "); </p> <p> System. out. println ("Is str1 blank? "+ StringUtils. isBlank (str1); </p> <p> System. out. println (" Is str2 blank? "+ StringUtils. isBlank (str2); </p> <p> System. out. println (" Is str3 blank? "+ StringUtils. isBlank (str3); </p> <p> System. out. println (" Is str4 blank? "+ StringUtils. isBlank (str4); </p> <p> // check for numerics </p> <p> System. out. println ("=============================== "); </p> <p> System. out. println ("Is str5 numeric? "+ StringUtils. isNumeric (str5); </p> <p> System. out. println (" Is str6 numeric? "+ StringUtils. isNumeric (str6); </p> <p> // reverse strings/whole words </p> <p> System. out. println ("=============================== "); </p> <p> System. out. println ("str6:" + str6); </p> <p> System. out. println ("str6 reversed:" + StringUtils. reverse (str6); </p> <p> System. out. println ("str7:" + str7); </p> <p> String str8 = StringUtils. chomp (str7); </p> <p> str8 = StringUtils. reverseDelimited (str8, ''); </p> <P> System. out. println ("str7 reversed whole words:/r/n" + str8); </p> <p> // build header (useful to print log messages that are easy to locate) </p> <p> System. out. println ("=============================== "); </p> <p> System. out. println ("print header:"); </p> <p> String padding = StringUtils. repeat ("=", 50); </p> <p> String msg = StringUtils. center ("Customised Header", 50, "%"); </p> <p> Object [] raw = ne W Object [] {padding, msg, padding}; </p> <p> String header = StringUtils. join (raw, "/r/n"); </p> <p> System. out. println (header); </p> <p >}</p> <p> the output result is as follows: </p> <p >======================================</p> <p> Is str1 blank? True </p> <p> Is str2 blank? True </p> <p> Is str3 blank? True </p> <p> Is str4 blank? True </p> <p >======================================</ p> <p> Is str5 numeric? True </p> <p> Is str6 numeric? False </p> <p >===================================</ p> <p> str6: ABCDEFG </p> <p> str6 reversed: GFEDCBA </p> <p> str7: It feels good to use Jakarta Commons. </p> <p> str7 reversed whole words: </p> <p> Commons. jakarta use to good feels It </p> <p >========================= ===</p> <p> print header: </p> <p >========================================= ==========================</p> <p> % Customised header % </p> <p >================= ========================================
From the code, we can get a rough idea of the simple and powerful processing capabilities of this stringutils class, from checking empty strings (handling the case of null is decent) to splitting substrings, to generate formatted strings, the use is concise and straightforward.