In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
Editor to share with you what are the commonly used tool libraries in the Java world, I believe most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!
Apache CommonsApache Commons has many sub-projects, and the common items are as follows:
BeanUtils
Provides a series of operations on java bean, reading and setting property values, and so on.
@ Data public class User {private String username; private String password;} User user = new User (); BeanUtils.setProperty (user, "username", "li"); BeanUtils.getProperty (user, "username")
Conversion between map and bean:
/ / bean- > map Map map = BeanUtils.describe (user); / / map- > bean BeanUtils.populate (user, map)
We usually use the hash in redis to put the object in the cache, as follows:
# set user information hset student name test hset student age 10
The utility class for converting map to bean is particularly useful in this scenario.
Codec
Common coding and decoding methods are encapsulated:
/ / Base64 Base64.encodeBase64String (byte [] binaryData) Base64.decodeBase64 (String base64String) / / MD5 DigestUtils.md5Hex (String data) / / URL URLCodec.decodeUrl (byte [] bytes); URLCodec.encodeUrl (BitSet urlsafe, byte [] bytes)
Collections
Operations such as intersection and difference:
/ / Null CollectionUtils.isEmpty (collA); / / intersection CollectionUtils.retainAll (collA, collB); / / Union CollectionUtils.union (collA, collB); / / difference CollectionUtils.subtract (collA, collB); / / Equal CollectionUtils.isEqualCollection (collA, collB)
I/O
Encapsulation of IO operation by IOUtils
/ / copy the stream IOUtils.copy (InputStream input, OutputStream output); / / read the content from the stream and change it to list List line = IOUtils.readLines (InputStream input, Charset encoding)
FileUtils
Encapsulation of file operation classes
File file = new File ("/ show/data.text"); / / read the file List lines = FileUtils.readLines (file, "UTF-8") by line; / / write the string to the file FileUtils.writeStringToFile (file, "test", "UTF-8"); / / copy the FileUtils.copyFile (srcFile, destFile)
Lang
StringUtils the following assertion test passed
/ / implementation of isEmpty cs = = null | | cs.length () = = 0; return true assertEquals (true, StringUtils.isEmpty (")); assertEquals (true, StringUtils.isBlank (null)); assertEquals (true, StringUtils.isBlank (")); / / blank assertEquals (true, StringUtils.isBlank (")); / / enter assertEquals (true, StringUtils.isBlank ("))
Pair and Triple when you want to return 2 or 3 values, but these values are not related, there is no need to encapsulate an object separately, you can use the following data structure to return Pair or Triple objects
Pair pair = new ImmutablePair (1Jue 2); / / 12 System.out.println (pair.getLeft () + "+" + pair.getRight ()); Triple triple = new ImmutableTriple (1PMI 2); / / 1 23 System.out.println (triple.getLeft () + "+ triple.getMiddle () +" + triple.getRight ())
Google Guava
Creation of a collection
/ / creation of ordinary sets Listlist = Lists.newArrayList (); Setset = Sets.newHashSet (); / / creation of immutable sets ImmutableListlist = ImmutableList.of ("a", "b", "c"); ImmutableSetset = ImmutableSet.of ("a", "b")
Immutable collections are thread-safe and cannot be changed halfway because methods such as add are declared expired and an exception is thrown.
/ / creation of ordinary sets List list = Lists.newArrayList (); Set set = Sets.newHashSet (); / / creation of immutable sets ImmutableList list = ImmutableList.of ("a", "b", "c"); ImmutableSet set = ImmutableSet.of ("a", "b")
Various cool techs collections
/ / use java Map map = new HashMap (); / / use guava Multimap map = ArrayListMultimap.create (); map.put ("key1", 1); map.put ("key1", 2); / / [1,2] System.out.println (map.get ("key1"))
2 keys map a value
Table table = HashBasedTable.create (); table.put ("a", "a", 1); table.put ("a", "b", 2); / / 2 System.out.println (table.get ("a", "b"))
There are many other various types of collections that will no longer be introduced.
Stop watch
View the run time of a piece of code
Stopwatch stopwatch = Stopwatch.createStarted (); / / do something long second = stopwatch.elapsed (TimeUnit.SECONDS)
TimeUnit can specify time precision
Joda Time
Before jdk1.8, only java.util.Date and java.util.Calendar were commonly used in date manipulation classes, but the ease of use of these two classes was so poor that SimpleDateFormat was not thread-safe. This forces users to choose third-party date manipulation classes, and Joda Time is one of the best.
The api of the two is very similar, if the company's jdk version is above 1.8, it is recommended to use jdk1.8 's new date class, and if it is below 1.8, it is recommended to use Joda Time.
These are all the contents of this article entitled "what are the common tool libraries in the Java world?" Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.