原创

Java-List专题-newArrayList-创建空的list对象

1、创建空的list对象
return Collections.emptyList() ×;
return Lists.newArrayList() ×; 以上两种只适合做最后请求的返回,或代码路中只对结果进行读取,无法增删改,因为没有重写其中的一些方法
return CollUtil.newArrayList() √; cn.hutool.core.collection

2、list初始化赋值
示例a:
private static List<String> list = new ArrayList<String>(){
{
add("/auth/contrMemberLogin");
add("/courseReportTask/syncTask");
add("/withoutLogin/**");
}
};

//遍历正则判断list【可能还有问题】
private boolean isSkipMatch(String uri){
for (String reg : list) {
boolean flag=uri.matches(reg);// /withoutLogin/** 这个判断报错。Dangling meta character '*' near index 15\r\n/withoutLogin/**\r\n
if(flag){
return true;
}
}
return false;
}

示例b:
private final static List<String> sumlist4中西生化处理合计 = Lists.newArrayList("洗煤沉降槽出水", "甲醇一厂生产地排水"
, "烯烃一厂生产地排水", "化一生产地排水", "化二生产地排水"
, "焦油加工生产污水", "苯加氢生产污水", "苯加氢生产污水", "异构生产废水", "碳四生产废水", "化一粗苯分离水", "甲醇一厂小精馏废水", "南区生活污水");
private final static List<String> sumlist4东生化处理合计 = Lists.newArrayList("甲醇一厂低甲排水", "烯烃一厂MTO装置废水");
private final static List<String> sumlist4一期污水处理合计 = new ArrayList<String>() {{
addAll(sumlist4中西生化处理合计);
addAll(sumlist4东生化处理合计);
}};

3、list分割批量执行
package com.google.common.collect.Lists;
Lists.partition(list, 30).forEach(subList-> {
// 批量操作
......
});

4、List<实体> 转成 list<Map<String, Object>>
public static void main(String[] args) {
List<LaborReceiveMateriaRequisitionDetailListVo> retainProductList =Lists.newArrayList();
LaborReceiveMateriaRequisitionDetailListVo v = new LaborReceiveMateriaRequisitionDetailListVo();
v.setBumen("bumen");
v.setDanwei("danwei");
retainProductList.add(v);
List<Map<String, Object>> maps = convertListEntityToListMap(retainProductList);
System.out.println(maps);
List<Map<String, Object>> maplist =Lists.newArrayList();
Map<String, Object> map = new HashMap<>();
map.put("ee","2");
maplist.add(map);
maps = convertListEntityToListMap(maplist);
System.out.println(maps);
}

private static <T> List<Map<String, Object>> convertListEntityToListMap(List<T> list) {
if (list == null) {
return CollectionUtil.newArrayList();
}
List<Map<String, Object>> result = Lists.newArrayList();
// if (CollectionUtil.isNotEmpty(list) && list.get(0) instanceof Map) {
// return (List<Map<String, Object>>) list;
// [{"uniqueTag":"$","uniqueTagByCode":"$","danwei":"danwei","bumen":"bumen"}]
// [{ee=2}]
// }
list.forEach(e -> {
Map<String, Object> map = (Map<String, Object>) JSON.parse(JSON.toJSONString(e));
result.add(map);
});
return result;
}

5、List<Map<String, Object>> 转成 list<实体>
//JSON.toJSONString是关键
List<实体> list = JSON.parseArray(JSON.toJSONString(mapList), 实体.class);

6、判断list是否相等
public static <T> boolean isEqualList(List<T> list1, List<T> list2) {
boolean isEmpty1 = CollectionUtils.isEmpty(list1); // []
boolean isEmpty2 = CollectionUtils.isEmpty(list2); // [1]
if (isEmpty1 && isEmpty2) {
return true;
}
if (isEmpty1) {
return false;
}
if (isEmpty2) {
return false;
}
if (list1.size() != list2.size()) {
return false;
}

for (int i = 0; i < list1.size(); i++) {
T element1 = list1.get(i);
T element2 = list2.get(i);
if (!element1.equals(element2)) {
return false;
}
}
return true;

}



正文到此结束
本文目录