1.1 StringUtilsorg.apache.commons commons-lang3
1.判断是否为空 null/“”/" "
String str = " "; System.out.println(StringUtils.isBlank(str));//true //判断不是空 System.out.println(StringUtils.isNotBlank(str));//fasle1.2 NumberUtils
//判断一个参数是不是数字 //isDigits,只能包含整数 //isParsable:整数,小数都可,不能识别正负 //isCreatable可以识别整数,浮点数,正负号,进制 String str = "12.3aa"; System.out.println(NumberUtils.isDigits(str));//false System.out.println(NumberUtils.isParsable(str));//false System.out.println(NumberUtils.isCreatable(str));//false String str1 = "12.3"; System.out.println(NumberUtils.isDigits(str1));//false System.out.println(NumberUtils.isParsable(str1));//true System.out.println(NumberUtils.isCreatable(str1));//true String str2 = "12"; System.out.println(NumberUtils.isDigits(str2));//true System.out.println(NumberUtils.isParsable(str2));//true System.out.println(NumberUtils.isCreatable(str2));//true String str3 = "+12"; System.out.println(NumberUtils.isDigits(str3));//false System.out.println(NumberUtils.isParsable(str3));//false System.out.println(NumberUtils.isCreatable(str3));//true String str4 = "09"; System.out.println(NumberUtils.isDigits(str4));//true System.out.println(NumberUtils.isParsable(str4));//true System.out.println(NumberUtils.isCreatable(str4));//false--8进制1.3 ObjectUtils
1.取第一个不为空的对象 String str1 = null; String str2 = null; String str3 = " "; String str4 = "数字"; System.out.println(ObjectUtils.firstNonNull(str1,str2,str3,str4));//str31.3 ArrayUtils
1.判断数组是否为空
Integer[] ints = new Integer[1];
Integer[] ints1 = new Integer[0];
Integer[] ints2 = new Integer[]{};
Integer[] ints3 = null;
System.out.println(ArrayUtils.isEmpty(ints));//false
System.out.println(ArrayUtils.isEmpty(ints1));//true
System.out.println(ArrayUtils.isEmpty(ints2));//true
System.out.println(ArrayUtils.isEmpty(ints3));//true
2.打印数组内容,向数组中添加内容
Integer[] ints = new Integer[1];
ints[0] = 3;
//ArrayUtils.toString打印数组内容
System.out.println(ArrayUtils.toString(ints));//{3}
//添加数据,新创建一个数组
Integer[] ints1 = ArrayUtils.add(ints, 7);
System.out.println(ArrayUtils.toString(ints1));//{3,7}
2.commons-collections4
2.1 CollectionUtilsorg.apache.commons commons-collections44.3
1.判断集合是否为空 List
2.获取交集,并集,差集 List3.commons-iolist = new ArrayList<>(); list.add("a"); list.add("b"); list.add("c"); List list1 = new ArrayList<>(); list1.add("1"); list1.add("2"); list1.add("c"); //交集 System.out.println(CollectionUtils.intersection(list, list1));//[c] //并集 System.out.println(CollectionUtils.union(list, list1));//[a, 1, b, 2, c] //差集 System.out.println(CollectionUtils.subtract(list, list1));//[a, b] System.out.println(CollectionUtils.subtract(list1, list));//[1, 2]
3.1 FileUtilsorg.apache.commons commons-io1.3.2
1.将文件内容转成字符串读出 String path = "D:\java\idea\IdeaProject2\utils-demo\src\main\resources\1.txt"; String file = FileUtils.readFileToString(new File(path), String.valueOf(StandardCharsets.UTF_8)); 2.文件内容分行读出 List list = FileUtils.readLines(new File(path), String.valueOf(StandardCharsets.UTF_8));3.2 FilenameUtils
3.获取文件的baseName/获取文件的后缀 String path = "D:\java\idea\IdeaProject2\utils-demo\src\main\resources\1.txt"; System.out.println(FilenameUtils.getBaseName(path)); System.out.println(FilenameUtils.getExtension(path));4.guava
4.1 Joinercom.google.guava guava29.0-jre
1.Joiner:把集合/数组/可变参数,通过指定的分隔符连接成字符串 Listlist = new ArrayList<>(); list.add("a"); list.add("b"); list.add("c"); list.add(null); //忽略null Joiner on = Joiner.on(",")skipNulls();//忽略null System.out.println(on.join(list));//a,b,c Joiner on = Joiner.on(",").useForNull("null替代");//替换null System.out.println(on.join(list));//a,b,c,null替代
2.Splitter:通过指定的分隔符把字符串转为集合
String str = "a,b,"", , c";
//on指定字符串的分隔符
Splitter on1 = Splitter.on(",");
Iterable split = on1.split(str);
System.out.println(split);//[a, b, "", , c]
//过滤空白的字符串,不包括""
Splitter on2 = Splitter.on(",").omitEmptyStrings();
Iterable split1 = on2.split(str);
System.out.println(split1);//[a, b, "", , c]
//.trimResults空格不保留
Splitter on3 = Splitter.on(",").omitEmptyStrings().trimResults();
Iterable split2 = on3.split(str);
System.out.println(split2);//[a, b, "", c]
//转数组
Iterable split3 = on3.splitToList(str);
System.out.println(split3);//[a, b, "", c]
4.2 CaseFormat
1.下划线和驼峰命名法互转 String str = "student_name"; System.out.println(CaseFormat.LOWER_UNDERSCORE.to(CaseFormat.LOWER_CAMEL, str));//studentName System.out.println(CaseFormat.LOWER_UNDERSCORE.to(CaseFormat.UPPER_CAMEL, str));//StudentName String str1 = "studentName"; System.out.println(CaseFormat.LOWER_CAMEL.to(CaseFormat.LOWER_UNDERSCORE, str1));//student_name4.3 Lists/Sets/Maps
1.快速创建ArrayList并添加数据 List4.4 Ints/Longs/…list = new ArrayList<>(); list.add("a"); list.add("b"); list.add("c"); //提供集合的快速创建方式 List list1 = Lists.newArrayList("a", "b", "c","d"); // Set hashSet = Sets.newHashSet("a", "b", "c","d"); 2.把list2分成小的集合,小的集合的大小是size List > lists = Lists.partition(list1, 2); System.out.println(lists);//[[a, b], [c, d]]
1.创建一个Integer类型的集合,并添加数据 List4.5 Multisetlist = Ints.asList(1, 2, 3); System.out.println(list);//[1, 2, 3]
* Multiset--可以放重复元素 * List:元素可重复的有序集合 * set:元素不可重复的无序集合 HashMultiset4.6 HashMultiMapmultiset = HashMultiset.create(); multiset.add("a"); multiset.add("b"); multiset.add("c"); multiset.add("a"); System.out.println(multiset);//[a x 2, b, c] //各个元素出现的次数 Set > entries = multiset.entrySet(); for (Multiset.Entry entry : entries) { System.out.println("元素"+entry.getElement()+",个数:"+entry.getCount()); } System.out.println(multiset.entrySet());//[a x 2, b, c] System.out.println(multiset.elementSet());//[a, b, c]
*HashMultiMap用来替代jdk原生的Map4.7 ImmutableList> map Multimap multimap = HashMultimap.create(); multimap.put("a", "1"); multimap.put("b", "2"); Collection a = multimap.get("a"); System.out.println(multimap);//{a=[1], b=[2]} System.out.println(a);//[1] //是否包含key=a,value=1的entry System.out.println(multimap.containsEntry("a", "1")); //转换成jdk原生api实现的数据结构 Map > map = multimap.asMap(); System.out.println(map);//{a=[1], b=[2]}
*不可变集合 List4.8 Preconditionslist = new ArrayList<>(); list.add("aa"); list.add("bb"); //不可变集合 ImmutableList list1 = ImmutableList.builder().add("aa").build(); //list1.add("cc");报错不可添加 Collection collection = Collections.unmodifiableCollection(list); //collection.add("cc");报错不可添加 list.add("cc");//但是源头可添加--哈哈哈 System.out.println(collection);
*参数为空,抛出异常
String param = null;
Integer param1 = 4;
//if(param == null){
//throw new RuntimeException("参数不可为空!");
//}
Preconditions.checkNotNull(param,"参数不能为空");
//表达式为fasle抛出异常
Preconditions.checkArgument(param1 <=5,"参数大于5");
5.1FileCopyUtils
FileCopyUtils,文件复制不用关心流关闭
//获取类路径下的资源
ClassPathResource resource = new ClassPathResource("files/1.txt");
//设置编码
EncodedResource encodedResource = new EncodedResource(resource, StandardCharsets.UTF_8);
String targetPath = "D:\java\idea\IdeaProject2\utils-demo\src\main\resources\files\2.txt";
//复制文件1的内容到文件2
FileCopyUtils.copy(encodedResource.getInputStream(),new FileOutputStream(targetPath));



