海风影像馆 海风影像馆
首页
扎根医疗
学习笔记
技术实战
项目管理
影像空间
驴行天下
阳明心学
  • 学习方法
  • 心情杂货
  • 实用技巧
  • 友情链接
  • 分类
  • 标签
  • 归档

知行旅人

生有热烈,藏于俗常。
首页
扎根医疗
学习笔记
技术实战
项目管理
影像空间
驴行天下
阳明心学
  • 学习方法
  • 心情杂货
  • 实用技巧
  • 友情链接
  • 分类
  • 标签
  • 归档
  • 编程笔记

    • ES查询压测
    • Spring Cloud Stream
    • 配置Prometheus及健康检测
    • RabbitMQ配置备忘
    • Nacos配置中心
    • Java代码杂记
      • 1 stream常见用法
      • 2 类相同属性比较
      • 3 驼峰字符串转下划线字符串
      • 4 Java输入输出流的互相转换
    • Mysql并发数与连接数
  • 源码阅读

  • 技巧备忘

  • 问题排查

  • 安装部署

  • 博客搭建

  • 技术实战
  • 编程笔记
zhixinglvren
2022-05-11
目录

Java代码杂记

# 1 stream常见用法

List<DTO> 转 List<String>

List<String> ids = dtos.stream()
                        .map(DTO::getId)
                        .distinct()
                        .collect(Collectors.toList());
1
2
3
4

List<DTO> 转 Map<String, List<DTO>>

Map<String, List<DTO>> map = dtos.stream()
                                  .collect(Collectors.groupingBy(DTO::getId));
1
2

List<DTO> 转 Map<key, value>

Map<Integer,String> map = dtos.stream()
                               .collect(Collectors.toMap(DTO::getId, DTO::getName));
1
2

List<DTO> 转 Map<key, DTO>

// 指定key-value,value是对象本身。
// Function.identity()是简洁写法,也是返回对象本身。
// key冲突的解决办法,这里选择第二个key覆盖第一个key
Map<Integer, DTO> map = dtos.stream()
                            .collect(
                                Collectors.toMap(
                                    DTO::getId, Function.identity(), (key1,key2)->key2
                                )
                             );
1
2
3
4
5
6
7
8
9

List<DTO> 转 String拼接

String str = dtos.stream()
                  .map(DTO::getId)
                  .collect(Collectors.joining(","));
1
2
3

List<DTO> 求和

// 使用mapToInteger、mapToDouble等

Integer returnCount = dtos.stream()
                           .mapToInteger(DTO::getNum)
                           .sum(); 

Double returnCount = dtos.stream()
                          .mapToDouble(DTO::getNum)
                          .sum();
1
2
3
4
5
6
7
8
9

List<DTO> 排序

// 使用mapToInteger、mapToDouble等

List<DTO> newDTOS = dtos.stream()
                        .sorted(Comparator.comparing(DTO::getSortNum, Comparator.reverseOrder()))
                        .collect(Collectors.toList()); 

1
2
3
4
5
6

# 2 类相同属性比较

public class Test {
  public static void main(String[]args)throws NoSuchFieldException {
    Map<String, String> map1 = new HashMap<>();
    Field[] fields = OmsOrderApiVO.class.getDeclaredFields();
    for (Field field : fields) {
      String fieldName = field.getName();
      ApiModelProperty annotation = field.getAnnotation(ApiModelProperty.class);
      String val = "-";
      if (annotation != null) {
        val = annotation.value();
      }
      map1.put(fieldName, val);
    }

    Map<String, String> map2 = new HashMap<>();
    Field[] fields2 = OmsWaybillApiDTO.class.getDeclaredFields();
    for (Field field2 : fields2) {
      String fieldName = field2.getName();
      ApiModelProperty annotation = field2.getAnnotation(ApiModelProperty.class);
      String val = "-";
      if (annotation != null) {
        val = annotation.value();
      }
      map2.put(fieldName, val);
    }
  }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27

# 3 驼峰字符串转下划线字符串

public class Test {
  public static void main(String[] args) throws NoSuchFieldException {
    Map<String, String> map1 = new HashMap<>();
    Field[] fields = SpmCashBillDetail.class.getDeclaredFields();
    for (Field field : fields) {
      String fieldName = field.getName();
      System.out.println(toUnderlineCase(fieldName).toUpperCase());
    }
  }

  public static String toUnderlineCase(String camelCaseStr) {
    if (camelCaseStr == null) {
      return null;
    }
    // 将驼峰字符串转换成数组
    char[] charArray = camelCaseStr.toCharArray();
    StringBuffer buffer = new StringBuffer();
    //处理字符串
    for (int i = 0, l = charArray.length; i < l; i++) {
      if (charArray[i] >= 65 && charArray[i] <= 90) {
        buffer.append("_").append(charArray[i] += 32);
      } else {
        buffer.append(charArray[i]);
      }
    }
    return buffer.toString();
  }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28

# 4 Java输入输出流的互相转换

public class Test {
  File file = new File("D:\\test\\1.txt");
  BufferedInputStream bis = null;
  ByteArrayOutputStream arrayOutputStream = null;
  OutputStream bos = null;
  try {
    // 模拟读入一个文件,作为输入流
    bis = new BufferedInputStream(new FileInputStream(file), 1024);
    // 创建ByteArray输出流,所有的输出可以输出到ByteArray中,可以替代一个文件
    arrayOutputStream = new ByteArrayOutputStream();
    //用buffered包装一下,为了提高效率使用缓冲区流
    bos = new BufferedOutputStream(arrayOutputStream);

    int len = -1;
    //读取文件输入流,写入到输出流ByteArray中,输入流转成了输出流
    byte[] buf = new byte[1024];
    while ((len = bis.read(buf)) != -1) {
      bos.write(buf, 0, len);
    }
    bos.flush();//清空缓冲区(非必要)
    //创建ByteArrayResource用ByteArray输出流的字节数组
    InputStreamSource inputStreamSource = new ByteArrayResource(arrayOutputStream.toByteArray());
    //至此把OutputStream已经转换成了InputStreamSource,输出流又转成了输入流
    // 又将输入流转成了输出流
    BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStreamSource.getInputStream());
    int bytesRead;
    byte[] buffer = new byte[4096];
    while ((bytesRead = bufferedInputStream.read(buffer)) != -1) {
      // 将文件发送到客户端
      bos.write(buffer, 0, bytesRead);
      bos.flush();
    }
  } catch(Exception e) {
    System.out.println(e);
  } finally {
    if (bos != null) {
      bos.close();//关闭BufferedOutputStream输出流
    }
    if (arrayOutputStream != null) {
      arrayOutputStream.close();//关闭ByteArray输出流
    }
    if (bis != null) {
      bis.close();
    }
  }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
上次更新: 2022/11/24, 17:59:25
Nacos配置中心
Mysql并发数与连接数

← Nacos配置中心 Mysql并发数与连接数→

最近更新
01
Linux常用指令
11-24
02
GitHub高级搜索技巧
11-24
03
散列表
11-09
更多文章>
Theme by Vdoing | Copyright © 2020-2023 知行旅人 | MIT License | 粤ICP备20036515号-1
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式