java开发常用知识点整理

188 阅读1分钟

1.字符串拼接

使用String中的join方法

集合或者数组非空:

 List<String> names = Arrays.asList("张三","李四","王五");
//        List<String> names=new ArrayList<>();        
System.out.println(String.join("*",names));

集合中数据为空:

List<String> names=new ArrayList<>();
System.out.println(String.join("*",names).equals(""));//true

集合空:

List<String> names=null;
System.out.println(String.join("*",names)
//出现空指针异常

解决方法:

当不知道是否为空时又想使用的办法:

List<String> names = null;
List<String> names1= Optional.ofNullable(names).orElseGet(ArrayList::new);
System.out.println(String.join("*",names1).equals(""));//true

2.保留2位小数

直接丢掉了2位后面的所有,少的就用0补充

BigDecimal decimal = new BigDecimal("1");
BigDecimal bigDecimal = decimal.setScale(2, RoundingMode.DOWN);
System.out.println(bigDecimal.toPlainString());

3.时间转字符串 字符串转时间 

使用hutool的工具

4.返回给前台的文件名称乱码

response.setHeader("Content-Disposition","attachment;filename="+ URLEncoder.encode(fileName ,"utf-8"));

5.对list集合分组

List<CarModel> carModels=new ArrayList<>();
carModels.add(new CarModel("奔驰",10.00));
carModels.add(new CarModel("奔驰",11.00));
carModels.add(new CarModel("奔驰",12.00));
carModels.add(new CarModel("宝马",10.00));
carModels.add(new CarModel("宝马",10.00));
LinkedHashMap<String, List<CarModel>> collect = carModels.stream().collect(Collectors.groupingBy(CarModel::getName, LinkedHashMap::new, Collectors.toList()));
System.out.println(collect.toString());

6.线程工具CompleteFuture

7.java中处理文件时无法删除

未进行关闭

8.克隆一个文件

要克隆一个文件的流

9.新poi中常用的样式设置

 //设置单元格背景颜色
        Sheet sheet=workbook.getSheetAt(0);
        Row row=sheet.getRow(0);
        Cell cell=row.getCell(0);
        CellStyle style = workbook.createCellStyle();
        style.cloneStyleFrom(cell.getCellStyle());
        //设置背景颜色
        style.setFillForegroundColor(IndexedColors.BLUE_GREY.getIndex());
        style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
        //设置自动换行
        style.setWrapText(true);
 
        //设置字体样式
        Font font= row.getSheet().getWorkbook().createFont();
        //默认字体为宋体
        font.setFontName("宋体");
        //设置字体大小
        font.setFontHeight((short) 18);
        //设置字体颜色
        font.setColor(IndexedColors.BLUE_GREY.getIndex());
        //设置字体加粗
        font.setBold(true);
        //设置字体斜体
        font.setItalic(true);
        //设置字体下划线
        font.setUnderline(Font.U_SINGLE);
        //设置字体上标下标
        font.setTypeOffset(Font.SS_SUPER);
        //设置字体删除线
        font.setStrikeout(true);
        style.setFont(font);
 
        //边框样式
        //设置上边框线条类型
        style.setBorderTop(BorderStyle.THIN);
        //设置右边框线条类型
        style.setBorderRight(BorderStyle.THIN);
        //设置下边框线条类型
        style.setBorderBottom(BorderStyle.THIN);
        //设置左边框线条类型
        style.setBorderLeft(BorderStyle.THIN);
        //设置上边框线条颜色
        style.setTopBorderColor(IndexedColors.BLUE_GREY.getIndex());
        //设置右边框线条颜色
        style.setRightBorderColor(IndexedColors.BLUE_GREY.getIndex());
        //设置下边框线条颜色
        style.setBottomBorderColor(IndexedColors.BLUE_GREY.getIndex());
        //设置左边框线条颜色
        style.setLeftBorderColor(IndexedColors.BLUE_GREY.getIndex());
 
        //对齐方式
        //设置水平对齐方式
        style.setAlignment(HorizontalAlignment.CENTER);
        //设置垂直对齐方式
        style.setVerticalAlignment(VerticalAlignment.CENTER);
 
        //设置列宽行高
        //设置自适应列宽
        sheet.setDefaultColumnWidth(0);
        //自定义列宽
        sheet.setColumnWidth(0,10);
        //自定义行高
        row.setHeight((short)10);