Java JDK 8 中的新特性

Java Platform, Standard Edition 8 是一个拥有丰富特性的主要版本。本文档总结了 Java SE 8、JDK 8 以及 Oracle 的 Java SE 8 实现中的特性和增强。单击组件名称可获取该组件增强功能更详细的描述。

  • Java 编程语言
    • Lambda 表达式是一个新的语言特性,已经在此版本中引入。该特性让您可以将功能视为方法参数,或者将代码视为数据。使用 Lambda 表达式,您可以更简洁地表示单方法接口(称为功能接口)的实例。
    • 方法引用为已经具有名称的方法提供了易于理解的 lambda 表达式。
    • 默认方法允许将新功能添加到库的接口中,并确保与为这些接口的旧版本编写的代码的二进制兼容性。
    • 重复批注支持对同一个声明或类型的使用多次应用相同的批注类型。
    • 类型批注支持在使用类型的任何地方应用批注,而不仅限于声明。与可插拔类型系统结合使用时,此特性可改进代码的类型检查。
    • 改进类型推断。
    • 方法参数反射。
  • 集合
    • 新的 java.util.stream 包中的类提供了一个 Stream API,支持对元素流进行函数式操作。Stream API 集成在 Collections API 中,可以对集合进行批量操作,例如顺序或并行的 map-reduce 转换。
    • 针对存在键冲突的 HashMap 的性能改进
  • 紧凑 profile包含 Java SE 平台的预定义子集,并且支持不需要在小型设备上部署和运行整个平台的应用。
  • 安全性
    • 默认启用客户端 TLS 1.2
    • AccessController.doPrivileged 的新变体支持代码断言其权限的子集,而不会阻止完全遍历堆栈来检查其他权限
    • 更强大的基于密码的加密算法
    • JSSE 服务器端支持 SSL/TLS 服务器名称指示 (SNI) 扩展
    • 支持 AEAD 算法:SunJCE 提供程序得到了增强,支持 AES/GCM/NoPadding 密码实现以及 GCM 算法参数。而且 SunJSSE 提供程序也得到了增强,支持基于 AEAD 模式的密码套件。请参阅 Oracle 提供程序文档,JEP 115。
    • 密钥库增强,包括新的域密钥库类型 java.security.DomainLoadStoreParameter 和为 keytool 实用程序新增的命令选项 -importpassword
    • SHA-224 消息摘要
    • 增强了对 NSA Suite B 加密的支持
    • 更好地支持高熵随机数生成
    • 新增了 java.security.cert.PKIXRevocationChecker 类,用于配置 X.509 证书的撤销检查
    • 适用于 Windows 的 64 位 PKCS11
    • Kerberos 5 重放缓存中新增了 rcache 类型
    • 支持 Kerberos 5 协议转换和受限委派
    • 默认禁用 Kerberos 5 弱加密类型
    • 适用于 GSS-API/Kerberos 5 机制的未绑定 SASL
    • 针对多个主机名称的 SASL 服务
    • JNI 桥接至 Mac OS X 上的原生 JGSS
    • SunJSSE 提供程序中支持更强大的临时 DH 密钥
    • JSSE 中支持服务器端加密套件首选项自定义
  • JavaFX
    • 本版本中实施了新的 Modena 主题。
    • 新的 SwingNode 类允许开发人员将 Swing 内容嵌入到 JavaFX 应用中。请参阅 SwingNode javadoc 和将 Swing 内容嵌入 JavaFX 应用中。
    • 新的 UI 控件包括 DatePickerTreeTableView 控件。
    • javafx.print 程序包为 JavaFX Printing API 提供了公共类。
    • 3D 图形特性现在包括 3D 形状、摄像头、灯光、子场景、材料、挑选和抗锯齿。JavaFX 3D 图形库中新增了 Shape3DBoxCylinderMeshViewSphere 子类)、SubSceneMaterialPickResultLightBaseAmbientLightPointLight子类)和 SceneAntialiasing API 类。此版本中的 Camera API 类也已更新。请参阅 javafx.scene.shape.Shape3Djavafx.scene.SubScenejavafx.scene.paint.Materialjavafx.scene.input.PickResultjavafx.scene.SceneAntialiasing 类的相关 javadoc 以及 JavaFX 3D 图形入门文档。
    • WebView 类包含新特性和改进。有关其他 HTML5 特性(包括 Web 套接字、Web 辅助进程和 Web 字体)
    • 增强了文本支持,包括双向文本、复杂文本脚本(如泰语和印地语控件)以及文本节点中的多行多样式文本。
    • 此版本添加了对 Hi-DPI 显示的支持。
    • CSS Styleable* 类已成为公共 API。有关更多信息,请参阅 Javafx.css javadoc。
    • 新的 ScheduledService 类允许自动重新启动服务。
    • JavaFX 现在可用于 ARM 平台。适用于 ARM 的 JDK 包含 JavaFX 的基础组件、图形组件和控制组件。
  • 工具
    • javadoc 工具支持新的 DocTree API,让您可以将 Javadoc 注释作为抽象语法树来进行遍历。
    • javadoc 工具支持新的 Javadoc Access API,让您可以直接从 Java 应用中调用 Javadoc 工具,而无需执行新的进程。
    • javadoc 工具现在支持检查 javadoc 注释的内容,从而避免在运行 javadoc 时生成的文件中产生各种问题,例如无效的 HTML 或可访问性问题。此特性默认为启用状态,可以通过新的 -Xdoclint 选项加以控制。有关更多详细信息,请参阅运行“javadoc -X”时的输出。javac 工具也支持此特性,但默认情况下并未启用它。
    • javac 命令的 -parameters 选项可用于存储正式参数名称,并启用反射 API 来检索正式参数名称。
    • javac 命令现已正确实施了 Java 语言规范 (JLS) 第 15.21 节中的相等运算符的类型规则。
    • javac 工具现在支持检查 javadoc 注释的内容,从而避免在运行 javadoc 时生成的文件中产生各种问题,例如无效的 HTML 或可访问性问题。可通过新的 Xdoclint 选项来启用此特性。有关更多详细信息,请参阅运行“javac-X”时的输出。此特性也可以在 javadoc 工具中使用,并且默认启用。
    • javac 工具现在支持根据需要生成原生标头。这样便无需在构建管道中单独运行 javah 工具。可以使用新的 -h 选项在 javac 中启用此特性,该选项用于指定写入头文件的目录。将为任何具有原生方法或者使用 java.lang.annotation.Native 类型的新批注的类进行批注的常量字段生成头文件。
    • 可通过 jjs 命令来调用 Nashorn 引擎。
    • java 命令用于启动 JavaFX 应用。
    • 重新编写了 java 手册页。
    • 可通过 jdeps 命令行工具来分析类文件。
    • Java Management Extensions (JMX) 支持远程访问诊断命令。
    • jarsigner 工具提供了一个选项用于请求获取时间戳机构 (TSA) 的签名时间戳。
    • Javac 工具
    • Javadoc 工具
  • 国际化
    • Unicode 增强,包括对 Unicode 6.2.0 的支持
    • 采用 Unicode CLDR 数据和 java.locale.providers 系统属性
    • 新增日历和区域设置 API
    • 支持将自定义资源包作为扩展进行安装
  • 部署
    • 现在可以使用 URLPermission 允许沙盒小程序和 Java Web Start 应用连接回启动它们的服务器。不再授予 SocketPermission
    • 在所有安全级别,主 JAR 文件的 JAR 文件清单中都需要 Permissions 属性。
  • Date-Time 程序包 — 一组新程序包,提供全面的日期-时间模型。
  • 脚本编写
    • Rhino Javascript 引擎已被替换为 Nashorn JavaScript 引擎
  • Pack200
    • Pack200 支持 JSR 292 引入的常量池条目和新字节码
    • JDK8 支持 JSR-292、JSR-308 和 JSR-335 指定的类文件更改
  • IO 和 NIO
    • 全新的基于 Solaris 事件端口机制的面向 Solaris 的 SelectorProvider 实现。要使用它,请将系统属性 java.nio.channels.spi.Selector 的值设置为 sun.nio.ch.EventPortSelectorProvider
    • 减小 <JDK_HOME>/jre/lib/charsets.jar 文件的大小
    • 提高了 java.lang.String(byte[], *) 构造函数和 java.lang.String.getBytes() 方法的性能。
  • java.lang 和 java.util 程序包
    • 并行数组排序
    • 标准编码和解码 Base64
    • 无符号算术支持
  • JDBC
    • 删除了 JDBC-ODBC Bridge。
    • JDBC 4.2 引入了新特性。
  • Java DB
    • JDK 8 包含 Java DB 10.10。
  • 网络
    • 已添加 java.net.URLPermission 类。
    • java.net.HttpURLConnection 类中,如果安装了安全管理器,那么请求打开连接的调用需要权限。
  • 并发性
    • java.util.concurrent 程序包中新增了一些类和接口。
    • java.util.concurrent.ConcurrentHashMap 类中新增了一些方法,支持基于新增流工具和 lambda 表达式的聚合操作。
    • java.util.concurrent.atomic 程序包中新增了一些类来支持可扩展、可更新的变量。
    • java.util.concurrent.ForkJoinPool 类中新增了一些方法来支持通用池。
    • 新增的 java.util.concurrent.locks.StampedLock 类提供了一个基于能力的锁,可通过三种模式来控制读/写访问。
  • Java XML – JAXP
  • HotSpot
    • 新增的硬件内部函数以便使用高级加密标准 (AES)。UseAESUseAESIntrinsics 标志用于为 Intel 硬件启用基于硬件的 AES 内部函数。硬件必须是 2010 年或更新的 Westmere 硬件。例如,要启用硬件 AES,请使用以下标志: -XX:+UseAES -XX:+UseAESIntrinsics 要禁用硬件 AES,请使用以下标志: -XX:-UseAES -XX:-UseAESIntrinsics
    • 删除了 PermGen。
    • 方法调用的字节码指令支持 Java 编程语言中的默认方法。
  • Java Mission Control 5.3 版本说明
    • JDK 8 包含 Java Mission Control 5.3。

java日期时间工具类

工具类继承了OutPut类,这个类只是提供了一个out()的静态方法,用于测试。

package system.utils;

import system.lib.OutPut;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.format.TextStyle;
import java.time.temporal.*;
import java.util.Date;
import java.util.Locale;

/**
 * Created by alan on 2019/4/19.
 *
 * ********************************
 * getTime():1555776871585
 * getTimeInt():1555776871
 * getLocalDate():2019-04-21
 * parseLocalDate("2019-04-20",1):2019-04-20
 * parseLocalDate(2019,04,20):2019-04-20
 * plus(getLocalDate(),2,ChronoUnit.DAYS):2019-04-23
 * --------------------------------
 * getLocalTime():00:14:31.585
 * parseLocalTime("23:56:00"):23:56
 * parseLocalTime(23,56,12):23:56:12
 * plus(getLocalTime(),-3,ChronoUnit.HOURS):21:14:31.585
 * --------------------------------
 * getLocalDateTime():2019-04-21T00:14:31.585
 * parseLocalDateTime("2019-04-20 21:05:56",1):2019-04-20T21:05:56
 * parseLocalDateTime(2019,4,20,21,05,56):2019-04-20T21:05:56
 * plus(getLocalDateTime(),-1,ChronoUnit.MONTHS):2019-03-21T00:14:31.585
 * --------------------------------
 * format(int t):2019-04-21T00:14:31
 * format(long t):2019-04-21T00:14:31.585
 * format(int t,1):2019-4-21 0:14:31
 * format(long t,1):2019-4-21 0:14:31
 * format(LocalDate d,1):2019-4-21
 * format(LocalTime t):0:14:31
 * format(LocalDateTime t,1):2019-4-21 0:14:31
 * format(ZonedDateTime.now(),1):2019-4-21 0:14:31
 * format(LocalDate d,DateTimeFormatter.ISO_DATE):2019-04-21
 * format(LocalTime t,DateTimeFormatter.ISO_TIME):00:14:31.585
 * format(LocalDateTime t,DateTimeFormatter.ISO_DATE_TIME):2019-04-21T00:14:31.585
 * format(ZonedDateTime t,DateTimeFormatter.ISO_DATE_TIME):2019-04-21T00:14:31.585+08:00[Asia/Shanghai]
 * format(LocalDateTime t,DATE_TIME_FULL):2019-04-21 00:14:31
 * format(int i,DATE_TIME_FULL):2019-04-21 00:14:31
 * format(long l,DATE_TIME_FULL):2019-04-21 00:14:31
 * --------------------------------
 * getYear():2019
 * getMonthI():4
 * getMonth():APRIL
 * getMonthS():四月
 * getDayOfMonth():21
 * getDayOfYear():111
 * getDayOfMonthMax():30
 * getWeek():星期日
 * getWeekI():7
 * getDate():Sun Apr 21 00:14:31 CST 2019
 * getTimestamp():2019-04-21 00:14:31.587
 * getTimestampL():2019-04-21 00:14:31.587
 * getHour():0
 * getMinute():14
 * getSecond():31
 */
public class DateTimeUtils extends OutPut {

    public static final String YEAR = "yyyy";
    public static final String MONTH = "MM";
    public static final String DAY = "dd";
    public static final String DATE_FULL = "yyyy-MM-dd";
    public static final String DATE_FULL2 = "yyyyMMdd";
    public static final String DATE_TIME_FULL = "yyyy-MM-dd HH:mm:ss";
    public static final String DATE_TIME_FULL2 = "yyyyMMddHHmmss";
    public static final String WEEK = "EEEE";

    private static final int ZONE_HOUR = 8;

    public DateTimeUtils() {

    }

    /**
     * 取时间戳
     *
     * @return long
     */
    public static long getTime() {
        return System.currentTimeMillis();
    }

    /**
     * 取时间戳
     *
     * @return int
     */
    public static int getTimeInt() {
        return (int) (getTime() / 1000);
    }

    /**
     * 取本地日期对象
     *
     * @return LocalDate
     */
    public static LocalDate getLocalDate() {
        return LocalDate.now();
    }

    /**
     * 字符串日期转本地日期对象
     *
     * @param str    2012/01/01|2012-01-01
     * @param format 1|2
     * @return LocalDate
     */
    public static LocalDate parseLocalDate(String str, int format) {
        /**
         * format:1=-,2=/
         */
        String regx = "/";
        if (1 == format) {
            regx = "-";
        }
        String[] s = str.split(regx);
        return parseLocalDate(Integer.valueOf(s[0]), Integer.valueOf(s[1]), Integer.valueOf(s[2]));
    }

    /**
     * 整数日期转本地日期对象
     *
     * @param y 年
     * @param m 月
     * @param d 日
     * @return LocalDate
     */
    public static LocalDate parseLocalDate(int y, int m, int d) {
        return LocalDate.of(y, m, d);
    }

    /**
     * 本地日期增减
     *
     * @param date 本地日期
     * @param n    +正数则增加;-负数则减去
     * @param unit ChronoUnit.DAYS|ChronoUnit.YEARS|ChronoUnit.MONTHS
     * @return LocalDate
     */
    public static LocalDate plus(LocalDate date, int n, ChronoUnit unit) {
        return date.plus(n, unit);
    }

    /**
     * 取本地时间对象
     *
     * @return LocalTime
     */
    public static LocalTime getLocalTime() {
        return LocalTime.now();
    }

    /**
     * 本地时间增减
     *
     * @param time 本地时间
     * @param n    +正数则增加;-负数则减去
     * @param unit ChronoUnit.HOURS|ChronoUnit.MINUTES|ChronoUnit.SECONDS
     * @return LocalTime
     */
    public static LocalTime plus(LocalTime time, int n, ChronoUnit unit) {
        return time.plus(n, unit);
    }

    /**
     * 字符串时间转本地时间对象
     *
     * @param time 00:00:00
     * @return LocalTime
     */
    public static LocalTime parseLocalTime(String time) {
        String[] s = time.split(":");
        return parseLocalTime(Integer.valueOf(s[0]), Integer.valueOf(s[1]), Integer.valueOf(s[2]));
    }

    /**
     * 整数时间转本地时间对象
     *
     * @param h 小时
     * @param m 分钟
     * @param s 秒
     * @return LocalTime
     */
    public static LocalTime parseLocalTime(int h, int m, int s) {
        return LocalTime.of(h, m, s);
    }

    /**
     * 获取本地日期时间对象
     *
     * @return
     */
    public static LocalDateTime getLocalDateTime() {
        return LocalDateTime.now();
    }

    /**
     * 本地日期时间增减
     *
     * @param time 本地日期时间
     * @param n    +正数则增加;-负数则减去
     * @param unit ChronoUnit.DAYS|ChronoUnit.YEARS|ChronoUnit.MONTHS|ChronoUnit.HOURS|ChronoUnit.MINUTES|ChronoUnit.SECONDS
     * @return LocalDateTime
     */
    public static LocalDateTime plus(LocalDateTime time, int n, ChronoUnit unit) {
        return time.plus(n, unit);
    }

    /**
     * 本地日期增减
     *
     * @param time 本地时间
     * @param n    +正数则增加;-负数则减去
     * @param unit ChronoUnit.DAYS|ChronoUnit.YEARS|ChronoUnit.MONTHS
     * @return LocalDate
     */
    public static LocalDate plusDate(LocalDate time, int n, ChronoUnit unit) {
        return time.plus(n, unit);
    }

    /**
     * 本地时间增减
     *
     * @param time 本地时间
     * @param n    +正数则增加;-负数则减去
     * @param unit ChronoUnit.HOURS|ChronoUnit.MINUTES|ChronoUnit.SECONDS
     * @return LocalTime
     */
    public static LocalTime plusTime(LocalTime time, int n, ChronoUnit unit) {
        return time.plus(n, unit);
    }

    /**
     * 字符串日期时间转本地日期时间对象
     *
     * @param datetime string like it:2019-01-01 00:00:00 other is 2019/01/01 00:00:00
     * @param format   if == 1 then 2019-01-01 00:00:00 other is 2019/01/01 00:00:00
     * @return null|LocalDateTime
     */
    public static LocalDateTime parseLocalDateTime(String datetime, int format) {
        String[] x = datetime.split(" ");
        if (x.length != 2) {
            return null;
        }
        String regx = "/";
        if (1 == format) {
            regx = "-";
        }
        String[] d = x[0].split(regx);
        String[] s = x[1].split(":");
        return parseLocalDateTime(Integer.valueOf(d[0]), Integer.valueOf(d[1]), Integer.valueOf(d[2]),
                Integer.valueOf(s[0]), Integer.valueOf(s[1]), Integer.valueOf(s[2]));
    }

    /**
     * 整数日期时间转本地日期时间对象
     *
     * @param y 年
     * @param m 月
     * @param d 日
     * @param h 小时
     * @param i 分钟
     * @param s 秒
     * @return LocalDateTime
     */
    public static LocalDateTime parseLocalDateTime(int y, int m, int d, int h, int i, int s) {
        return LocalDateTime.of(y, m, d, h, i, s);
    }

    /**
     * 格式化当前时间
     * @return
     */
    public static String format() {
        return format(getTime(),1);
    }

    /**
     * 秒时间戳转本地日期时间对象
     *
     * @param datetime 秒时间戳
     * @return LocalDateTime
     */
    public static LocalDateTime format(int datetime) {
        return format((long) datetime * 1000L);
    }

    /**
     * 微秒时间戳转本地日期时间对象
     *
     * @param datetime 微秒时间戳
     * @return LocalDateTime
     */
    public static LocalDateTime format(long datetime) {
        LocalDateTime localDateTime = Instant.ofEpochMilli(datetime).atZone(ZoneOffset.systemDefault()).toLocalDateTime();
        return localDateTime;
    }

    /**
     * 秒时间戳格式化字符串文本
     *
     * @param datetime 秒时间戳
     * @param format   格式化字符串
     * @return String
     */
    public static String format(int datetime, int format) {
        return format((long) datetime * 1000L, format);
    }

    /**
     * 微秒时间戳格式化字符串文本
     *
     * @param datetime 微秒时间戳
     * @param format   格式化字符串
     * @return String
     */
    public static String format(long datetime, int format) {
        return format(format(datetime), format);
    }

    /**
     * 本地日期对象格式化文本
     *
     * @param date   本地日期对象
     * @param format 1~2
     * @return String
     */
    public static String format(LocalDate date, int format) {
        String regx = "/";
        if (1 == format) {
            regx = "-";
        }
        StringBuffer st = new StringBuffer();
        st.append(date.getYear()).append(regx)
                .append(date.getMonthValue()).append(regx)
                .append(date.getDayOfMonth());
        return st.toString();
    }


    /**
     * 本地时间对象格式化文本
     *
     * @param time 本地时间对象
     * @return String
     */
    public static String format(LocalTime time) {
        StringBuffer st = new StringBuffer();
        st.append(time.getHour()).append(":")
                .append(time.getMinute()).append(":")
                .append(time.getSecond());
        return st.toString();
    }

    /**
     * 本地日期时间对象格式化文本
     *
     * @param dateTime 本地日期时间对象
     * @param format   1~2
     * @return String
     */
    public static String format(LocalDateTime dateTime, int format) {
        String regx = "/";
        if (1 == format) {
            regx = "-";
        }
        StringBuffer st = new StringBuffer();
        st.append(dateTime.getYear()).append(regx)
                .append(dateTime.getMonthValue()).append(regx)
                .append(dateTime.getDayOfMonth()).append(" ")
                .append(dateTime.getHour()).append(":")
                .append(dateTime.getMinute()).append(":")
                .append(dateTime.getSecond());
        return st.toString();
    }

    /**
     * 时区时间格式化文本
     *
     * @param dateTime 时区时间对象
     * @param format 1~2
     * @return String
     */
    public static String format(ZonedDateTime dateTime, int format) {
        String regx = "/";
        if (1 == format) {
            regx = "-";
        }
        StringBuffer st = new StringBuffer();
        st.append(dateTime.getYear()).append(regx)
                .append(dateTime.getMonthValue()).append(regx)
                .append(dateTime.getDayOfMonth()).append(" ")
                .append(dateTime.getHour()).append(":")
                .append(dateTime.getMinute()).append(":")
                .append(dateTime.getSecond());
        return st.toString();
    }

    /**
     * 格式化本地日期
     * @param date 本地日期对象
     * @param format 日期时间格式化对象:DateTimeFormatter.ISO_DATE
     * @return String
     */
    public static String format(LocalDate date, DateTimeFormatter format) {
        return format.format(date);
    }

    /**
     * 格式化本地时间
     * @param time 本地时间对象
     * @param format 日期时间格式化对象:DateTimeFormatter.ISO_TIME
     * @return String
     */
    public static String format(LocalTime time, DateTimeFormatter format) {
        return format.format(time);
    }

    /**
     * 格式化本地日期时间
     * @param dateTime 本地日期时间对象
     * @param format 日期时间格式化对象:DateTimeFormatter.ISO_LOCAL_DATE_TIME|ISO_DATE_TIME
     * @return String
     */
    public static String format(LocalDateTime dateTime, DateTimeFormatter format) {
        return format.format(dateTime);
    }

    /**
     * 格式化时区日期时间
     * @param dateTime 时区日期时间对象
     * @param format 日期格式化对象:DateTimeFormatter.ISO_ZONED_DATE_TIME|ISO_DATE_TIME
     * @return String
     */
    public static String format(ZonedDateTime dateTime, DateTimeFormatter format) {
        return format.format(dateTime);
    }

    /**
     * 取Date对象
     * @return Date
     */
    public static Date getDate() {
        return Date.from(Instant.now());
    }

    /**
     * 取Timestamp对象
     * @return Timestamp
     */
    public static Timestamp getTimestamp() {
        return Timestamp.from(Instant.now());
    }

    /**
     * 取Timestamp对象
     * @return Timestamp
     */
    public static Timestamp getTimestampL() {
        return Timestamp.valueOf(getLocalDateTime());
    }

    /**
     * 获取星期
     *
     * @return like 星期六|?
     */
    public static String getWeek() {
        return getLocalDate().getDayOfWeek().getDisplayName(TextStyle.FULL, Locale.getDefault());
    }

    /**
     * 获取星期
     *
     * @return like 1~7
     */
    public static int getWeekI() {
        return getLocalDate().getDayOfWeek().getValue();
    }

    /**
     * 获取当天日(按月)
     * @return int
     */
    public static int getDayOfMonth() {
        return getLocalDate().getDayOfMonth();
    }

    /**
     * 获取当天日(按年)
     * @return int
     */
    public static int getDayOfYear() {
        return getLocalDate().getDayOfYear();
    }

    /**
     * 获取当月最后一天
     * @return int
     */
    public static int getDayOfMonthMax() {
        return getMonth().maxLength();
    }

    /**
     * 获取Month对象
     * @return Month
     */
    public static Month getMonth() {
        return getLocalDate().getMonth();
    }

    /**
     * 获取月份(当地格式)
     * @return String|六月
     */
    public static String getMonthS() {
        return getMonth().getDisplayName(TextStyle.FULL, Locale.getDefault());
    }

    /**
     * 获取月份
     * @return int
     */
    public static int getMonthI() {
        return getMonth().getValue();
    }

    /**
     * 取年份
     * @return int|2019
     */
    public static int getYear() {
        return getLocalDate().getYear();
    }

    /**
     * 取小时
     * @return int|0~23
     */
    public static int getHour() {
        return getLocalTime().getHour();
    }

    /**
     * 取分钟
     * @return int|0~59
     */
    public static int getMinute() {
        return getLocalTime().getMinute();
    }

    /**
     * 取秒
     * @return int|0~59
     */
    public static int getSecond() {
        return getLocalTime().getSecond();
    }

    /**
     * 格式化本地日期时间对象
     * @param dateTime 本地日期时间对象
     * @param pattern  public static final String YEAR = "yyyy";
     *                 public static final String MONTH = "MM";
     *                 public static final String DAY = "dd";
     *                 public static final String FULL_DATE = "yyyy-MM-dd";
     *                 public static final String FULL_DATE_Q = "yyyyMMdd";
     *                 public static final String FULL_DATE_TIME = "yyyy-MM-dd HH:mm:ss";
     *                 public static final String FULL_DATE_TIME_Q = "yyyyMMddHHmmss";
     *                 public static final String WEEK = "EEEE";
     * @return String
     */
    public static String format(LocalDateTime dateTime, String pattern) {
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        return sdf.format(Date.from(dateTime.atZone(ZoneOffset.systemDefault()).toInstant()));
    }

    /**
     * 格式化日期时间(整数)
     * @param dateTime 时间戳
     * @param pattern 格式化文本
     * @return String
     */
    public static String format(int dateTime, String pattern) {
        return format((long) dateTime * 1000L, pattern);
    }

    /**
     * 格式化日期时间(长整数)
     * @param dateTime 微秒时间戳
     * @param pattern 格式化文本
     * @return String
     */
    public static String format(long dateTime, String pattern) {
        return format(format(dateTime), pattern);
    }

    public static void main(String[] args) {
        out("********************************");
        out("getTime():"+getTime());
        out("getTimeInt():"+getTimeInt());
        out("getLocalDate():"+getLocalDate());
        out("parseLocalDate(\"2019-04-20\",1):"+parseLocalDate("2019-04-20",1));
        out("parseLocalDate(2019,04,20):"+parseLocalDate(2019,04,20));
        out("plus(getLocalDate(),2,ChronoUnit.DAYS):"+plus(getLocalDate(),2,ChronoUnit.DAYS));
        out("--------------------------------");
        out("getLocalTime():"+getLocalTime());
        out("parseLocalTime(\"23:56:00\"):"+parseLocalTime("23:56:00"));
        out("parseLocalTime(23,56,12):"+parseLocalTime(23,56,12));
        out("plus(getLocalTime(),-3,ChronoUnit.HOURS):"+plus(getLocalTime(),-3,ChronoUnit.HOURS));
        out("--------------------------------");
        out("getLocalDateTime():"+getLocalDateTime());
        out("parseLocalDateTime(\"2019-04-20 21:05:56\",1):"+parseLocalDateTime("2019-04-20 21:05:56",1));
        out("parseLocalDateTime(2019,4,20,21,05,56):"+parseLocalDateTime(2019,4,20,21,05,56));
        out("plus(getLocalDateTime(),-1,ChronoUnit.MONTHS):"+plus(getLocalDateTime(),-1,ChronoUnit.MONTHS));
        out("--------------------------------");
        out("format(int t):"+format(getTimeInt()));
        out("format(long t):"+format(getTime()));
        out("format(int t,1):"+format(getTimeInt(),1));
        out("format(long t,1):"+format(getTime(),1));
        out("format(LocalDate d,1):"+format(getLocalDate(),1));
        out("format(LocalTime t):"+format(getLocalTime()));
        out("format(LocalDateTime t,1):"+format(getLocalDateTime(),1));
        out("format(ZonedDateTime.now(),1):"+format(ZonedDateTime.now(),1));
        out("format(LocalDate d,DateTimeFormatter.ISO_DATE):"+format(getLocalDate(),DateTimeFormatter.ISO_DATE));
        out("format(LocalTime t,DateTimeFormatter.ISO_TIME):"+format(getLocalTime(),DateTimeFormatter.ISO_TIME));
        out("format(LocalDateTime t,DateTimeFormatter.ISO_DATE_TIME):"+format(getLocalDateTime(),DateTimeFormatter.ISO_DATE_TIME));
        out("format(ZonedDateTime t,DateTimeFormatter.ISO_DATE_TIME):"+format(ZonedDateTime.now(),DateTimeFormatter.ISO_DATE_TIME));
        out("format(LocalDateTime t,DATE_TIME_FULL):"+format(getLocalDateTime(),DATE_TIME_FULL));
        out("format(int i,DATE_TIME_FULL):"+format(getTimeInt(),DATE_TIME_FULL));
        out("format(long l,DATE_TIME_FULL):"+format(getTime(),DATE_TIME_FULL));
        out("--------------------------------");
        out("getYear():"+getYear());
        out("getMonthI():"+getMonthI());
        out("getMonth():"+getMonth());
        out("getMonthS():"+getMonthS());
        out("getDayOfMonth():"+getDayOfMonth());
        out("getDayOfYear():"+getDayOfYear());
        out("getDayOfMonthMax():"+getDayOfMonthMax());
        out("getWeek():"+getWeek());
        out("getWeekI():"+getWeekI());
        out("getDate():"+getDate());
        out("getTimestamp():"+getTimestamp());
        out("getTimestampL():"+getTimestampL());
        out("getHour():"+getHour());
        out("getMinute():"+getMinute());
        out("getSecond():"+getSecond());


    }
}

Java集合类(Set、List、Map)及排序

Java集合类如下图:

  • set是一个数据集合
  • List属于数组列表
  • Map也属于一个数据集合

866452-20170226172935866-1277792347

网上找了个图,方便理解。

  • List , Set, Map都是接口,前两个继承至Collection接口,Map为独立接口
  • Set下有HashSet,LinkedHashSet,TreeSet
  • List下有ArrayList,Vector,LinkedList
  • Map下有Hashtable,LinkedHashMap,HashMap,TreeMap
  • Collection接口下还有个Queue接口,有PriorityQueue类

1.Set对象

Set是最简单的一种集合。集合中的对象不按特定的方式排序,并且没有重复对象。

Modifier and Type Method and Description
boolean add(E e)

Adds the specified element to this set if it is not already present (optional operation).
boolean addAll(Collection<? extends E> c)

Adds all of the elements in the specified collection to this set if they’re not already present (optional operation).
void clear()

Removes all of the elements from this set (optional operation).
boolean contains(Object o)

Returns true if this set contains the specified element.
boolean containsAll(Collection<?> c)

Returns true if this set contains all of the elements of the specified collection.
boolean equals(Object o)

Compares the specified object with this set for equality.
int hashCode()

Returns the hash code value for this set.
boolean isEmpty()

Returns true if this set contains no elements.
Iterator<E> iterator()

Returns an iterator over the elements in this set.
boolean remove(Object o)

Removes the specified element from this set if it is present (optional operation).
boolean removeAll(Collection<?> c)

Removes from this set all of its elements that are contained in the specified collection (optional operation).
boolean retainAll(Collection<?> c)

Retains only the elements in this set that are contained in the specified collection (optional operation).
int size()

Returns the number of elements in this set (its cardinality).
default Spliterator<E> spliterator()

Creates a Spliterator over the elements in this set.
Object[] toArray()

Returns an array containing all of the elements in this set.
<T> T[] toArray(T[] a)

Returns an array containing all of the elements in this set; the runtime type of the returned array is that of the specified array.

 

HashSet

HashSet底层用的是哈希表,它把对象根据其哈希值存放到对应的区域里。由于这种特性,两个在不同区域的对象会被认为不相同的。
所以如果对象要存放到Hash集合里面,则需要重写对象的hashCode方法,让相等的对象的hashCode的值也相等。

TreeSet

TreeSet采用的数据结构是红黑树,我们可以让它按指定规则对其中的元素进行排序。它又是如何判断两个元素是否相同呢?除了用equals方法检查两个元素是否相同外,还要检查compareTo方法是否返回为0。

所以如果对象要存放到Tree集合里,需要在重写compareTo方法,把相同的对象的比较值定为0,防止相同的元素被重复添加进集合中。

LinkedHashSet

底层数据结构是链表和哈希表。(FIFO插入有序,唯一)

  • 由链表保证元素有序
  • 由哈希表保证元素唯一

set的测试:

Set<String> data = new TreeSet<>();
data.add("re");
data.add("idn");
data.add("mob");
data.add("ref");
data.add("01");
out(data);

Set<Integer> integers = new TreeSet<>();
integers.add(12);
integers.add(1);
integers.add(15);
integers.add(5);
integers.add(65);

out(integers);

 

打印结果如下:

[01, idn, mob, re, ref]
[1, 5, 12, 15, 65]

2.List对象

List 接口继承了 Collection 接口以定义一个允许重复项的有序集合。该接口不但能够对列表的一部分进行处理,还添加了面向位置的操作。

有序的 collection(也称为序列)。此接口的用户可以对列表中每个元素的插入位置进行精确地控制。用户可以根据元素的整数索引(在列表中的位置)访问元素,并搜索列表中的元素。

Modifier and Type Method and Description
boolean add(E e)

Appends the specified element to the end of this list (optional operation).
void add(int index, E element)

Inserts the specified element at the specified position in this list (optional operation).
boolean addAll(Collection<? extends E> c)

Appends all of the elements in the specified collection to the end of this list, in the order that they are returned by the specified collection’s iterator (optional operation).
boolean addAll(int index, Collection<? extends E> c)

Inserts all of the elements in the specified collection into this list at the specified position (optional operation).
void clear()

Removes all of the elements from this list (optional operation).
boolean contains(Object o)

Returns true if this list contains the specified element.
boolean containsAll(Collection<?> c)

Returns true if this list contains all of the elements of the specified collection.
boolean equals(Object o)

Compares the specified object with this list for equality.
E get(int index)

Returns the element at the specified position in this list.
int hashCode()

Returns the hash code value for this list.
int indexOf(Object o)

Returns the index of the first occurrence of the specified element in this list, or -1 if this list does not contain the element.
boolean isEmpty()

Returns true if this list contains no elements.
Iterator<E> iterator()

Returns an iterator over the elements in this list in proper sequence.
int lastIndexOf(Object o)

Returns the index of the last occurrence of the specified element in this list, or -1 if this list does not contain the element.
ListIterator<E> listIterator()

Returns a list iterator over the elements in this list (in proper sequence).
ListIterator<E> listIterator(int index)

Returns a list iterator over the elements in this list (in proper sequence), starting at the specified position in the list.
E remove(int index)

Removes the element at the specified position in this list (optional operation).
boolean remove(Object o)

Removes the first occurrence of the specified element from this list, if it is present (optional operation).
boolean removeAll(Collection<?> c)

Removes from this list all of its elements that are contained in the specified collection (optional operation).
default void replaceAll(UnaryOperator<E> operator)

Replaces each element of this list with the result of applying the operator to that element.
boolean retainAll(Collection<?> c)

Retains only the elements in this list that are contained in the specified collection (optional operation).
E set(int index, E element)

Replaces the element at the specified position in this list with the specified element (optional operation).
int size()

Returns the number of elements in this list.
default void sort(Comparator<? super E> c)

Sorts this list according to the order induced by the specified Comparator.
default Spliterator<E> spliterator()

Creates a Spliterator over the elements in this list.
List<E> subList(int fromIndex, int toIndex)

Returns a view of the portion of this list between the specified fromIndex, inclusive, and toIndex, exclusive.
Object[] toArray()

Returns an array containing all of the elements in this list in proper sequence (from first to last element).
<T> T[] toArray(T[] a)

Returns an array containing all of the elements in this list in proper sequence (from first to last element); the runtime type of the returned array is that of the specified array.

ArrayList

  • 优点: 底层数据结构是数组,查询快,增删慢。
  • 缺点: 线程不安全,效率高

Vector

  • 优点: 底层数据结构是数组,查询快,增删慢。
  • 缺点: 线程安全,效率低

LinkedList

  • 优点: 底层数据结构是链表,查询慢,增删快。
  • 缺点: 线程不安全,效率高

PS:ArrayList是实现了基于底层的数据结构,LinkedList基于底层数据结构是链表;对于随机访问get和set,ArrayList觉得优于LinkedList,因为LinkedList遍历全部再确定;对于新增和删除操作add和remove,LinedList比较占优势,因为ArrayList要移动数据。

Collection是集合接口

|————Set子接口:无序,不允许重复。
|————List子接口:有序,可以有重复元素。

区别:Collections是集合类

Set和List对比:

  • Set:检索元素效率低下,删除和插入效率高,插入和删除不会引起元素位置改变。
  • List:和数组类似,List可以动态增长,查找元素效率高,插入删除元素效率低,因为会引起其他元素位置改变。

 

List的排序示例:

List<Integer> list = new ArrayList<>();
list.add(12);
list.add(1);
list.add(15);
list.add(5);
list.add(65);
list.sort((a,b)->{
    return a-b;
});
out(list);

打印结果:
[1, 5, 12, 15, 65]

如果降序,只需要把a-b改成b-a即可。

 

3.Map对象

Map 是一种把键对象和值对象映射的集合,它的每一个元素都包含一对键对象和值对象。 Map没有继承于Collection接口 从Map集合中检索元素时,只要给出键对象,就会返回对应的值对象。

map主要的特性:

  • key字段不可重复
  • key,value 都可以是任何引用类型的数据,包括 null
  • Map 取代了古老的 Dictionary 抽象类

因此,每个 key 只能对应一个 value, 多个 key 可以对应一个 value。Map集合维护“键、值对”的关联性,使你可以通过“键”查找“值”。

Modifier and Type Method and Description
void clear()

Removes all of the mappings from this map (optional operation).
default V compute(K key, BiFunction<? super K,? super V,? extends V> remappingFunction)

Attempts to compute a mapping for the specified key and its current mapped value (or null if there is no current mapping).
default V computeIfAbsent(K key, Function<? super K,? extends V> mappingFunction)

If the specified key is not already associated with a value (or is mapped to null), attempts to compute its value using the given mapping function and enters it into this map unless null.
default V computeIfPresent(K key, BiFunction<? super K,? super V,? extends V> remappingFunction)

If the value for the specified key is present and non-null, attempts to compute a new mapping given the key and its current mapped value.
boolean containsKey(Object key)

Returns true if this map contains a mapping for the specified key.
boolean containsValue(Object value)

Returns true if this map maps one or more keys to the specified value.
Set<Map.Entry<K,V>> entrySet()

Returns a Set view of the mappings contained in this map.
boolean equals(Object o)

Compares the specified object with this map for equality.
default void forEach(BiConsumer<? super K,? super V> action)

Performs the given action for each entry in this map until all entries have been processed or the action throws an exception.
V get(Object key)

Returns the value to which the specified key is mapped, or null if this map contains no mapping for the key.
default V getOrDefault(Object key, V defaultValue)

Returns the value to which the specified key is mapped, or defaultValue if this map contains no mapping for the key.
int hashCode()

Returns the hash code value for this map.
boolean isEmpty()

Returns true if this map contains no key-value mappings.
Set<K> keySet()

Returns a Set view of the keys contained in this map.
default V merge(K key, V value, BiFunction<? super V,? super V,? extends V> remappingFunction)

If the specified key is not already associated with a value or is associated with null, associates it with the given non-null value.
V put(K key, V value)

Associates the specified value with the specified key in this map (optional operation).
void putAll(Map<? extends K,? extends V> m)

Copies all of the mappings from the specified map to this map (optional operation).
default V putIfAbsent(K key, V value)

If the specified key is not already associated with a value (or is mapped to null) associates it with the given value and returns null, else returns the current value.
V remove(Object key)

Removes the mapping for a key from this map if it is present (optional operation).
default boolean remove(Object key, Object value)

Removes the entry for the specified key only if it is currently mapped to the specified value.
default V replace(K key, V value)

Replaces the entry for the specified key only if it is currently mapped to some value.
default boolean replace(K key, V oldValue, V newValue)

Replaces the entry for the specified key only if currently mapped to the specified value.
default void replaceAll(BiFunction<? super K,? super V,? extends V> function)

Replaces each entry’s value with the result of invoking the given function on that entry until all entries have been processed or the function throws an exception.
int size()

Returns the number of key-value mappings in this map.
Collection<V> values()

Returns a Collection view of the values contained in this map.

Map的功能方法

  • put(Object key, Object value)添加一个“值”(想要得东西)和与“值”相关联的“键”(key)(使用它来查找)。
  • get(Object key)返回与给定“键”相关联的“值”。
  • containsKey()和containsValue()可以检查Map中是否包含某个“键”或“值”。

标准的Java类库中包含了几种不同的Map:

HashMap, TreeMap, LinkedHashMap, WeakHashMap, IdentityHashMap。

它们都有同样的基本接口Map,但是行为、效率、排序策略、保存对象的生命周期和判定“键”等价的策略等各不相同。

执行效率是Map的一个大问题,看看get方法的代码,就会明白为什么在ArrayList中搜索“键”是相当慢的。而这正是HashMap提高速度的地方。HashMap使用了特殊的值,称为“散列码”(hash code),来取代对键的缓慢搜索。“散列码”是“相对唯一”用以代表对象的int值,它是通过将该对象的某些信息进行转换而生成的。所有Java对象都能产生散列码,因为hashCode()是定义在基类Object中的方法。

HashMap

Map基于散列表的实现,就是使用对象的hashCode()进行快速查询的,插入和查询“键值对”的开销是固定的,此方法能够显着提高性能。

 

LinkedHashMap

类似于HashMap,但是迭代遍历它时,取得“键值对”的顺序是其插入次序,或者是最近最少使用(LRU算法)的次序;只比HashMap慢一点,而在迭代访问时发而更快,因为它使用链表维护内部次序。

TreeMap

基于红黑树数据结构的实现,查看“键”或“键值对”时,它们会被排序(次序由Comparabel或Comparator决定)。TreeMap的特点在于,你得到的结果是经过排序的。TreeMap是唯一的带有subMap()方法的Map,它可以返回一个子树。

WeakHashMao

弱键(weak key)Map,Map中使用的对象也被允许释放: 这是为解决特殊问题设计的。如果没有map之外的引用指向某个“键”,则此“键”可以被垃圾收集器回收。

IdentifyHashMap

使用==代替equals()对“键”作比较的hash map,专为解决特殊问题而设计。

总结

List,Set,Map将持有对象一律视为Object型别;Collection、List、Set、Map都是接口,不能实例化。继承自它们的 ArrayList, Vector, HashTable, HashMap是具体的对象,可被实例化。vector容器确切知道它所持有的对象隶属什么型别,vector不进行边界检查。

注意:

  • 如果涉及到堆栈,队列等操作,应该考虑用List,对于需要快速插入,删除元素,应该使用LinkedList,如果需要快速随机访问元素,应该使用ArrayList。
  • 如果程序在单线程环境中,或者访问仅仅在一个线程中进行,考虑非同步的类,其效率较高,如果多个线程可能同时操作一个类,应该使用同步的类。
  • 在除需要排序时使用TreeSet、TreeMap外,其他应使用HashSet,HashMap,因为他们的效率更高。
  • 要特别注意对哈希表的操作,作为key的对象要正确复写equals和hashCode方法。
  • 容器类仅能持有对象引用(指向对象的指针),而不是将对象信息copy一份至数列某位置,一旦将对象置入容器内,便损失了该对象的型别信息。
  • 尽量返回接口而非实际的类型,如返回List而非ArrayList,这样如果以后需要将ArrayList换成LinkedList时,客户端代码不用改变。这就是针对抽象编程。
  • Collection没有get()方法来取得某个元素。只能通过iterator()遍历元素。
  • Set和Collection拥有一模一样的接口。
  • List,可以通过get()方法来一次取出一个元素。
  • 一般使用ArrayList;用LinkedList构造堆栈stack、队列使用queue。
  • Map用 put(k,v) / get(k),还可以使用containsKey()/containsValue()来检查其中是否含有某个key、alue。
  • HashMap会利用对象的hashCode来快速找到key。
  • Map中元素,可以将key序列、value序列单独抽取出来。
  • 使用keySet()抽取key序列,将map中的所有keys生成一个Set。
  • 使用values()抽取value序列,将map中的所有values生成一个Collection。
  • 为什么一个生成Set,一个生成Collection呢?因为,key总是独一无二的,value允许重复。

 

Java语言Volatile原理

Java语言提供了一种稍弱的同步机制,即volatile变量,用来确保将变量的更新操作通知到其他线程。当把变量声明为volatile类型后,编译器与运行时都会注意到这个变量是共享的,因此不会将该变量上的操作与其他内存操作一起重排序。volatile变量不会被缓存在寄存器或者对其他处理器不可见的地方,因此在读取volatile类型的变量时总会返回最新写入的值。

在访问volatile变量时不会执行加锁操作,因此也就不会使执行线程阻塞,因此volatile变量是一种比sychronized关键字更轻量级的同步机制。

1553739684-9830-20160708224602686-2141387366

当对非 volatile 变量进行读写的时候,每个线程先从内存拷贝变量到CPU缓存中。如果计算机有多个CPU,每个线程可能在不同的CPU上被处理,这意味着每个线程可以拷贝到不同的 CPU cache 中。

而声明变量是 volatile 的,JVM 保证了每次读变量都从内存中读,跳过 CPU cache 这一步。

当一个变量定义为 volatile 之后,将具备两种特性:

1.保证此变量对所有的线程的可见性,这里的“可见性”,当一个线程修改了这个变量的值,volatile 保证了新值能立即同步到主内存,以及每次使用前立即从主内存刷新。但普通变量做不到这点,普通变量的值在线程间传递均需要通过主内存来完成。

2.禁止指令重排序优化。有volatile修饰的变量,赋值后多执行了一个“load addl $0x0, (%esp)”操作,这个操作相当于一个内存屏障(指令重排序时不能把后面的指令重排序到内存屏障之前的位置),只有一个CPU访问内存时,并不需要内存屏障;

什么是指令重排序:是指CPU采用了允许将多条指令不按程序规定的顺序分开发送给各相应电路单元处理。

volatile 性能:

volatile 的读性能消耗与普通变量几乎相同,但是写操作稍慢,因为它需要在本地代码中插入许多内存屏障指令来保证处理器不发生乱序执行。

1181920212232
 
Copyright © 2008-2021 lanxinbase.com Rights Reserved. | 粤ICP备14086738号-3 |