Java函数库中有哪些常用的函数和工具类,如何使用它们?
Java函数库包含了大量的函数和工具类,这些函数和工具可以方便地提高程序设计的效率,简化了常用的程序设计任务。本文将介绍Java函数库中常用的一些函数和工具类,并说明如何使用它们。
一、常用的函数
1. 字符串处理函数
1)字符串切割函数split
语法:String[] split(String regex)
作用:将字符串根据regex所指定的分隔符进行分割,并返回分割后的字符串数组。
例如:
String str = "Hello,world!";
String[] arr = str.split(",");
for (String s : arr) {
System.out.println(s);
}
输出结果:
Hello
world!
2)字符串替换函数replace
语法:String replace(CharSequence target, CharSequence replacement)
作用:将字符串中匹配到的 个target字符串替换成replacement字符串。
例如:
String str = "Hello,world!";
String newStr = str.replace("world", "Java");
System.out.println(newStr);
输出结果:
Hello,Java!
2. 数组处理函数
1)数组遍历函数forEach
语法:void forEach(T[] array, Consumer<? super T> action)
作用:将一个数组中的元素全部取出,并进行操作。
例如:
String[] arr = {"Hello","Java","World"};
Arrays.stream(arr).forEach(e-> System.out.println(e));
输出结果:
Hello
Java
World
2)数组排序函数sort
语法:void sort(int[] a)
作用:将一个数组进行排序。
例如:
int[] arr = {6, 4, 3, 8, 9, 7};
Arrays.sort(arr);
for (int i : arr) {
System.out.println(i);
}
输出结果:
3
4
6
7
8
9
3. 时间处理函数
1)获取当前时间函数now
语法:Instant.now()
作用:获取当前时间。
例如:
Instant now = Instant.now();
System.out.println(now);
输出结果:
2022-04-14T11:22:53.869Z
2)时间格式化函数format
语法:String format(TemporalAccessor temporal)
作用:将时间按照指定的格式进行输出。
例如:
Instant now = Instant.now();
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
System.out.println(formatter.format(now));
输出结果:
2022-04-14 11:23:42
4. 文件处理函数
1)文件读取函数readAllBytes
语法:byte[] readAllBytes(Path path) throws IOException
作用:从指定的path路径读取文件的所有字节,返回字节数组。
例如:
Path path = Paths.get("/home/user/test.txt");
byte[] bytes = Files.readAllBytes(path);
2)文件输出函数write
语法:void write(byte[] b, Path file, OpenOption... options) throws IOException
作用:将字节数组b写入到指定的文件中。
例如:
Path path = Paths.get("/home/user/test.txt");
String content = "Hello,Java!";
Files.write(path, content.getBytes());
二、常用的工具类
1. 字符串工具类String
1)判断字符串是否为空
语法:static boolean isEmpty(CharSequence str)
例如:
String str = "";
if (StringUtils.isEmpty(str)) {
System.out.println("字符串为空");
}
输出结果:
字符串为空
2)拼接字符串
语法:static String join(CharSequence delimiter, CharSequence... elements)
例如:
String[] arr = {"Hello","Java","World"};
String str = StringUtils.join(",", arr);
System.out.println(str);
输出结果:
Hello,Java,World
2. 数组工具类Arrays
1)输出数组内容
语法:static String toString(Object[] a)
例如:
String[] arr = {"Hello","Java","World"};
System.out.println(Arrays.toString(arr));
输出结果:
[Hello, Java, World]
2)数组复制
语法:static copyOf(T[] original, int newLength)
例如:
int[] arr = {6, 4, 3, 8, 9, 7};
int[] newArr = Arrays.copyOf(arr, 3);
System.out.println(Arrays.toString(newArr));
输出结果:
[6, 4, 3]
3. 时间工具类Instant
1)时间比较
语法:int compareTo(Instant otherInstant)
例如:
Instant now = Instant.now();
Instant other = Instant.parse("2022-04-14T11:15:00.00Z");
if (now.compareTo(other) > 0) {
System.out.println("当前时间大于other时间");
}
输出结果:
当前时间大于other时间
2)时间比较器
语法:static InstantComparator comparing(ToLongFunction<? super Instant> keyExtractor)
例如:
Instant now = Instant.now();
Instant other = Instant.parse("2022-04-14T11:15:00.00Z");
List<Instant> list = Arrays.asList(now, other);
list.sort(InstantComparator.comparing(Instant::toEpochMilli).reversed());
System.out.println(list);
输出结果:
[2022-04-14T11:24:08.693Z, 2022-04-14T11:15:00Z]
4. 文件工具类Files
1)删除文件
语法:static void deleteIfExists(Path path) throws IOException
例如:
Path path = Paths.get("/home/user/test.txt");
Files.deleteIfExists(path);
2)判断文件是否存在
语法:static boolean exists(Path path, LinkOption... options)
例如:
Path path = Paths.get("/home/user/test.txt");
if (Files.exists(path)) {
System.out.println("文件存在");
}
输出结果:
文件存在
以上是Java函数库中常用的函数和工具类,使用时只需引入相应的库,即可使用其中提供的函数和工具类。这不仅提高了程序设计的效率,也提升了程序的易读性和易维护性。
