目录
一、列表
1、List转数组
List<String> list = new ArrayList<>();
list.add("a");
list.add("b");
list.add("c");
String[] arr = new String[list.size()];
arr = list.toArray(arr);
System.out.println(Arrays.toString(arr));
2、列表为Null转空列表
package org.apache.commons.collections4
使用: ListUtils.emptyIfNull(需处理的list)
3、list拆分
package com.google.common.collect;
使用:Lists.partition(待处理的list,处理后子list的size)
二、数字处理
4、获取随机整数
import java.util.*;
// 生成随机数(区间为 [0,bound) )
Random random = new Random();
// 生成[0,100]的整数;如果需要的区间不是0开头的,可以对随机的结果值做加减法
Integer temp = random.nextInt(101);
三、字典处理
1、map按照value值进行降序排列
Map<Integer, Integer> map = new HashMap<Integer, Integer>();
// map按照value排序
List<Map.Entry<Integer, Integer>> list = new ArrayList<>(map.entrySet());
list.sort((a, b) -> {
return b.getValue() - a.getValue();
});
// 打印输出key、value
for (Map.Entry<Integer, Integer> ele : list) {
System.out.println(ele.getKey() + ":" + ele.getValue());
}