Java各种工具箱的使用

这篇具有很好参考价值的文章主要介绍了Java各种工具箱的使用。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

【一】StrUtil

(1)常用的方法

String str = "abCDEfghi";
//是否为空
boolean blank = StrUtil.isBlank(str);//false
//是否不为空
boolean notBlank = StrUtil.isNotBlank(str);//true
//去掉字符串后缀(removeprefix:前缀)
String removeSuffix = StrUtil.removeSuffix("test.txt", ".txt");//test
//忽略大小写去掉前缀(removeSuffixIgnoreCase:去掉后缀)
String removePrefixIgnoreCase = StrUtil.removePrefixIgnoreCase(str, "A");//bCDEfghi
//sub方法
//顺数第2个到第4个,包含尾部包含头
String sub = StrUtil.sub(str, 2, 4);//CD
//-3表示倒数第三个字符
String sub1 = StrUtil.sub(str, 2, -3);//CDEf
//format方法(使用字符串模板代替字符串拼接)
String template = "{}爱{}!{}";
String fin = StrUtil.format(template, "我", "JAVA","哈"); //我爱JAVA!哈

(2)hasBlank、hasEmpty方法

就是给定一些字符串,如果一旦有空的就返回true,常用于判断好多字段是否有空的(例如web表单数据)。

这两个方法的区别是hasEmpty只判断是否为null或者空字符串(“”),hasBlank则会把不可见字符也算做空,isEmpty和isBlank同理。

(3)sub方法

避免subString方法越界问题,index的位置还支持负数,-1表示最后一个字符(这个思想来自于Python),还有就是如果不小心把第一个位置和第二个位置搞反了,也会自动修正(例如想截取第4个和第2个字符之间的部分也是可以的

public static String sub(CharSequence str, int fromIndex, int toIndex) 
public static String subPreGbk(CharSequence str, int len, CharSequence suffix) 
public static String maxLength(CharSequence string, int length) 
public static String subPre(CharSequence string, int toIndex) 
public static String subSuf(CharSequence string, int fromIndex) 
public static String subSufByLength(CharSequence string, int length) 
public static String subWithLength(String input, int fromIndex, int length) 
public static String subBefore(CharSequence string, CharSequence separator, boolean isLastSeparator) 
public static String subBefore(CharSequence string, char separator, boolean isLastSeparator) 
public static String subAfter(CharSequence string, CharSequence separator, boolean isLastSeparator) 
public static String subAfter(CharSequence string, char separator, boolean isLastSeparator) 
public static String subBetween(CharSequence str, CharSequence before, CharSequence after) 
public static String subBetween(CharSequence str, CharSequence beforeAndAfter)

案例

String str = “abcdefgh”;
String strSub1 = StrUtil.sub(str, 2, 3); //strSub1 -> c
String strSub2 = StrUtil.sub(str, 2, -3); //strSub2 -> cde
String strSub3 = StrUtil.sub(str, 3, 2); //strSub2 -> c

(4)去空格 回车操作 与空有关的方法

public static boolean isBlank(CharSequence str)
public static boolean isBlankIfStr(Object obj)
public static boolean isNotBlank(CharSequence str)
public static boolean hasBlank(CharSequence… strs)
public static boolean isAllBlank(CharSequence… strs)
public static boolean isEmpty(CharSequence str)
public static boolean isEmptyIfStr(Object obj)
public static boolean isNotEmpty(CharSequence str)
public static String nullToEmpty(CharSequence str)
public static String nullToDefault(CharSequence str, String defaultStr)
public static String emptyToDefault(CharSequence str, String defaultStr)
public static String blankToDefault(CharSequence str, String defaultStr)
public static String emptyToNull(CharSequence str)
public static boolean hasEmpty(CharSequence… strs)
public static boolean isAllEmpty(CharSequence… strs)
public static boolean isNullOrUndefined(CharSequence str)
public static boolean isEmptyOrUndefined(CharSequence str)
public static boolean isBlankOrUndefined(CharSequence str)
public static String cleanBlank(CharSequence str)

// 去空格

StrUtil.cleanBlank

// 去\n\r

StrUtil.removeAllLineBreaks()

(5)字符串包含关系

案例

// 字符串中: 同时匹配 “小明” 和 “19岁” 这两个字符才返回true
boolean isContain = StrUtil.containsAll(“我叫小明今年18岁职业java软件工程师”, “小明”,19岁”);

其他方法

public static boolean startWith(CharSequence str, char c) 
public static boolean startWith(CharSequence str, CharSequence prefix, boolean isIgnoreCase) 
public static boolean startWith(CharSequence str, CharSequence prefix) 
public static boolean startWithIgnoreCase(CharSequence str, CharSequence prefix) 
public static boolean startWithAny(CharSequence str, CharSequence... prefixes) 
public static boolean endWith(CharSequence str, char c) 
public static boolean endWith(CharSequence str, CharSequence suffix, boolean isIgnoreCase) 
public static boolean endWith(CharSequence str, CharSequence suffix) 
public static boolean endWithIgnoreCase(CharSequence str, CharSequence suffix) 
public static boolean endWithAny(CharSequence str, CharSequence... suffixes) 
public static boolean contains(CharSequence str, char searchChar) 
public static boolean containsAny(CharSequence str, CharSequence... testStrs) 
public static boolean containsAny(CharSequence str, char... testChars) 
public static boolean containsBlank(CharSequence str) 
public static String getContainsStr(CharSequence str, CharSequence... testStrs) 
public static boolean containsIgnoreCase(CharSequence str, CharSequence testStr) 
public static boolean containsAnyIgnoreCase(CharSequence str, CharSequence... testStrs) 
public static String getContainsStrIgnoreCase(CharSequence str, CharSequence... testStrs)

(6)头尾的一些处理

public static String trim(CharSequence str) 
public static void trim(String[] strs) 
public static String trimToEmpty(CharSequence str) 
public static String trimToNull(CharSequence str) 
public static String trimStart(CharSequence str) 
public static String trimEnd(CharSequence str) 
public static String trim(CharSequence str, int mode) 
public static String strip(CharSequence str, CharSequence prefixOrSuffix) 
public static String strip(CharSequence str, CharSequence prefix, CharSequence suffix) 
public static String stripIgnoreCase(CharSequence str, CharSequence prefixOrSuffix) 
public static String stripIgnoreCase(CharSequence str, CharSequence prefix, CharSequence suffix) 
public static String addPrefixIfNot(CharSequence str, CharSequence prefix) 
public static String addSuffixIfNot(CharSequence str, CharSequence suffix) 
public static boolean isSurround(CharSequence str, CharSequence prefix, CharSequence suffix) 
public static boolean isSurround(CharSequence str, char prefix, char suffix) 

(7)删除字符操作

public static String removeAll(CharSequence str, CharSequence strToRemove) 
public static String removeAll(CharSequence str, char... chars) 
public static String removeAllLineBreaks(CharSequence str) 
public static String removePreAndLowerFirst(CharSequence str, int preLength) 
public static String removePreAndLowerFirst(CharSequence str, CharSequence prefix) 
public static String removePrefix(CharSequence str, CharSequence prefix) 
public static String removePrefixIgnoreCase(CharSequence str, CharSequence prefix) 
public static String removeSuffix(CharSequence str, CharSequence suffix) 
public static String removeSufAndLowerFirst(CharSequence str, CharSequence suffix) 
public static String removeSuffixIgnoreCase(CharSequence str, CharSequence suffix) 

(8)大小写的转换

public static String upperFirstAndAddPre(CharSequence str, String preString) 
public static String upperFirst(CharSequence str) 
public static String lowerFirst(CharSequence str) 
public static boolean isUpperCase(CharSequence str) 
public static boolean isLowerCase(CharSequence str) 

(9)分割操作

public static String[] splitToArray(CharSequence str, char separator) 
public static long[] splitToLong(CharSequence str, char separator) 
public static long[] splitToLong(CharSequence str, CharSequence separator) 
public static int[] splitToInt(CharSequence str, char separator) 
public static int[] splitToInt(CharSequence str, CharSequence separator) 
public static List<String> split(CharSequence str, char separator) 
public static String[] splitToArray(CharSequence str, char separator, int limit) 
public static List<String> split(CharSequence str, char separator, int limit) 
public static List<String> splitTrim(CharSequence str, char separator) 
public static List<String> splitTrim(CharSequence str, CharSequence separator) 
public static List<String> splitTrim(CharSequence str, char separator, int limit) 
public static List<String> splitTrim(CharSequence str, CharSequence separator, int limit) 
public static List<String> split(CharSequence str, char separator, boolean isTrim, boolean ignoreEmpty) 
public static List<String> split(CharSequence str, char separator, int limit, boolean isTrim, boolean ignoreEmpty) 
public static List<String> split(CharSequence str, CharSequence separator, int limit, boolean isTrim, boolean ignoreEmpty) 
public static String[] split(CharSequence str, CharSequence separator) 
public static String[] split(CharSequence str, int len) 
public static String[] cut(CharSequence str, int partLength) 

(10)判断相等

public static boolean equals(CharSequence str1, CharSequence str2) 
public static boolean equalsIgnoreCase(CharSequence str1, CharSequence str2) 
public static boolean equals(CharSequence str1, CharSequence str2, boolean ignoreCase) 
public static boolean isSubEquals(CharSequence str1, int start1, CharSequence str2, int start2, int length, boolean ignoreCase) 
public static boolean isAllCharMatch(CharSequence value, Matcher<Character> matcher) 
public static boolean equalsCharAt(CharSequence str, int position, char c) 

(11)出现次数统计

public static int count(CharSequence content, CharSequence strForSearch) 
public static int count(CharSequence content, char charForSearch)

【二】StringUtils

StringUtils 方法的操作对象是 Java.lang.String 类型的对象,是 JDK 提供的 String 类型操作方法的补充,并且是 null 安全的(即如果输入参数 String 为 null 则不会抛出 NullPointerException ,而是做了相应处理,例如,如果输入为 null 则返回也是 null 等。除了构造器,StringUtils 中一共有130多个方法,并且都是 static 的,所以我们可以这样调用 StringUtils.xxx()

(1)isEmpty:判断某字符串是否为空

public static boolean isEmpty(String str) :判断某字符串是否为空,为空的标准是 str==null 或 str.length()==0

StringUtils.isEmpty(null) = true
StringUtils.isEmpty("") = true 
StringUtils.isEmpty(" ") = false //注意在 StringUtils 中空格作非空处理
StringUtils.isEmpty("   ") = false
StringUtils.isEmpty("bob") = false
StringUtils.isEmpty(" bob ") = false

(2)isNotEmpty:判断某字符串是否非空

判断某字符串是否非空,等于 !isEmpty(String str) :public static boolean isNotEmpty(String str)

StringUtils.isNotEmpty(null) = false
StringUtils.isNotEmpty("") = false
StringUtils.isNotEmpty(" ") = true
StringUtils.isNotEmpty("         ") = true
StringUtils.isNotEmpty("bob") = true
StringUtils.isNotEmpty(" bob ") = true

(3)isBlank(String str):判断某字符串是否为空或长度为0或由空白符(whitespace) 构成

public static boolean isBlank(String str)

StringUtils.isBlank(null) = true
StringUtils.isBlank("") = true
StringUtils.isBlank(" ") = true
StringUtils.isBlank("        ") = true
StringUtils.isBlank("\t \n \f \r") = true   //对于制表符、换行符、换页符和回车符
 
StringUtils.isBlank()   //均识为空白符
StringUtils.isBlank("\b") = false   //"\b"为单词边界符
StringUtils.isBlank("bob") = false
StringUtils.isBlank(" bob ") = false

(4)isNotBlank(String str)

判断某字符串是否不为空且长度不为0且不由空白符(whitespace) 构成,等于 !isBlank(String str)

StringUtils.isNotBlank(null) = false
StringUtils.isNotBlank("") = false
StringUtils.isNotBlank(" ") = false
StringUtils.isNotBlank("         ") = false
StringUtils.isNotBlank("\t \n \f \r") = false
StringUtils.isNotBlank("\b") = true
StringUtils.isNotBlank("bob") = true
StringUtils.isNotBlank(" bob ") = true

(5)trim(String str)

去掉字符串两端的控制符(control characters, char <= 32) , 如果输入为 null 则返回null

StringUtils.trim(null) = null
StringUtils.trim("") = ""
StringUtils.trim(" ") = ""
StringUtils.trim(" \b \t \n \f \r    ") = ""
StringUtils.trim("     \n\tss   \b") = "ss"
StringUtils.trim(" d   d dd     ") = "d   d dd"
StringUtils.trim("dd     ") = "dd"
StringUtils.trim("     dd       ") = "dd"

(6)trimToNull(String str)

去掉字符串两端的控制符(control characters, char <= 32) ,如果变为 null 或"",则返回 null

StringUtils.trimToNull(null) = null
StringUtils.trimToNull("") = null
StringUtils.trimToNull(" ") = null
StringUtils.trimToNull("     \b \t \n \f \r    ") = null
StringUtils.trimToNull("     \n\tss   \b") = "ss"
StringUtils.trimToNull(" d   d dd     ") = "d   d dd"
StringUtils.trimToNull("dd     ") = "dd"
StringUtils.trimToNull("     dd       ") = "dd"

(7) trimToEmpty(String str)

去掉字符串两端的控制符(control characters, char <= 32) ,如果变为 null 或 “” ,则返回 “”

StringUtils.trimToEmpty(null) = ""
StringUtils.trimToEmpty("") = ""
StringUtils.trimToEmpty(" ") = ""
StringUtils.trimToEmpty("     \b \t \n \f \r    ") = ""
StringUtils.trimToEmpty("     \n\tss   \b") = "ss"
StringUtils.trimToEmpty(" d   d dd     ") = "d   d dd"
StringUtils.trimToEmpty("dd     ") = "dd"
StringUtils.trimToEmpty("     dd       ") = "dd"

(8)strip(String str)

去掉字符串两端的空白符(whitespace) ,如果输入为 null 则返回 null
注意和 trim() 的区别

StringUtils.strip(null) = null
StringUtils.strip("") = ""
StringUtils.strip(" ") = ""
StringUtils.strip("     \b \t \n \f \r    ") = "\b"
StringUtils.strip("     \n\tss   \b") = "ss   \b"
StringUtils.strip(" d   d dd     ") = "d   d dd"
StringUtils.strip("dd     ") = "dd"
StringUtils.strip("     dd       ") = "dd"

(9)stripToNull(String str)

去掉字符串两端的空白符(whitespace) ,如果变为 null 或"",则返回 null
(注意和 trimToNull() 的区别

StringUtils.stripToNull(null) = null
StringUtils.stripToNull("") = null
StringUtils.stripToNull(" ") = null
StringUtils.stripToNull("     \b \t \n \f \r    ") = "\b"
StringUtils.stripToNull("     \n\tss   \b") = "ss   \b"
StringUtils.stripToNull(" d   d dd     ") = "d   d dd"
StringUtils.stripToNull("dd     ") = "dd"
StringUtils.stripToNull("     dd       ") = "dd"

(10)stripToEmpty(String str)

去掉字符串两端的空白符(whitespace) ,如果变为 null 或"" ,则返回""
注意和 trimToEmpty() 的区别

StringUtils.stripToNull(null) = ""
StringUtils.stripToNull("") = ""
StringUtils.stripToNull(" ") = ""
StringUtils.stripToNull("     \b \t \n \f \r    ") = "\b"
StringUtils.stripToNull("     \n\tss   \b") = "ss   \b"
StringUtils.stripToNull(" d   d dd     ") = "d   d dd"
StringUtils.stripToNull("dd     ") = "dd"
StringUtils.stripToNull("     dd       ") = "dd"

(11)其他方法

(1)public static String strip(String str, String stripChars)
去掉 str 两端的在 stripChars 中的字符。
如果 str 为 null 或等于"" ,则返回它本身;
如果 stripChars 为 null 或"" ,则返回 strip(String str) 。

(2)public static String stripStart(String str, String stripChars)
去掉 str 前端的在 stripChars 中的字符。

(3)public static String stripEnd(String str, String stripChars)
和11相似,去掉 str 末端的在 stripChars 中的字符。

(4)public static String[] stripAll(String[] strs)
对字符串数组中的每个字符串进行 strip(String str) ,然后返回。
如果 strs 为 null 或 strs 长度为0,则返回 strs 本身

(5)public static String[] stripAll(String[] strs, String stripChars)
对字符串数组中的每个字符串进行 strip(String str, String stripChars) ,然后返回。
如果 strs 为 null 或 strs 长度为0,则返回 strs 本身

(6)public static boolean equals(String str1, String str2)
比较两个字符串是否相等,如果两个均为空则也认为相等。

(7)public static boolean equalsIgnoreCase(String str1, String str2)
比较两个字符串是否相等,不区分大小写,如果两个均为空则也认为相等。

(8)public static int indexOf(String str, char searchChar)
返回字符 searchChar 在字符串 str 中第一次出现的位置。
如果 searchChar 没有在 str 中出现则返回-1,
如果 str 为 null 或 “” ,则也返回-1

(9)public static int indexOf(String str, char searchChar, int startPos)
返回字符 searchChar 从 startPos 开始在字符串 str 中第一次出现的位置。
如果从 startPos 开始 searchChar 没有在 str 中出现则返回-1,
如果 str 为 null 或 “” ,则也返回-1

(10)public static int indexOf(String str, String searchStr)
返回字符串 searchStr 在字符串 str 中第一次出现的位置。
如果 str 为 null 或 searchStr 为 null 则返回-1,
如果 searchStr 为 “” ,且 str 为不为 null ,则返回0,
如果 searchStr 不在 str 中,则返回-1

(11)public static int ordinalIndexOf(String str, String searchStr, int ordinal)
返回字符串 searchStr 在字符串 str 中第 ordinal 次出现的位置。
如果 str=null 或 searchStr=null 或 ordinal<=0 则返回-1
举例(*代表任意字符串):

StringUtils.ordinalIndexOf(null, *, *) = -1
StringUtils.ordinalIndexOf(*, null, *) = -1
StringUtils.ordinalIndexOf("", "", *) = 0
StringUtils.ordinalIndexOf("aabaabaa", "a", 1) = 0
StringUtils.ordinalIndexOf("aabaabaa", "a", 2) = 1
StringUtils.ordinalIndexOf("aabaabaa", "b", 1) = 2
StringUtils.ordinalIndexOf("aabaabaa", "b", 2) = 5
StringUtils.ordinalIndexOf("aabaabaa", "ab", 1) = 1
StringUtils.ordinalIndexOf("aabaabaa", "ab", 2) = 4
StringUtils.ordinalIndexOf("aabaabaa", "bc", 1) = -1
StringUtils.ordinalIndexOf("aabaabaa", "", 1) = 0
StringUtils.ordinalIndexOf("aabaabaa", "", 2) = 0

(12)public static int indexOf(String str, String searchStr, int startPos)
返回字符串 searchStr 从 startPos 开始在字符串 str 中第一次出现的位置。
举例(*代表任意字符串):

StringUtils.indexOf(null, *, *) = -1
StringUtils.indexOf(*, null, *) = -1
StringUtils.indexOf("", "", 0) = 0
StringUtils.indexOf("aabaabaa", "a", 0) = 0
StringUtils.indexOf("aabaabaa", "b", 0) = 2
StringUtils.indexOf("aabaabaa", "ab", 0) = 1
StringUtils.indexOf("aabaabaa", "b", 3) = 5
StringUtils.indexOf("aabaabaa", "b", 9) = -1
StringUtils.indexOf("aabaabaa", "b", -1) = 2
StringUtils.indexOf("aabaabaa", "", 2) = 2
StringUtils.indexOf("abc", "", 9) = 3

(13)public static int lastIndexOf(String str, char searchChar)
基本原理同18

(14)public static int lastIndexOf(String str, char searchChar, int startPos)
基本原理同19

(15)public static int lastIndexOf(String str, String searchStr)
基本原理同20

(16)public static int lastIndexOf(String str, String searchStr, int startPos)

(12)裁剪字符串

(1)利用StringUtils字符串工具类的substring—截取

 String str = "2023年2月16日21:10:05 Jiangnan Cui";
         //            012345 6789 012345678901234567890
         //                        1         2         3
         
 /**
  * 利用StringUtils工具类截取
  */
 // 1、截取字符串str第一个"内容"前的所有内容
 String s = StringUtils.substringBefore(str, " ");
 System.out.println("s = " + s);// 2023年2月16日21:10:05// 2、截取字符串str第一个"内容"后的所有内容
 String s1 = StringUtils.substringAfter(str, " ");
 System.out.println("s1 = " + s1);// Jiangnan Cui
 
String s2 = StringUtils.substringAfter(str, " ",ture);
 System.out.println("s2 = " + s2);// 其它详见:
 //        StringUtils.substring()
 //        StringUtils.substringBefore()
 //        StringUtils.substringAfter()
 //        StringUtils.substringAfterLast()
 //        StringUtils.substringBeforeLast()
 //        StringUtils.substringBetween()

【三】CollectionUtils

(1)判断集合的空与非空

(1)判断集合是否为空

CollectionUtils.isEmpty(null): true
CollectionUtils.isEmpty(new ArrayList()): true  
CollectionUtils.isEmpty({a,b}): false

(2)判断集合是否不为空

CollectionUtils.isNotEmpty(null): false
CollectionUtils.isNotEmpty(new ArrayList()): false
CollectionUtils.isNotEmpty({a,b}): true

(2)集合并集

@Test
public void testUnion(){
    String[] arrayA = new String[] { "A", "B", "C", "D", "E", "F" };  
    String[] arrayB = new String[] { "B", "D", "F", "G", "H", "K" };
    List<String> listA = Arrays.asList(arrayA);
    List<String> listB = Arrays.asList(arrayB);
    //2个数组取并集 
    System.out.println(ArrayUtils.toString(CollectionUtils.union(listA, listB)));
    //[A, B, C, D, E, F, G, H, K]
}

(3)集合交集

@Test
public void testIntersection(){
    String[] arrayA = new String[] { "A", "B", "C", "D", "E", "F" };  
    String[] arrayB = new String[] { "B", "D", "F", "G", "H", "K" };
    List<String> listA = Arrays.asList(arrayA);
    List<String> listB = Arrays.asList(arrayB);
    //2个数组取交集 
    System.out.println(ArrayUtils.toString(CollectionUtils.intersection(listA, listB)));
    //[B, D, F]
 
}

(4)交集的补集(析取)

@Test
public void testDisjunction(){
    String[] arrayA = new String[] { "A", "B", "C", "D", "E", "F" };  
    String[] arrayB = new String[] { "B", "D", "F", "G", "H", "K" };
    List<String> listA = Arrays.asList(arrayA);
    List<String> listB = Arrays.asList(arrayB);
    //2个数组取交集 的补集
    System.out.println(ArrayUtils.toString(CollectionUtils.disjunction(listA, listB)));
    //[A, C, E, G, H, K]
}

(5)差集(扣除)

@Test
public void testSubtract(){
    String[] arrayA = new String[] { "A", "B", "C", "D", "E", "F" };  
    String[] arrayB = new String[] { "B", "D", "F", "G", "H", "K" };
    List<String> listA = Arrays.asList(arrayA);
    List<String> listB = Arrays.asList(arrayB);
    //arrayA扣除arrayB
    System.out.println(ArrayUtils.toString(CollectionUtils.subtract(listA, listB)));
    //[A, C, E]
 
}

(6)集合是否为空

@Test
public void testIsEmpty(){
 
    class Person{}
    class Girl extends Person{}
 
    List<Integer> first = new ArrayList<>();
    List<Integer> second = null;
    List<Person> boy = new ArrayList<>();
    //每个男孩心里都装着一个女孩
    boy.add(new Girl());
    //判断集合是否为空
    System.out.println(CollectionUtils.isEmpty(first));   //true
    System.out.println(CollectionUtils.isEmpty(second));   //true
    System.out.println(CollectionUtils.isEmpty(boy));   //false
 
    //判断集合是否不为空
    System.out.println(CollectionUtils.isNotEmpty(first));   //false
    System.out.println(CollectionUtils.isNotEmpty(second));   //false
    System.out.println(CollectionUtils.isNotEmpty(boy));   //true
}

(7)集合是否相等

@Test
public void testIsEqual(){
 
    class Person{}
    class Girl extends Person{
    }
 
    List<Integer> first = new ArrayList<>();
    List<Integer> second = new ArrayList<>();
    first.add(1);
    first.add(2);
    second.add(2);
    second.add(1);
    Girl goldGirl = new Girl();
    List<Person> boy1 = new ArrayList<>();
    //每个男孩心里都装着一个女孩
    boy1.add(new Girl());
    List<Person> boy2 = new ArrayList<>();
    //每个男孩心里都装着一个女孩
    boy2.add(new Girl());
    //比较两集合值
    System.out.println(CollectionUtils.isEqualCollection(first,second));   //true
    System.out.println(CollectionUtils.isEqualCollection(first,boy1));   //false
    System.out.println(CollectionUtils.isEqualCollection(boy1,boy2));   //false
 
    List<Person> boy3 = new ArrayList<>();
    //每个男孩心里都装着一个女孩
    boy3.add(goldGirl);
    List<Person> boy4 = new ArrayList<>();
    boy4.add(goldGirl);
    System.out.println(CollectionUtils.isEqualCollection(boy3,boy4));   //true
}

(8)不可修改的集合

我们对c进行操作,s也同样获得了和c相同的内容,这样就可以避免其他人员修改这个s对象。有时候需要对它进行保护,避免返回结果被人修改。

@Test
public void testUnmodifiableCollection(){
    Collection<String> c = new ArrayList<>();
    Collection<String> s = CollectionUtils.unmodifiableCollection(c);
    c.add("boy");
    c.add("love");
    c.add("girl");
    //! s.add("have a error");
    System.out.println(s);
}

Collections.unmodifiableCollection可以得到一个集合的镜像,它的返回结果是不可直接被改变,否则会提示错误

java.lang.UnsupportedOperationException
at org.apache.commons.collections.collection.UnmodifiableCollection.add(UnmodifiableCollection.java:75)

【四】ObjectUtil

【五】ObjectUtils

【六】FileUtil

【七】DateUtil

【八】常用的案例分析

【1】判断字符串是否为空方法

(1)String字符串的4种状态

1-声明了并别引用值为 null
2-分配了内存空间赋值为 “”
3-分配了内存空间没赋值(默认‘’‘’) String A=new String();
4-只声明了没引用 String B;
5-有值

(2)String空字符串的2种状态

1-null
2-“”

(3)比较字符串为空的4种方法

(1)if(s == null || s.isEmpty()):Java SE 6.0 后开始提供的方法
(2)if(s == null || s.length() <= 0):比较字符串长度, 效率最高
(3)if (s == null || s == “”):比较直观,简便的方法
(4)if(s == null ||“”.equals(s)):效率很低(== 引用数据类型比较的是值 equals比较的是地址但string类重写了equals方法 比较的是值)

(4)StringUtils比较

推荐使用common.lang3

(1)一种是org.apache.commons.lang包下的; 只能比较字符串长度是否为0

public boolean isEmpty() {
    return value.length == 0;
}

(2)一种是org.springframework.util包下的:参数是Object类 可以比较任何类型

public static boolean isEmpty(Object str){
    return (str == null || "".equals(str));
}

所以我们一般用springframework包下的stringutil更方便或者直接 if(s == null || s.isEmpty()) 这么比较比较严谨

(3)用isBlank还是isEmpty?用isBlank,有空格也是空
推荐使用common.lang3的isBlank()文章来源地址https://www.toymoban.com/news/detail-466225.html

//推荐使用(如果字符中是空的,也是true)  
System.out.println(StringUtils.isBlank(" "));//true
//字符中包含空格,它认为该字符串不为空
System.out.println(StringUtils.isEmpty(" "));//false

(5)StrUtils比较

【2】判断对象是否为空

【3】判断List集合和Map集合是否为空

到了这里,关于Java各种工具箱的使用的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • matlab机器人工具箱基础使用

    资料:https://blog.csdn.net/huangjunsheng123/article/details/110630665 test1.m

    2024年02月12日
    浏览(40)
  • SwissArmyTransformer瑞士军刀工具箱使用手册

    Introduction sat(SwissArmyTransformer)是一个灵活而强大的库,用于开发您自己的Transformer变体。 sat是以“瑞士军刀”命名的,这意味着所有型号(例如BERT、GPT、T5、GLM、CogView、ViT…)共享相同的backone代码,并通过一些超轻量级的mixin满足多种用途。 sat由deepspeed ZeRO和模型并行性提

    2024年02月04日
    浏览(67)
  • 工具箱:在线免费使用的文档工具:(PDF转换,图片压缩等)

    这些都是博主亲自使用过的,可以使用。  PDF转换器:  http://www.pdfdo.com/ 图片压缩:   免费在线图片/视频压缩工具 | 图片压缩 | 免费 JPG PNG GIF 图像压缩 (yalijuda.com) 文档OCR转EXCEL:   文字识别 OCR_ 图片文字识别_图片文字智能识别-腾讯云 (tencent.com) 

    2024年02月14日
    浏览(53)
  • GTOT和RGBT234测评工具箱使用

    目录 说明 环境 GTOT测评工具箱使用 结构 准备工作 测评过程 生成ERRresults/xxx.mat 生成PR、SR曲线图 合成跟踪可视化视频 RGBT234测评工具箱使用 说明 结构 准备工作 测评过程 生成ERRresults_TIP/xxx.mat  生成PR、SR曲线图 合成跟踪可视化视频  本篇博客是RGBT目标跟踪专题中的其中一篇

    2024年02月08日
    浏览(43)
  • 【打工日常】使用docker部署Dashdot工具箱

    dashdot 是一个简洁清晰的服务器数据仪表板,基于 React 实现 ,主要是显示操作系统、进程、存储、内存、网络这五个的数据。 本次实践部署环境为个人测试环境 本次实践环境规划:docker快速拉取Dashdot镜像,然后后台启动Dashdot镜像 启动镜像后的名字 IP地址 容器镜像版本 操

    2024年02月19日
    浏览(46)
  • Java:Hutool工具箱之Hutool-crypto加密解密

    文档 https://hutool.cn/docs/#/crypto/概述 重点单词: 摘自文档 依赖 以MD5 为例 以AES 加密为例 这里有个问题,如果秘钥长度不够16位,会报错 长度只能是16位,24位,32位 参考 https://toscode.gitee.com/dromara/hutool/issues/I4O1EB 以RSA为例

    2024年02月16日
    浏览(73)
  • CC工具箱使用指南:【获取所有字段信息】

    一、简介 这个工具的目的简单易懂,就是获取选定要素图层的所有字段信息。 本身不对要素图层作任何处理,只是一个查看属性的工具。 问我要用在什么地方,我也不知道-_- 二、工具参数介绍 点击【信息获取】组里的【获取所有字段信息】工具: 即可打开下面的工具框界

    2024年01月16日
    浏览(45)
  • CC工具箱使用指南:【更改字段别名(属性映射)】

    一、简介 在我工作中遇到的大多数图斑,字段名称一般是英文,字段别名是中文,使用起来是比较方便的。 但有时候数据经过分析处理过后,图斑的字段别名被修改成了和字段名称一样的英文,这样就很难理解字段名称的意思,特别是其它专业、不熟悉的图斑,就很麻烦。

    2024年01月18日
    浏览(44)
  • 如何使用图吧工具箱进行CPU和显卡双烤

    图吧工具箱是一款功能强大的硬件检测工具合集,且开源、免费、绿色; 集成了硬件检测、评分工具、测试工具,常见的工具都有; 电脑压力测试能对电脑硬件进行检测,通过释放电脑的性能来达到性能检测的目的; 压力测试也是一种包质保量的操作,是软件检测过程的一

    2023年04月23日
    浏览(61)
  • CC工具箱使用指南:【现状规划用地变化检查(村规)】

    一、简介 在规划工作中,有一个普遍性的需求,就是需要检查规划前后在用地上究竟发生了哪些变化。 这一点很重要,不仅是要展示给别人看,自己也要十分注意。 规划方案完成后,一定要进行用地变化的检查,曾经在村规中就遇到过因为误操作占用了村民宅基地的情况。

    2024年01月23日
    浏览(43)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包