Java|Java 8 要点

Java 8 要点

  1. new Date/Time API
  2. Stream
  3. Lambda Expressions
  4. Method references
  5. Optional
  6. Default Method
new Date/Time API - java.time
  • 老时间库的问题 - why jsr310
    1. java.util.Date和SimpleDateFormat 线程不安全
    2. 月份从1开始,日期从0开始
    3. 两个相同的java.sql.Time实例不equals
    4. java.util.Date vs. java.sql.Timestamp/Date/Time
  • 新的时间库的设计思想
    1. Immutable
    2. LocalDate, LocalTime, LocalDateTime
    3. 如果需要时区信息,换另外一套
  • 新老时间类型互转
    1. java.sql.Timestamp#toLocalDateTime
    2. java.sql.Timestamp#from
  • format
@Test public void format() throws Exception { DateTimeFormatter dateFormatter = DateTimeFormatter.ISO_LOCAL_DATE; Assert.assertEquals("2011-11-11", dateFormatter.format(LocalDate.of(2011, 11, 11))); }

  • parse
@Test public void parse() throws Exception { DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd"); LocalDate day = LocalDate.parse("1900-01-01", dateFormatter); Assert.assertEquals(LocalDate.of(1900, 1, 1), day); }

  • JDBC
    JDBC4.2(mysql java 驱动6.0)开始才支持 Java 8的新的时间库,在使用JDBC4.2之前,使用时需要将新的类型转换成老的类型
  • Spring MVC
@SpringBootApplication public class WebMain extends WebMvcConfigurerAdapter {@Override public void addFormatters(FormatterRegistry registry) { DateTimeFormatterRegistrar registrar = new DateTimeFormatterRegistrar(); registrar.setDateFormatter(DateTimeFormatter.ISO_DATE); registrar.setTimeFormatter(DateTimeFormatter.ISO_TIME); registrar.setDateTimeFormatter(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")); registrar.registerFormatters(registry); }}

  • JPA
@Bean public LocalContainerEntityManagerFactoryBean catalogEntityManagerFactory( @Qualifier(CatalogRepositoryConfig.UNIT_NAME) DataSource catalogDataSource, EntityManagerFactoryBuilder builder, JpaProperties jpaProperties) { return builder.dataSource(catalogDataSource) .packages(Server.class, Jsr310JpaConverters.class) .persistenceUnit(UNIT_NAME) .properties(jpaProperties.getHibernateProperties(catalogDataSource)) .build(); }

  • Jackson
spring.jackson.serialization.WRITE_DATES_AS_TIMESTAMPS = false

com.fasterxml.jackson.datatype jackson-datatype-jsr310

@Bean public SimpleModule jsr310JacksonModule() { SimpleModule jsr310Module = new SimpleModule(); DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); jsr310Module.addDeserializer(LocalDateTime.class, new LocalDateTimeDeserializer(dateTimeFormatter)); jsr310Module.addSerializer(LocalDateTime.class, new LocalDateTimeSerializer(dateTimeFormatter)); return jsr310Module; }

  • 时间计算
@Test public void plusOrMinus() throws Exception { LocalDateTime payTime = LocalDateTime.of(2015, 10, 10, 19, 0); Assert.assertEquals(LocalDateTime.of(2015, 10, 30, 19, 0), payTime.plusDays(20)); Assert.assertEquals(LocalDateTime.of(2015, 10, 11, 5, 0), payTime.plusHours(10)); Assert.assertEquals(LocalDateTime.of(2015, 9, 30, 19, 0), payTime.minusDays(10)); }

stream consumer
使用场景
  1. groupBy
  2. partition
  3. sum
  4. map
  5. filter
【Java|Java 8 要点】注意
  1. stream只能consume一次
  2. Iterable没有stream
  3. stream代表的是一些function的整合,并不是一个数据结构
  4. parallel stream 慎用
  5. stream的每个function最好是没有副作用的
Lambda Expressions Method References Optional
  1. Optional
  2. OptionalInt, OptionalLong, OptionalDouble
参考资料
  1. https://leanpub.com/whatsnewinjava8/read
  2. https://github.com/adamd/hellojava8
  3. https://github.com/katoquro/jdk8-lambda-samples
  4. https://octoperf.com/blog/2015/08/24/one-year-with-java-8-in-production/
  5. http://zeroturnaround.com/rebellabs/java-parallel-streams-are-bad-for-your-health/
  6. http://zeroturnaround.com/rebellabs/how-your-addiction-to-java-8-default-methods-may-make-pandas-sad-and-your-teammates-angry/
  7. http://zeroturnaround.com/rebellabs/java-8-explained-default-methods/
  8. http://blog.jooq.org/2014/06/13/java-8-friday-10-subtle-mistakes-when-using-the-streams-api/
  9. http://blog.jooq.org/2014/04/04/java-8-friday-the-dark-side-of-java-8/
  10. http://winterbe.com/posts/2014/07/31/java8-stream-tutorial-examples/
  11. http://blog.takipi.com/new-parallelism-apis-in-java-8-behind-the-glitz-and-glamour/
  12. http://blog.takipi.com/the-dark-side-of-lambda-expressions-in-java-8/
  13. https://robinst.github.io/guava-java8-presentation

    推荐阅读