Java中的List操作
1. 数组转List
String[] arr={"1","2","3"};
List<String> list = Arrays.asList(arr);
2. list转字符串 类似string.join
import org.springframework.util.StringUtils; String[] arr={"1","2","3"}; List<String> list = Arrays.asList(arr); String string= StringUtils.collectionToDelimitedString(list,",");
3. list转数组
Long[] arr= (Long[]) list.toArray(new Long[list.size()]);
4. list+stream去重操作
List<Integer> list = new ArrayList<>(); list.add(1); list.add(2); list.add(3); list.add(4); list.add(1); List<Integer> a = list.stream().distinct().collect(Collectors.toList());
如果根据某个属性去重的话,暂时没有想到更简洁的代码,只能定义一个list,循环往list里添加, 添加的时候验证key是否已经存在,如果已经存在了就跳过。
5. list转map
Map<Long, BucketInfo> map = list.stream().collect(Collectors.toMap(BucketInfo::getTimestamp, (p) -> p));
6. 排序
//倒序 rstUserList = rstUserList.stream().sorted((y, x) -> Long.compare(x.time, y.time)).collect(Collectors.toList());