常用语法汇总
List<AccidentEduSLResp> respList =list.stream().sorted(Comparator.comparing(AccidentEduSLResp::getOrgUnit)).collect(Collectors.toList());
district.sort(Comparator.comparing(AccidentEduSLResp::getOrgUnit)); //性能优
List<String> sortedDistrict = district.stream().sorted().collect(Collectors.toList());
dao.findAll(spec, Sort.by(Sort.Order.desc(ExpertConstants.UPDATE_TIME), Sort.Order.asc(ExpertConstants.EXAMINE_STATUS)));
Map<String, List<AccidentSupervisePO>> collect = cityList.stream().collect(Collectors.groupingBy(s -> s.getDistrictCode()));
Map<String, Long> collect = list.stream().collect(Collectors.groupingBy(o -> o.getDistrictCode() + "_" + o.getOrgUnit(), Collectors.counting()));
Arrays.asList 不允许add remove操作 UnsupportedOperationException
public static final List CITY_ARR = Arrays.asList("230100","230200","230300","230400","230500","230600");
需要add等操作需要以下写法支持
List<String> list = new ArrayList<>(Arrays.asList(arr));
List 转字符串:
Joiner.on(",").join(list)
字符串转List:
//-> 引入guava-28.2-jre.jar//-> CommonConstants常量类定义
public static final Splitter SPLITTER_COMMA = Splitter.on(",").omitEmptyStrings().trimResults();
//需要判断字符串是否为空
List<String> list = CommonConstants.SPLITTER_COMMA.splitToList(a.getDistrictCode());
用hashset去重list 性能优
List<String> testListDistinctResult = new ArrayList<>(new HashSet(testList));
Book b = new Book("书名1","简介1");
//使用gson将对象转为json字符串
String json = new Gson().toJson(b);
System.out.println(json);
//使用gson将json字符转转为对象(第一个参数为json字符串,第二个参数为要转为的类)
Book b2 = new Gson().fromJson("{\\"name\\":\\"书名1\\",\\"info\\":\\"简介1\\"}",Book.class);
Book b = new Book("书名2","简介2");
//使用fastjson将对象转为json字符串
String json= JSON.toJSONString(b);
System.out.println(json);
//使用fastjson将json字符转转为对象(第一个参数为json字符串,第二个参数为要转为的类)
Book b2 = JSON.parseObject("{\\"name\\":\\"书名1\\",\\"info\\":\\"简介1\\"}", Book.class);
//1.具有转换功能的对象
DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
//2.要转换的对象
LocalDateTime time = LocalDateTime.now();
//3.发动功能
String localTime = df.format(time);
System.out.println("LocalDateTime转成String类型的时间:"+localTime);
//3.LocalDate发动,将字符串转换成 df格式的LocalDateTime对象,的功能
LocalDateTime LocalTime = LocalDateTime.parse(strLocalTime,df)
System.out.println("String类型的时间转成LocalDateTime:"+LocalTime);
//Date转LocalDateTime
Date date = new Date();
Instant instant = date.toInstant();
ZoneId zoneId = ZoneId.systemDefault();
LocalDateTime localDateTime = instant.atZone(zoneId).toLocalDateTime();
System.out.println("Date = " + date);
System.out.println("LocalDateTime = " + localDateTime);
//LocalDateTime转Date
ZoneId zoneId = ZoneId.systemDefault();
LocalDateTime localDateTime = LocalDateTime.now();
ZonedDateTime zdt = localDateTime.atZone(zoneId);
Date date = Date.from(zdt.toInstant());
System.out.println("LocalDateTime = " + localDateTime);
System.out.println("Date = " + date);
● map适用于基础数据类型
● flatMap适用于对象类型
user不为空的时候取address address为空取city city为空异常
Optional.ofNullable(user)
.map(u-> u.getAddress())
.map(a->a.getCity())
.orElseThrow(()->new Exception("错误"));
user不为空时dosomething
Optional.ofNullable(user)
.ifPresent(u->{
dosomething(u);
});
如果user的name的是zhangsan的,则返回当前对象。否则返回构造的user对象。
Optional.ofNullable(user)
.filter(u->"zhangsan".equals(u.getName()))
.orElseGet(()-> {
User user1 = new User();
user1.setName("zhangsan");
return user1;
});
response.setContentType("application/octet-stream");
response.setCharacterEncoding("utf-8");
// 这里URLEncoder.encode可以防止中文乱码 当然和easyExcel没有关系
String fileName = URLEncoder.encode("专家报销汇总" + DateUtil.localDateToString(LocalDate.now()), "UTF-8");
response.setHeader("Content-Disposition", "attachment; filename=" + fileName + ".xlsx");
// 前端需要FileName头否则会会有问题
response.setHeader("FileName", fileName + ".xlsx");
response.setHeader("Access-Control-Expose-Headers", "FileName");
WriteCellStyle contentWriteCellStyle = new WriteCellStyle();
contentWriteCellStyle.setHorizontalAlignment(HorizontalAlignment.CENTER);
WriteCellStyle headWriteCellStyle = new WriteCellStyle();
HorizontalCellStyleStrategy horizontalCellStyleStrategy =
new HorizontalCellStyleStrategy(headWriteCellStyle, contentWriteCellStyle);
EasyExcel//将数据映射到DownloadDTO实体类并响应到浏览器
.write(response.getOutputStream(), ExpertAndCostInfoOutDTO.class)
//07的excel版本,节省内存
.excelType(ExcelTypeEnum.XLSX)
//是否自动关闭输入流
.autoCloseStream(Boolean.TRUE)
.registerWriteHandler(horizontalCellStyleStrategy)
.sheet("专家报销汇总").doWrite(findExpertAndCostGatherList(dto).getExpertList());
//不映射excel
@ExcelIgnore
//列宽 class上控制全部字段 属性上控制该属性字段
@ColumnWidth(40)
//2.1.4 表头名称及排序
//@ExcelProperty(order = 2) 2.2.7 排序写法 index被兼容
@ExcelProperty(value = "任务内容", index = 1)
df -h
du -h --max-depth=1
sh startup.sh -m standalone
systemctl stop firewalld
systemctl status firewalld
systemctl disable firewalld
清除前后使用 free -h 查看效果
free -h
sysctl -w vm.drop_caches=3
free -h