文章目錄
- 前言
- 一、lambda
- 1. 排序
- 1.1 按照對象屬性排序:
- 1.2 字符串List排序:
- 1.3 數據庫排序jpa
- 2. 聚合
- 2.1 基本聚合(返回對象list)
- 2.2 多字段組合聚合(直接返回對象list數量)
- 二、基礎語法
- 2.1 List
- 2.1.1 數組初始化賦值
- 2.1.2. 逗號分割字符串、list互轉
- 2.1.3 去重
- 2.2. Json解析
- 2.2.1 Gson
- 2.2.2 Fastjson
- 2.3. LocalDateTime
- 2.3.1 String->LocalDateTime
- 2.3.2 Date->LocalDateTime
- 2.4. Optional
- 2.4.1 map/flatMap
- 2.4.2 ifPresent
- 2.4.3 filter
- 2.5. EsayExcel
- 2.5.1 導出示例
- 2.5.2 相關注解
- 三、 Linux命令
- 3.1 磁盤爆滿占用情況
- 3.2 nacos單機啟動命令
- 3.3 防火墻狀態查詢及操作
- 3.4 清理緩存(buff/cache)
前言
常用語法匯總
一、lambda
1. 排序
1.1 按照對象屬性排序:
List<AccidentEduSLResp> respList =list.stream().sorted(Comparator.comparing(AccidentEduSLResp::getOrgUnit)).collect(Collectors.toList());
district.sort(Comparator.comparing(AccidentEduSLResp::getOrgUnit)); //性能優
1.2 字符串List排序:
List<String> sortedDistrict = district.stream().sorted().collect(Collectors.toList());
1.3 數據庫排序jpa
dao.findAll(spec, Sort.by(Sort.Order.desc(ExpertConstants.UPDATE_TIME), Sort.Order.asc(ExpertConstants.EXAMINE_STATUS)));
2. 聚合
2.1 基本聚合(返回對象list)
Map<String, List<AccidentSupervisePO>> collect = cityList.stream().collect(Collectors.groupingBy(s -> s.getDistrictCode()));
2.2 多字段組合聚合(直接返回對象list數量)
Map<String, Long> collect = list.stream().collect(Collectors.groupingBy(o -> o.getDistrictCode() + "_" + o.getOrgUnit(), Collectors.counting()));
二、基礎語法
2.1 List
2.1.1 數組初始化賦值
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));
2.1.2. 逗號分割字符串、list互轉
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());
2.1.3 去重
用hashset去重list 性能優
List<String> testListDistinctResult = new ArrayList<>(new HashSet(testList));
2.2. Json解析
2.2.1 Gson
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);
2.2.2 Fastjson
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);
2.3. LocalDateTime
2.3.1 String->LocalDateTime
//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);
2.3.2 Date->LocalDateTime
//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);
2.4. Optional
2.4.1 map/flatMap
● map適用于基礎數據類型
● flatMap適用于對象類型
user不為空的時候取address address為空取city city為空異常
Optional.ofNullable(user).map(u-> u.getAddress()).map(a->a.getCity()).orElseThrow(()->new Exception("錯誤"));
2.4.2 ifPresent
user不為空時dosomething
Optional.ofNullable(user)
.ifPresent(u->{dosomething(u);
});
2.4.3 filter
如果user的name的是zhangsan的,則返回當前對象。否則返回構造的user對象。
Optional.ofNullable(user)
.filter(u->"zhangsan".equals(u.getName()))
.orElseGet(()-> {User user1 = new User();user1.setName("zhangsan");return user1;
});
2.5. EsayExcel
2.5.1 導出示例
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());
2.5.2 相關注解
//不映射excel
@ExcelIgnore //列寬 class上控制全部字段 屬性上控制該屬性字段
@ColumnWidth(40) //2.1.4 表頭名稱及排序
//@ExcelProperty(order = 2) 2.2.7 排序寫法 index被兼容
@ExcelProperty(value = "任務內容", index = 1)
三、 Linux命令
3.1 磁盤爆滿占用情況
df -h
du -h --max-depth=1
3.2 nacos單機啟動命令
sh startup.sh -m standalone
3.3 防火墻狀態查詢及操作
systemctl stop firewalld
systemctl status firewalld
systemctl disable firewalld
3.4 清理緩存(buff/cache)
清除前后使用 free -h 查看效果
free -h
sysctl -w vm.drop_caches=3
free -h