【Java】Appache StringUtils 和 CollectionUtils工具类的学习笔记
【前言】
最近字符串工具和集合工具类的学习,做个笔记以免忘记。
目录:
1、字符串工具类 StringUtils
2、集合工具类 CollectionUtils
附:包导入
import org.apache.commons.lang3.StringUtils; import org.apache.commons.collections4.CollectionUtils;
导入的maven包:
<!-- 工具类--> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> <version>3.4</version> </dependency> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-collections4</artifactId> <version>4.4</version> </dependency>
【字符串工具类 StringUtils】
方法概览:
-
- isBlank():判断是否是空字符串
- isEmpty():判断是否空字符串
- trim(string str)、strip(string str):去除字符串前后空格,同时不改变原字符串
- trimToNull(string str):去除字符串前后空格,如果去除是空字符串就转为空字符串null
- trimToEmpty(string str):去除字符串前后空格,如果去除是空字符串就转为空字符串""
- truncate(string str,int maxwidth):截字符串的前maxwidth个字符,如果str的长度小于maxwidth就直接返回str,小于0的话直接抛出异常
- truncate(string str,int offset,int maxwidth):截取指定区域的字符串
- strip(string str ,string stripchars):从str前后剥离stripchars字符串
- stripStart(final String str, final String stripChars):从开始的地方剥离指定字符串;
- stripEnd(final String str, final String stripChars):从结束的地方剥离字符串;
- String[] stripAll(final String... strs):对数组中的所有字符串进行去除空格;
- String[] stripAll(final String[] strs, final String stripChars):对数组中的每个字符串进行字符串剥离;
- equals(final CharSequence cs1, final CharSequence cs2):判断两个字符串是否相等;
- equalsIgnoreCase(final CharSequence str1, final CharSequence str2):判断两个字符串在忽略大小写下是否相等;
- compare(final String str1, final String str2):比较两个字符串长度的大小;
- compareIgnoreCase(final String str1, final String str2)忽略大小写下比较两个字符串的大小;
- equalsAny(final CharSequence string, final CharSequence... searchStrings):比较多字符串中是否存在相同的字符串
- equalsAnyIgnoreCase(final CharSequence string, final CharSequence...searchStrings):忽略大小写是否存在相同字符
- indexOf(final CharSequence seq, final int searchChar):查找指定字符在字符串中首次出现的位置的索引值;
- indexOf(final CharSequence seq, final int searchChar, final int startPos):从指定位置final int startPos开始查找;
- indexOfIgnoreCase(final CharSequence str, final CharSequence searchStr):忽略大小写的查找字符串首次出现位置
- indexOfIgnoreCase(final CharSequence str, final CharSequence searchStr, int startPos):同`indexOf类似;
- lastIndexOf(final CharSequence seq, final int searchChar):从尾部开始查找;
- lastIndexOf(final CharSequence seq, final int searchChar, final int startPos):同理从尾部开始查找指定位置前的字符
区别:
- isBlank()和isEmpty()的区别:
StringUtils.isBlank(null); = true StringUtils.isBlank(""); = true StringUtils.isBlank(" "); = true StringUtils.isEmpty(null); = true StringUtils.isEmpty(""); = true StringUtils.isEmpty(" "); = false
isEmpty()认为“ ”不是空字符串
- 填充函数:——重点介绍:我在这里踩了很久的坑。
- left(string str , int size) , right(string str , int size):
- leftpad(string str,int size, string padStr), rightpad(string str,int size, string padStr), center( string str,int size, string padStr):左侧填充,右侧填充,两边填充。size是完成填充后的总长度,size> length(str),否则填充不成功。
- removeStart(string str, string remove),removeEnd(string str, string remove),remove(string str, string remove):移除。但是注意只能移除已有。
- countMatches(String str, String find):计算字符串在另一个字符串中出现的次数:
【集合工具类CollectionUtils】
方法概览:
-
- isEmpty():判断是否空集合
- union(str,str):并集,intersection(str,str):交集,disjunction(str,str):补集;substract(str,str):差集
对比:
1、list自带方法
public static void main(String[] args) { String[] arrayA = new String[] { "1", "2", "3", "4"}; String[] arrayB = new String[] { "3", "4", "5", "6" }; List<String> listA = Arrays.asList(arrayA); List<String> listB = Arrays.asList(arrayB); //1、交集 List<String> jiaoList = new ArrayList<>(listA); jiaoList.retainAll(listB); System.out.println(jiaoList); //输出:[3, 4] //2、差集 List<String> chaList = new ArrayList<>(listA); chaList.removeAll(listB); System.out.println(chaList); //输出:[1, 2] //3、并集 (先做差集再做添加所有) List<String> bingList = new ArrayList<>(listA); bingList.removeAll(listB); // bingList为 [1, 2] bingList.addAll(listB); //添加[3,4,5,6] System.out.println(bingList); //输出:[1, 2, 3, 4, 5, 6] }
2、java8.0stream新特性
public static void main(String[] args) { String[] arrayA = new String[] { "1", "2", "3", "4"}; String[] arrayB = new String[] { "3", "4", "5", "6" }; List<String> listA = Arrays.asList(arrayA); List<String> listB = Arrays.asList(arrayB); // 交集 List<String> intersection = listA.stream().filter(item -> listB.contains(item)).collect(toList()); System.out.println(intersection); //输出:[3, 4] // 差集 (list1 - list2) List<String> reduceList = listA.stream().filter(item -> !listB.contains(item)).collect(toList()); System.out.println(reduceList); //输出:[1, 2] // 并集 (新建集合:1、是因为不影响原始集合。2、Arrays.asList不能add和remove操作。 List<String> listAll = listA.parallelStream().collect(toList()); List<String> listAll2 = listB.parallelStream().collect(toList()); listAll.addAll(listAll2); System.out.println(listAll); //输出:[1, 2, 3, 4, 3, 4, 5, 6] // 去重并集 List<String> list =new ArrayList<>(listA); list.addAll(listB); List<String> listAllDistinct = list.stream().distinct().collect(toList()); System.out.println(listAllDistinct); //输出:[1, 2, 3, 4, 5, 6] }
第1种还需要确定该集合是否被多次调用。第2种可读性不高。但是新特性后买会写