JDK工具类

64 阅读4分钟

1. Integer 整数工具类

int parseInt(String s);                      // 字符串转整数
int parseInt(String s, int radix);           // 字符串转整数
Integer decode(String s);                    // 字符串编码转整数

String toBinaryString(int i);                // 整数转二进制字符串
String toOctalString(int i);                 // 整数转八进制字符串
String toHexString(int i);                   // 整数转十六进制字符串

int bitCount(int i);                         // 获取二进制形式中1的个数

2. Character 字符工具类

boolean isLowerCase(char ch);                // 是否是小写
boolean isUpperCase(char ch);                // 是否是大写

char toLowerCase(char ch);                   // 字母转小写
char toUpperCase(char ch);                   // 字母转大写

char reverseBytes(char ch);                  // 按字节反转

int digit(char ch, int radix);               // 获取该进制下字符的数值
char forDigit(int digit, int radix);         // 获取该进制下数值的字符

3. String 字符串工具类

int length();                                      // 获取字符串长度

boolean isEmpty();                                 // 字符串是否为空
boolean isBlank();                                 // 字符串是否有值

byte[] getBytes();                                 // 字符串转字节数组
char[] toCharArray();                              // 字符串转字符数组

String toLowerCase();                              // 字符串转小写
String toUpperCase();                              // 字符串转大写

String strip();                                    // 去除前后空格
String stripLeading();                             // 去除前导空格
String stripTrailing();                            // 去除尾随空格

char charAt(int index);                            // 获取指定下标字符
int codePointAt(int index);                        // 获取指定下标字符码

int indexOf(int ch);                               // 获取字符所在下标
int indexOf(int ch, int fromIndex);                // 获取字符所在下标
int lastIndexOf(int ch);                           // 获取字符所在下标
int lastIndexOf(int ch, int fromIndex);            // 获取字符所在下标

int compareTo(String str);                         // 字符串大小比较
int compareToIgnoreCase(String str);               // 字符串大小比较

boolean equals(Object obj);                        // 字符串是否相等
boolean equalsIgnoreCase(String str);              // 字符串忽略大小写
boolean regionMatches(int l1, String str, int l2, int len);

boolean contains(CharSequence s);                  // 字符串是否包含
boolean startsWith(String prefix);                 // 字符串前缀判断
boolean endsWith(String suffix);                   // 字符串后缀判断

String substring(int beginIndex);                  // 字符串截取
String substring(int beginIndex, int endIndex);    // 字符串局部截取

String concat(String str);                         // 字符串拼接
String repeat(int count);                          // 字符串多次拼接

String replace(char oldChar, char newChar);        // 字符串替换
String replaceAll(String regex, String str);       // 字符串全部替换
String replaceFirst(String regex, String str);     // 字符串首个替换

String[] split(String regex);                      // 字符串分割
String join(String dlt, String... es);             // 字符串拼接

String formatted(Object... args);                  // 字符串格式化
String format(String format, Object... args);      // 字符串格式化

4. Arrays 数组工具类

List<T> asList(T... a);                            // 数组转列表

boolean equals(int[] a, int[] b);                  // 数组是否相同
int compare(int[] a, int[] b);                     // 数组元素比较
int mismatch(int[] a, int[] b);                    // 数组首个不同元素下标

int[] copyOf(int[] a, int length);                 // 数组复制
int[] copyOfRange(int[] a, int from, int to);      // 数组局部复制

void fill(int[] a, int val);                       // 数组填充
void setAll(int[] a, IntUnaryOperator generator);  // 数组填充

void sort(int[] a);                                // 数组排序
void sort(T[] a, Comparator<T> c);                 // 数组排序
void parallelSort(int[] a);                        // 大型数组排序

int binarySearch(int[] a, int key);                // 二分查找

5. Collections 集合工具类

List<T> emptyList();                                       // 空集合

void sort(List<T> list);                                   // 集合排序
void sort(List<T> list, Comparator<T> c);                  // 集合排序
void shuffle(List<?> list);                                // 集合随机排序

void reverse(List<?> list);                                // 集合反转
void rotate(List<?> list, int distance);                   // 集合旋转

void fill(List<T> list, T obj);                            // 集合填充
boolean addAll(Collection<T> c, T... elements);            // 集合追加

T max(Collection<T> coll);                                 // 获取最大值
T min(Collection<T> coll);                                 // 获取最小值

int binarySearch(List<T> list, T key);                     // 获取元素下标
int frequency(Collection<?> c, Object o);                  // 获取出现次数
int indexOfSubList(List<?> source, List<?> target);        // 获取子集合下标
int lastIndexOfSubList(List<?> source, List<?> target);    // 获取子集合下标

void swap(List<?> list, int i, int j);                     // 元素交换
boolean replaceAll(List<T> list, T oldVal, T newVal);      // 元素替换

boolean disjoint(Collection<?> c1, Collection<?> c2);      // 是否不相交

6. Files 文件工具类

Path of(String first, String... more);                                     // 路径拼接

boolean exists(Path path, LinkOption... options);                          // 是否存在
boolean notExists(Path path, LinkOption... options);                       // 是否存在
boolean isReadable(Path path);                                             // 是否可读
boolean isWritable(Path path);                                             // 是否可写
boolean isDirectory(Path path, LinkOption... options);                     // 是否是目录

String readString(Path path);                                              // 文件读取
Path writeString(Path path, CharSequence csq, OpenOption... options);      // 文件写入
Path copy(Path src, Path tag, CopyOption... options);                      // 文件复制
Path move(Path source, Path target, CopyOption... options);                // 文件移动
boolean deleteIfExists(Path path);                                         // 文件删除

Path createFile(Path path, FileAttribute... attrs);                        // 创建文件
Path createTempFile(String name, String suffix, FileAttribute... attrs);   // 创建系统临时文件

Path createDirectory(Path dir, FileAttribute... attrs);                    // 创建单级目录
Path createDirectories(Path dir, FileAttribute... attrs);                  // 创建多级目录
Path createTempDirectory(String dir, FileAttribute... attrs);              // 创建系统临时目录

long size(Path path);                                                      // 获取文件大小
String probeContentType(Path path);                                        // 获取文件类型
FileTime getLastModifiedTime(Path path, LinkOption... options);            // 获取修改时间
UserPrincipal getOwner(Path path, LinkOption... options);                  // 获取文件所有者
Object getAttribute(Path path, String attr, LinkOption... options);        // 获取文件单个属性
V getFileAttributeView(Path path, Class<V> type, LinkOption... options);   // 获取文件所有属性