java


java8

将支付方式一样的结果进行合并

public List<Map<String, Object>> mergePayData(List<Map<String, Object>> list) {
 List<Map<String, Object>> result = list.stream().collect(
         Collectors.groupingBy(item -> item.get("paytype"))).values().stream().map(e -> {
     Map<String, Object> map = e.get(0);
     map.put("paynum", e.stream().map(s -> new BigInteger(String.valueOf(s.get("paynum")))).reduce(BigInteger.ZERO, BigInteger::add));
     map.put("payfee", e.stream().map(s -> new BigDecimal(String.valueOf(s.get("payfee")))).reduce(BigDecimal.ZERO, BigDecimal::add));
     return map;
 }).collect(Collectors.toList());
 return result;
}

将停车场一样的结果进行合并

public List<Map<String, Object>> mergePayDataByParkId(List<Map<String, Object>> list) {
 List<Map<String, Object>> result = list.stream().collect(
         Collectors.groupingBy(item -> item.get("parkname"))).values().stream().map(e -> {
     Map<String, Object> map = e.get(0);
     map.put("payfee", e.stream().map(s -> new BigDecimal(String.valueOf(s.get("payfee")))).reduce(BigDecimal.ZERO, BigDecimal::add));
     return map;
 }).collect(Collectors.toList());
 return result;
}

将支付方式一样的结果进行合并

public List<PayOverViewBO> mergePayData(List<PayOverViewBO> list) {
 List<PayOverViewBO> result = list.stream()
         .collect(Collectors.toMap(PayOverViewBO::getPaytype, a -> a, (o1, o2) -> {
             o1.setAmount(o1.getAmount().add(o2.getAmount()));
             o1.setCount(o1.getCount() + o2.getCount());
             return o1;
         })).values().stream().collect(Collectors.toList());
 return result;
}

对List<Map<String, Object>>进行排序

List<Map<String, Object>> allPayParkId = new ArrayList<>();
     allPayParkId.addAll(paySortedByParkId);
     allPayParkId.addAll(monthFeeByParkId);

     //按金额进行排序
     allPayParkId = allPayParkId.stream().sorted(Comparator.comparing(Test::comparingByPayFee).reversed()).collect(Collectors.toList());

/**
 * 排序内部类
 */
private static class Test {
    private static BigDecimal comparingByPayFee(Map<String, Object> map) {
        return (BigDecimal) map.get("payfee");
    }

    private static BigDecimal comparingByAmount(Map<String, Object> map) {
        return (BigDecimal) map.get("amount");
    }
}

对List<实体类>进行去重

  • 第一种,实体类字段值全一样

    List<CardInfo> cardInfos = cardinfoService.selectCarList(beginTime);
    cardInfos = cardInfos.stream().distinct().collect(Collectors.toList());
  • 第二种,实体类字段值不一样,按照某一个字段去重

    List<CardInfo> cardInfos = cardinfoService.selectCarList(beginTime);
    //去除车牌号重复数据
    cardInfos =cardInfos.stream().collect(Collectors.collectingAndThen(Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(CardInfo::getCarNo))), ArrayList::new));

Springboot项目使用logstash

  1. pom.xml增加依赖

    <!--logstash日志收集-->
    <dependency>
        <groupId>net.logstash.logback</groupId>
        <artifactId>logstash-logback-encoder</artifactId>
        <version>5.3</version>
    </dependency>
  2. 日志文件logback.xml增加

    <!-- 输出到logstash -->
    <appender name="logstash" class="net.logstash.logback.appender.LogstashTcpSocketAppender">
        <destination>139.155.33.58:9600</destination>
        <!-- 日志输出编码 -->
        <encoder charset="UTF-8" class="net.logstash.logback.encoder.LogstashEncoder">
            <customFields>{"appname":"parking-app-server"}</customFields>
        </encoder>
    </appender>
                         
     <root level="ERROR">
            <appender-ref ref="logstash"/>
        </root>

Springboot方法调用链路追踪和运行时长统计工具koTime

引入依赖

<dependency>
    <groupId>cn.langpy</groupId>
    <artifactId>ko-time</artifactId>
    <version>2.0.4</version>
  </dependency>

配置

application.properties文件中进行配置

  • 必填配置

ko-time.pointcut=execution(public * com.huoyo..*.*(..)) # 需要监测的切面范围,参考aop的@pointcut 或者左侧常见问题

访问

  • 启动项目访问 /koTime 路径即可
  • 如果仅仅只是想统计某个方法,在方法上加上@ComputeTime即可,控制台会输出耗时

Spring框架

  • Spring原理

https://oipunk.github.io/2020/08/24/Spring%E5%8E%9F%E7%90%86%E8%AE%B2%E8%A7%A3/

  • Spring设计模式

  • spring用到的设计模式

https://mp.weixin.qq.com/s/j1zO9OPw8h3QxHEH2n9K6Q

  • 实战设计模式,告别流水线式写代码

https://mp.weixin.qq.com/s/2jWM4O_1pS7r9P5-1U6cjw

curl发送post请求

curl -H “Content-Type: application/json” -X POST -d ‘{“jobNumber”: “010234012” }’ “http://127.0.0.1:8002/visitor/getJianDaStaff"

mybatis-plus 团队新作 mybatis-mate 轻松搞定数据权限

https://juejin.cn/post/7002876425187360781


文章作者: 陈海伟
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 陈海伟 !
  目录