Java常用工具类

Collections类

排序

1
2
3
4
5
6
7
8
9
// 看看使用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
4
5
6
7
8
9
// 有时候需要找出集合中的最大值或者最小值,这时可以使用Collections的max和min方法
List<Integer> list = new ArrayList<>();
list.add(2);
list.add(1);
list.add(3);
Integer max = Collections.max(list);//获取最大值
Integer min = Collections.min(list);//获取最小值
System.out.println(max);
System.out.println(min);

转换线程安全集合

1
2
3
4
5
6
7
// java中的很多集合,比如:ArrayList、LinkedList、HashMap、HashSet等,都是线程不安全的 这些集合在多线程的环境中,添加数据会出现异常
List<Integer> list = new ArrayList<>();
list.add(2);
list.add(1);
list.add(3);
List<Integer> integers = Collections.synchronizedList(list);//将ArrayList转换成线程安全集合
System.out.println(integers);

返回空集合

1
2
3
4
5
6
7
8
// 我们在判空之后,需要返回空集合,就可以使用emptyList方法
private List<Integer> fun(List<Integer> list) {
    if (list == null || list.size() == 0) {
        return Collections.emptyList();
    }
    //业务处理
    return list;
}

二分查找

1
2
3
4
5
6
7
8
// binarySearch方法提供了一个非常好用的二分查找功能,只用传入指定集合和需要找到的key即可
List<Integer> list = new ArrayList<>();
list.add(2);
list.add(1);
list.add(3);

int i = Collections.binarySearch(list, 3);//二分查找
System.out.println(i );

转换成不可修改集合

1
2
3
4
5
6
7
8
9
// 为了防止后续的程序把某个集合的结果修改了,有时候我们需要把某个集合定义成不可修改的,使用Collections的unmodifiablexxx方法就能轻松实现
List<Integer> list = new ArrayList<>();
list.add(2);
list.add(1);
list.add(3);

List<Integer> integers = Collections.unmodifiableList(list);
integers.add(4);
System.out.println(integers);

apache的CollectionUtils工具类

集合判空

1
2
3
4
5
6
7
8
9
10
11
12
13
// 通过CollectionUtils工具类的isEmpty方法可以轻松判断集合是否为空,isNotEmpty方法判断集合不为空。
List<Integer> list = new ArrayList<>();
list.add(2);
list.add(1);
list.add(3);

if (CollectionUtils.isEmpty(list)) {
    System.out.println("集合为空");
}

if (CollectionUtils.isNotEmpty(list)) {
    System.out.println("集合不为空");
}

对两个集合进行操作

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
// 有时候我们需要对已有的两个集合进行操作,比如取交集或者并集
List<Integer> list = new ArrayList<>();
list.add(2);
list.add(1);
list.add(3);

List<Integer> list2 = new ArrayList<>();
list2.add(2);
list2.add(4);

//获取并集
Collection<Integer> unionList = CollectionUtils.union(list, list2);
System.out.println(unionList);

//获取交集
Collection<Integer> intersectionList = CollectionUtils.intersection(list, list2);
System.out.println(intersectionList);

//获取交集的补集
Collection<Integer> disjunctionList = CollectionUtils.disjunction(list, list2);
System.out.println(disjunctionList);

//获取差集
Collection<Integer> subtractList = CollectionUtils.subtract(list, list2);
System.out.println(subtractList);

google.guava Lists

创建空集合

1
List<Integer> list = Lists.newArrayList();

快速初始化集合

1
List<Integer> list = Lists.newArrayList(123);

笛卡尔积

1
2
3
4
List<Integer> list1 = Lists.newArrayList(123);
List<Integer> list2 = Lists.newArrayList(4,5);
List<List<Integer>> productList = Lists.cartesianProduct(list1,list2);
System.out.println(productList);

分页

1
2
3
4
// 如果你想将一个大集合分成若干个小集合,可以使用Lists的partition方法
List<Integer> list = Lists.newArrayList(12345);
List<List<Integer>> partitionList = Lists.partition(list, 2);
System.out.println(partitionList);

流处理

1
2
3
4
// 想把某个集合转换成另外一个接口,可以使用Lists的transform方法
List<String> list = Lists.newArrayList("a","b","c");
List<String> transformList = Lists.transform(list, x -> x.toUpperCase());
System.out.println(transformList);

颠倒顺序

1
2
3
List<Integer> list = Lists.newArrayList(312);
List<Integer> reverseList = Lists.reverse(list);
System.out.println(reverseList);

Objects

对象判空

1
2
3
4
5
6
7
8
9
10
// Objects的isNull方法判断对象是否为空,而nonNull方法判断对象是否不为空
Integer integer = new Integer(1);

if (Objects.isNull(integer)) {
    System.out.println("对象为空");
}

if (Objects.nonNull(integer)) {
    System.out.println("对象不为空");
}

对象为空抛异常

1
2
3
4
5
6
// 在对象为空时,抛出空指针异常,可以使用Objects的requireNonNull方法
Integer integer1 = new Integer(128);

Objects.requireNonNull(integer1);
Objects.requireNonNull(integer1, "参数不能为空");
Objects.requireNonNull(integer1, () -> "参数不能为空");

判断两个对象是否相等

1
2
3
4
5
// 判断两个对象是否相等,Objects给我们提供了equals方法
Integer integer1 = new Integer(1);
Integer integer2 = new Integer(1);

System.out.println(Objects.equals(integer1, integer2));

BooleanUtils

转换成数字

1
2
3
4
5
// 将true转换成数字1,false转换成数字0,可以使用toInteger方法
Boolean aBoolean = new Boolean(true);
Boolean aBoolean1 = new Boolean(false);
System.out.println(BooleanUtils.toInteger(aBoolean));
System.out.println(BooleanUtils.toInteger(aBoolean1));

Boolean转换成布尔值

1
2
3
4
5
6
// 将包装类Boolean对象,转换成原始的boolean对象,可以使用toBoolean方法
Boolean aBoolean = new Boolean(true);
Boolean aBoolean1 = null;
System.out.println(BooleanUtils.toBoolean(aBoolean));
System.out.println(BooleanUtils.toBoolean(aBoolean1));
System.out.println(BooleanUtils.toBooleanDefaultIfNull(aBoolean1, false));

Assert

断言参数是否为空

1
2
3
4
5
// 断言参数是否空,如果不满足条件,则直接抛异常
String str = null;
Assert.isNull(str, "str必须为空");
Assert.isNull(str, () -> "str必须为空");
Assert.notNull(str, "str不能为空");

断言集合是否为空

1
2
3
4
5
6
// 断言集合是否空,如果不满足条件,则直接抛异常
List<String> list = null;
Map<String, String> map = null;
Assert.notEmpty(list, "list不能为空");
Assert.notEmpty(list, () -> "list不能为空");
Assert.notEmpty(map, "map不能为空");

断言条件是否为空

1
2
3
4
// 断言是否满足某个条件,如果不满足条件,则直接抛异常
List<String> list = null;
Assert.isTrue(CollectionUtils.isNotEmpty(list), "list不能为空");
Assert.isTrue(CollectionUtils.isNotEmpty(list), () -> "list不能为空");