【一】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()) 这么比较比较严谨文章来源:https://www.toymoban.com/news/detail-466225.html
(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模板网!