zoukankan      html  css  js  c++  java
  • 新项目新知识总结01

    开启新的大门

    @Builder

    jdk1.8的新特性,不再需要额外的set方法,直接链式调用,更加方便简洁,更加优雅
    @Builder public class Card { 
      private int id;
      private String name;
      private boolean sex;
    }
    Card card = Card.builder().id(10).name("dasd").sex(true).build();

    @Builder对类做了什么?

    我们可以反编译生成的 Card.class

    public class Card {
        private int id;
        private String name;
        private boolean sex;
    
        Card(int id, String name, boolean sex) {
            this.id = id;
            this.name = name;
            this.sex = sex;
        }
    
        public static Card.CardBuilder builder() {
            return new Card.CardBuilder();
        }
    
        public static class CardBuilder {
            private int id;
            private String name;
            private boolean sex;
    
            CardBuilder() {
            }
    
            public Card.CardBuilder id(int id) {
                this.id = id;
                return this;
            }
    
            public Card.CardBuilder name(String name) {
                this.name = name;
                return this;
            }
    
            public Card.CardBuilder sex(boolean sex) {
                this.sex = sex;
                return this;
            }
    
            public Card build() {
                return new Card(this.id, this.name, this.sex);
            }
    
            public String toString() {
                return "Card.CardBuilder(id=" + this.id + ", name=" + this.name + ", sex=" + this.sex + ")";
            }
        }
    }

    那么其实很明显了,注解在编译后使得Card类中多了一个名为Card.CardBuilder的静态内部类。这个静态类拥有和Card类相同的属性,并且他额外实现了一些方法:

    1.name、sex、id等的属性方法
        其实这些方法和setAttribute十分类似,只是额外返回了实例本身,这使得它可以使用类似于链式调用的写法。
    
    2.build方法
        该方法调用Card类的全参构造方法来生成Card实例。
        Card类还是实现了builder方法,这个方法生成一个空的Card.CardBuilder实例。

    缺点

    在生成Card实例之前,实际上是先创建了一个Card.CardBuilder实例,这样很明显额外占用了内存。

    huTool--工具类常用方法

    maven依赖(jdk8对应版本5 而jdk7对应版本4)

    <dependency>
         <groupId>cn.hutool</groupId>
         <artifactId>hutool-all</artifactId>
         <version>5.4.1</version>
    </dependency>

    DateUtil: 日期时间工具类,定义了一些常用的日期时间操作方法。

    //Date、long、Calendar之间的相互转换
    //当前时间
    Date date = DateUtil.date();
    //Calendar转Date
    date = DateUtil.date(Calendar.getInstance());
    //时间戳转Date
    date = DateUtil.date(System.currentTimeMillis());
    //自动识别格式转换
    String dateStr = "2017-03-01";
    date = DateUtil.parse(dateStr);
    //自定义格式化转换
    date = DateUtil.parse(dateStr, "yyyy-MM-dd");
    //格式化输出日期
    String format = DateUtil.format(date, "yyyy-MM-dd");
    //获得年的部分
    int year = DateUtil.year(date);
    //获得月份,从0开始计数  +1获取当前月份
    int month = DateUtil.month(date);
    //获取某天的开始2020-09-01 00:00:00、结束时间 2020-09-01 23:59:59
    Date beginOfDay = DateUtil.beginOfDay(date);
    Date endOfDay = DateUtil.endOfDay(date);
    //计算偏移后的日期时间 (当前时间加两天)
    Date newDate = DateUtil.offset(date, DateField.DAY_OF_MONTH, 2);
    //计算日期时间之间的偏移量(date与newDate相差的天数)
    long betweenDay = DateUtil.between(date, newDate, DateUnit.DAY);

    StrUtil:字符串工具类,定义了一些常用的字符串操作方法。

    //判断是否为空字符串
    String str = "test";
    StrUtil.isEmpty(str);
    StrUtil.isNotEmpty(str);
    //去除字符串的前后缀
    StrUtil.removeSuffix("a.jpg", ".jpg");
    StrUtil.removePrefix("a.jpg", "a.");
    //格式化字符串    这只是个占位符:我是占位符
    String template = "这只是个占位符:{}";
    String str2 = StrUtil.format(template, "我是占位符");
    LOGGER.info("/strUtil format:{}", str2);

    NumberUtil :数字处理工具类,可用于各种类型数字的加减乘除操作及判断类型。

    double n1 = 1.234;
    double n2 = 1.234;
    double result;
    //对float、double、BigDecimal做加减乘除操作
    result = NumberUtil.add(n1, n2);
    result = NumberUtil.sub(n1, n2);
    result = NumberUtil.mul(n1, n2);
    result = NumberUtil.div(n1, n2);
    //保留两位小数
    BigDecimal roundNum = NumberUtil.round(n1, 2);
    String n3 = "1.234";
    //判断是否为数字、整数、浮点数
    NumberUtil.isNumber(n3);
    NumberUtil.isInteger(n3);
    NumberUtil.isDouble(n3);

    BeanUtil:JavaBean的工具类,可用于Map与JavaBean对象的互相转换以及对象属性的拷贝。

    PmsBrand brand = new PmsBrand();
    brand.setId(1L);
    brand.setName("华为");
    brand.setShowStatus(0);
    //Bean转Map
    Map<String, Object> map = BeanUtil.beanToMap(brand);
    LOGGER.info("beanUtil bean to map:{}", map);
    //Map转Bean
    PmsBrand mapBrand = BeanUtil.mapToBean(map, PmsBrand.class, false);
    LOGGER.info("beanUtil map to bean:{}", mapBrand);
    //Bean属性拷贝
    PmsBrand copyBrand = new PmsBrand();
    BeanUtil.copyProperties(brand, copyBrand);
    LOGGER.info("beanUtil copy properties:{}", copyBrand);

    CollUtil:集合操作的工具类,定义了一些常用的集合操作

    //数组转换为列表
    String[] array = new String[]{"a", "b", "c", "d", "e"};
    List<String> list = CollUtil.newArrayList(array);
    //join:数组转字符串时添加连接符号
    String joinStr = CollUtil.join(list, ",");
    LOGGER.info("collUtil join:{}", joinStr);
    //将以连接符号分隔的字符串再转换为列表
    List<String> splitList = StrUtil.split(joinStr, ',');
    LOGGER.info("collUtil split:{}", splitList);
    //创建新的Map、Set、List
    HashMap<Object, Object> newMap = CollUtil.newHashMap();
    HashSet<Object> newHashSet = CollUtil.newHashSet();
    ArrayList<Object> newList = CollUtil.newArrayList();
    //判断列表是否为空
    CollUtil.isEmpty(list);

    MapUtil:Map操作工具类,可用于创建Map对象及判断Map是否为空。

    //将多个键值对加入到Map中
    Map<Object, Object> map = MapUtil.of(new String[][]{
        {"key1", "value1"},
        {"key2", "value2"},
        {"key3", "value3"}
    });
    //判断Map是否为空
    MapUtil.isEmpty(map);
    MapUtil.isNotEmpty(map);

    AnnotationUtil:注解工具类,可用于获取注解与注解中指定的值。

    //获取指定类、方法、字段、构造器上的注解列表
    Annotation[] annotationList = AnnotationUtil.getAnnotations(HutoolController.class, false);
    LOGGER.info("annotationUtil annotations:{}", annotationList);
    //获取指定类型注解
    Api api = AnnotationUtil.getAnnotation(HutoolController.class, Api.class);
    LOGGER.info("annotationUtil api value:{}", api.description());
    //获取指定类型注解的值
    Object annotationValue = AnnotationUtil.getAnnotationValue(HutoolController.class, RequestMapping.class);

    SecureUtil:加密解密工具类,可用于MD5加密。

    //MD5加密
    String str = "123456";
    String md5Str = SecureUtil.md5(str);
    LOGGER.info("secureUtil md5:{}", md5Str);

    CaptchaUtil:验证码工具类,可用于生成图形验证码。

    //生成验证码图片
    LineCaptcha lineCaptcha = CaptchaUtil.createLineCaptcha(200, 100);
    try {
        request.getSession().setAttribute("CAPTCHA_KEY", lineCaptcha.getCode());
        response.setContentType("image/png");//告诉浏览器输出内容为图片
        response.setHeader("Pragma", "No-cache");//禁止浏览器缓存
        response.setHeader("Cache-Control", "no-cache");
        response.setDateHeader("Expire", 0);
        lineCaptcha.write(response.getOutputStream());
    } catch (IOException e) {
        e.printStackTrace();
    }

    Hutool中的工具类很多,可以参考:https://www.hutool.cn/

  • 相关阅读:
    【转】机器学习算法一览,应用建议与解决思路
    机器学习笔记04(Logistic Regression)
    机器学习笔记03(Classification: Probabilistic Generative Model)
    机器学习笔记00(课程结构)
    .Net PE
    wpf 画五角星函数
    .Net Core CLR FileFormat Call Method( Include MetaData, Stream, #~)
    天子守国门,君王死社稷
    CoreCLR Host源码分析(C++)
    Core CLR Host 源码简单分析
  • 原文地址:https://www.cnblogs.com/qcq0703/p/14985112.html
Copyright © 2011-2022 走看看