java8新特性详解_JAVA8 十大新特性详解

2022年11月14日11:59:14

1 long t0 =System.nanoTime();2 long count =values.stream().sorted().count();3 System.out.println(count);4 long t1 =System.nanoTime();5 long millis = TimeUnit.NANOSECONDS.toMillis(t1 -t0);6 System.out.println(String.format("sequential sort took: %d ms", millis));

// 串行耗时: 899 ms并行排序:

1 long t0 =System.nanoTime();2 long count =values.parallelStream().sorted().count();3 System.out.println(count);4 long t1 =System.nanoTime();5 long millis = TimeUnit.NANOSECONDS.toMillis(t1 -t0);6 System.out.println(String.format("parallel sort took: %d ms", millis));

// 并行排序耗时: 472 ms

上面两个代码几乎是一样的,但是并行版的快了50%之多,唯一需要做的改动就是将stream()改为parallelStream()。

Map

前面提到过,Map类型不支持stream,不过Map提供了一些新的有用的方法来处理一些日常任务。

1 Map map = new HashMap<>();2 for (int i = 0; i < 10; i++) {3 map.putIfAbsent(i, "val" +i);4 }

map.forEach((id, val) -> System.out.println(val));

以上代码很容易理解, putIfAbsent 不需要我们做额外的存在性检查,而forEach则接收一个Consumer接口来对map里的每一个键值对进行操作。

下面的例子展示了map上的其他有用的函数:

1 map.computeIfPresent(3, (num, val) -> val +num);2 map.get(3); //val33

3 map.computeIfPresent(9, (num, val) -> null);4 map.containsKey(9); //false

5 map.computeIfAbsent(23, num -> "val" +num);6 map.containsKey(23); //true

7 map.computeIfAbsent(3, num -> "bam");8 map.get(3); //val33

接下来展示如何在Map里删除一个键值全都匹配的项:

1 map.remove(3, "val3");2 map.get(3); //val33

3 map.remove(3, "val33");4 map.get(3); //null

另外一个有用的方法

map.getOrDefault(42, "not found"); //not found

对Map的元素做合并也变得很容易了:

1 map.merge(9, "val9", (value, newValue) ->value.concat(newValue));2 map.get(9); //val9

3 map.merge(9, "concat", (value, newValue) ->value.concat(newValue));4 map.get(9); //val9concat

Merge做的事情是如果键名不存在则插入,否则则对原键对应的值做合并操作并重新插入到map中。

九、Date API

Java 8 在包java.time下包含了一组全新的时间日期API。新的日期API和开源的Joda-Time库差不多,但又不完全一样,下面的例子展示了这组新API里最重要的一些部分:

Clock 时钟

Clock类提供了访问当前日期和时间的方法,Clock是时区敏感的,可以用来取代 System.currentTimeMillis()

来获取当前的微秒数。某一个特定的时间点也可以使用Instant类来表示,Instant类也可以用来创建老的java.util.Date对象。

1 lock clock =Clock.systemDefaultZone();2 long millis =clock.millis();3 Instant instant =clock.instant();4 Date legacyDate = Date.from(instant); //legacy java.util.Date

Timezones 时区

在新API中时区使用ZoneId来表示。时区可以很方便的使用静态方法of来获取到。 时区定义了到UTS时间的时间差,在Instant时间点对象到本地日期对象之间转换的时候是极其重要的。

1 System.out.println(ZoneId.getAvailableZoneIds());2 //prints all available timezone ids

3 ZoneId zone1 = ZoneId.of("Europe/Berlin");4 ZoneId zone2 = ZoneId.of("Brazil/East");5 System.out.println(zone1.getRules());6 System.out.println(zone2.getRules());7 //ZoneRules[currentStandardOffset=+01:00]8 //ZoneRules[currentStandardOffset=-03:00]

LocalTime 本地时间

LocalTime 定义了一个没有时区信息的时间,例如 晚上10点,或者 17:30:15。下面的例子使用前面代码创建的时区创建了两个本地时间。之后比较时间并以小时和分钟为单位计算两个时间的时间差:

1 LocalTime now1 =LocalTime.now(zone1);2 LocalTime now2 =LocalTime.now(zone2);3 System.out.println(now1.isBefore(now2)); //false

4 long hoursBetween =ChronoUnit.HOURS.between(now1, now2);5 long minutesBetween =ChronoUnit.MINUTES.between(now1, now2);6 System.out.println(hoursBetween); //-3

7 System.out.println(minutesBetween); //-239

LocalTime 提供了多种工厂方法来简化对象的创建,包括解析时间字符串。

1 LocalTime late = LocalTime.of(23, 59, 59);2 System.out.println(late); //23:59:59

3 DateTimeFormatter germanFormatter =

4 DateTimeFormatter5 .ofLocalizedTime(FormatStyle.SHORT)6 .withLocale(Locale.GERMAN);7 LocalTime leetTime = LocalTime.parse("13:37", germanFormatter);8 System.out.println(leetTime); //13:37

LocalDate 本地日期

LocalDate 表示了一个确切的日期,比如 2014-03-11。该对象值是不可变的,用起来和LocalTime基本一致。下面的例子展示了如何给Date对象加减天/月/年。另外要注意的是这些对象是不可变的,操作返回的总是一个新实例。

1 LocalDate today =LocalDate.now();2 LocalDate tomorrow = today.plus(1, ChronoUnit.DAYS);3 LocalDate yesterday = tomorrow.minusDays(2);4 LocalDate independenceDay = LocalDate.of(2014, Month.JULY, 4);5 DayOfWeek dayOfWeek = independenceDay.getDayOfWeek();

System.out.println(dayOfWeek);    // FRIDAY从字符串解析一个LocalDate类型和解析LocalTime一样简单:

1 DateTimeFormatter germanFormatter =

2 DateTimeFormatter3 .ofLocalizedDate(FormatStyle.MEDIUM)4 .withLocale(Locale.GERMAN);5 LocalDate xmas = LocalDate.parse("24.12.2014", germanFormatter);6 System.out.println(xmas); //2014-12-24

LocalDateTime 本地日期时间

LocalDateTime 同时表示了时间和日期,相当于前两节内容合并到一个对象上了。LocalDateTime和LocalTime还有LocalDate一样,都是不可变的。LocalDateTime提供了一些能访问具体字段的方法。

1 LocalDateTime sylvester = LocalDateTime.of(2014, Month.DECEMBER, 31, 23, 59, 59);2 DayOfWeek dayOfWeek =sylvester.getDayOfWeek();3 System.out.println(dayOfWeek); //WEDNESDAY

4 Month month =sylvester.getMonth();5 System.out.println(month); //DECEMBER

6 long minuteOfDay =sylvester.getLong(ChronoField.MINUTE_OF_DAY);7 System.out.println(minuteOfDay); //1439

只要附加上时区信息,就可以将其转换为一个时间点Instant对象,Instant时间点对象可以很容易的转换为老式的java.util.Date。

1 Instant instant =sylvester2 .atZone(ZoneId.systemDefault())3 .toInstant();4 Date legacyDate =Date.from(instant);5 System.out.println(legacyDate); //Wed Dec 31 23:59:59 CET 2014

格式化LocalDateTime和格式化时间和日期一样的,除了使用预定义好的格式外,我们也可以自己定义格式:

1 DateTimeFormatter formatter =

2 DateTimeFormatter3 .ofPattern("MMM dd, yyyy - HH:mm");4 LocalDateTime parsed = LocalDateTime.parse("Nov 03, 2014 - 07:13", formatter);5 String string =formatter.format(parsed);6 System.out.println(string); //Nov 03, 2014 - 07:13

和java.text.NumberFormat不一样的是新版的DateTimeFormatter是不可变的,所以它是线程安全的。

关于时间日期格式的详细信息:http://download.java.net/jdk8/docs/api/java/time/format/DateTimeFormatter.html

十、Annotation 注解

在Java 8中支持多重注解了,先看个例子来理解一下是什么意思。

首先定义一个包装类Hints注解用来放置一组具体的Hint注解:

1 @interfaceHints {2 Hint[] value();3 }4 @Repeatable(Hints.class)5 @interfaceHint {6 String value();7 }

Java 8允许我们把同一个类型的注解使用多次,只需要给该注解标注一下@Repeatable即可。

例 1: 使用包装类当容器来存多个注解(老方法)

1 @Hints({@Hint("hint1"), @Hint("hint2")})2 class Person {}

例 2:使用多重注解(新方法)

1 @Hint("hint1")2 @Hint("hint2")3 class Person {}

第二个例子里java编译器会隐性的帮你定义好@Hints注解,了解这一点有助于你用反射来获取这些信息:

1 Hint hint = Person.class.getAnnotation(Hint.class);2 System.out.println(hint); //null

3 Hints hints1 = Person.class.getAnnotation(Hints.class);4 System.out.println(hints1.value().length); //2

5 Hint[] hints2 = Person.class.getAnnotationsByType(Hint.class);6 System.out.println(hints2.length); //2

即便我们没有在Person类上定义@Hints注解,我们还是可以通过 getAnnotation(Hints.class) 来获取 @Hints注解,更加方便的方法是使用 getAnnotationsByType 可以直接获取到所有的@Hint注解。另外Java 8的注解还增加到两种新的target上了:

1 @Target({ElementType.TYPE_PARAMETER, ElementType.TYPE_USE})2 @interface MyAnnotation {}

关于Java 8的新特性就写到这了,肯定还有更多的特性等待发掘。JDK 1.8里还有很多很有用的东西,比如Arrays.parallelSort, StampedLock和CompletableFuture等等。

  • 作者:weixin_40008884
  • 原文链接:https://blog.csdn.net/weixin_40008884/article/details/114233011
    更新时间:2022年11月14日11:59:14 ,共 5873 字。