好用的Java工具类
Character
方法 | 作用 |
---|---|
isLetter() |
是否是字母 |
isDigit() |
是否是数字 |
isWhitespace() |
是否是空白 |
isUpperCase() |
是否是大写字母 |
isLowerCase() |
是否是小写字母 |
toUpperCase() |
指定字母的大写形式 |
toLowerCase() |
指定字母的小写形式 |
toString() |
返回字符的字符串形式 |
Collections
排序
List<Integer> list = new ArrayList<>();
list.add(2);
list.add(1);
list.add(3);
Collections.sort(list);//升序
System.out.println(list);
Collections.reverse(list);//降序
System.out.println(list);
// 输出
// [1, 2, 3]
// [3, 2, 1]
查找
binarySearch(List list, Object key)
:二分查找法,前提是 List 已经排序过了max(Collection coll)
:返回最大元素max(Collection coll, Comparator comp)
:根据自定义比较器,返回最大元素min(Collection coll)
:返回最小元素min(Collection coll, Comparator comp)
:根据自定义比较器,返回最小元素fill(List list, Object obj)
:使用指定对象填充frequency(Collection c, Object o)
:返回指定对象出现的次数
public void test() {
List<String> list = Arrays.asList(split);
System.out.println("最小元素:" + Collections.min(list));
System.out.println("出现的次数:" + Collections.frequency(list, "to"));
System.out.println("最大元素:" + Collections.max(list));
// 没有排序直接调用二分查找,结果是不确定的
System.out.println("排序前的二分查找结果:" + Collections.binarySearch(list, "to"));
Collections.sort(list);
// 排序后,查找结果和预期一致
System.out.println("排序后的二分查找结果:" + Collections.binarySearch(list, "to"));
Collections.fill(list, "to");
System.out.println("填充后的结果:" + list);
}
// 最大元素:to
// 最小元素:Guoguo's
// 出现的次数:1
// 排序前的二分查找结果:1
// 排序后的二分查找结果:3
// 填充后的结果:[to, to, to, to]
Arrays
求和
int[] gas = { 1, 2, 3, 4, 5 };
int sum = Arrays.stream(gas).sum();
System.out.println(sum);
// 输出
// 15
比较
public void test() {
boolean equals = Arrays.equals(split, new String[] { "Welcome", "to", "Guoguo's", "blog!" });
boolean equals2 = Arrays.equals(split, new String[] { "Welcome", "to", "Guoguo's" });
System.out.println(equals);
System.out.println(equals2);
}
// true
// false
排序
public void test() {
String s = "Welcome to Guoguo's blog!";
String[] split = s.split(" ");
Arrays.sort(split);
for (String string : split) {
System.out.print(string + " ");
}
}
// Guoguo's Welcome blog! to
二维数组排序
public void test() {
int[][] nums = { { 1, 5 }, { 3, 2 }, { 4, 5 }, { 2, 3 } };
Arrays.sort(nums, (a, b) -> a[0] - b[0]);
for (int[] num : nums) {
System.out.print(num[0] + ", " + num[1] + ". ");
}
System.out.println();
Arrays.sort(nums, (a, b) -> a[1] - b[1]);
for (int[] num : nums) {
System.out.print(num[0] + ", " + num[1] + ". ");
}
}
// 1, 5. 2, 3. 3, 2. 4, 5.
// 3, 2. 2, 3. 1, 5. 4, 5.
查找
public void test() {
String s = "Welcome to Guoguo's blog!";
String[] split = s.split(" ");
Arrays.sort(split);
int binarySearch = Arrays.binarySearch(split, "to");
System.out.println(binarySearch);
}
// 3
打印
public void test() {
String s = "Welcome to Guoguo's blog!";
String[] split = s.split(" ");
System.out.println(split);
System.out.println(Arrays.toString(split));
}
// [Ljava.lang.String;@63c12fb0
// [Welcome, to, Guoguo's, blog!]
setAll
setAll()
方法提供了一个函数式编程的入口,可以对数组的元素进行填充
int[] array = new int[10];
Arrays.setAll(array, i -> i * 10);
System.out.println(Arrays.toString(array));
// [0, 10, 20, 30, 40, 50, 60, 70, 80, 90]
JUC
阻塞队列
ArrayBlockingQueue 是长度固定的队列,是一种阻塞队列,它在元素被插入或移除时会引发锁定。
轮转数组中ArrayBlockingQueue
的应用
import java.util.concurrent.ArrayBlockingQueue;
// 使用长为k的队列
public void rotate(int[] nums, int k) {
if (k == 0) // 避免 new ArrayBlockingQueue 出错
return;
int l = nums.length;
int offset = k % l; // k 可以大于数组长度
if (offset == 0) // 避免 new ArrayBlockingQueue 出错
return;
Queue<Integer> queue = new ArrayBlockingQueue<>(offset);
int left = 0, right = l - offset; // right 坐标 不能是 l - offset + 1
while (left < l) {
if (queue.offer(nums[left])) {
nums[left] = nums[right];
left++;
right++;
} else {
int temp = (int) queue.poll();
queue.offer(nums[left]);
nums[left] = temp;
left++;
}
}
}
Stream
计算字符串中指定字符出现的次数
// 使用流和 lambda 来实现计数
String someString = "elephant";
long count = someString.chars().filter(ch -> ch == 'e').count();
assertEquals(2, count);
long count2 = someString.codePoints().filter(ch -> ch == 'e').count();
assertEquals(2, count2);
Guava
将字符串分割为字符数组,再转为 List
import com.google.common.primitives.Chars;
public void test() {
String str = "Welcome to guoguo's blog!";
List<Character> asList = Chars.asList(s.toCharArray());
System.out.println(asList);
}
// [W, e, l, c, o, m, e, , t, o, , G, u, o, g , u, o, ', s, , b, l, o, g, !]
JDK 8 提供的 lambda 流式编程
public void test() {
String str = "Welcome to guoguo's blog!";
List<Character> asList = str.chars().mapToObj(c -> (char) c).collect(Collectors.toList());
System.out.println(asList);
}
// [W, e, l, c, o, m, e, , t, o, , G, u, o, g , u, o, ', s, , b, l, o, g, !]
好用的Java工具类
http://guoguo.host/blog/p/db71b5c1.html