JDK1.8新特性(六): Map和日期

一:简介

JDK1.8中Map新增了一些方法,其中一部分方法是为了简化代码的,如forEach,另外一些方法是为了防止null,使操作代码更加严谨。

二:Map

public interface Map<K,V> {
    // 如果key存在,则忽略put操作
    default V putIfAbsent(K key, V value) {
        V v = get(key);
        if (v == null) {
            v = put(key, value);
        }

        return v;
    }

    // 循环
    public void forEach(BiConsumer<? super K, ? super V> action){...}

    // 如果存在则计算:先判断key是否存在,如果key存在,将BiFunction计算的结果作为key的新值重新put进去
    public V computeIfPresent(K key, BiFunction<? super K, ? super V, ? extends V> remappingFunction) {}

    // 如果key不存在则将计算的结果put进去
    public V computeIfAbsent(K key, Function<? super K, ? extends V> mappingFunction) {}

    // 只有key-value同时满足条件时才会删除
    public boolean remove(Object key, Object value){}

    // 如果key不存在,则返回默认值
    public V getOrDefault(Object key, V defaultValue) {}

    // 合并:将BiFunction的结果作为key的新值
    public V merge(K key, V value, BiFunction<? super V, ? super V, ? extends V> remappingFunction) {}

三:示例

@Test
public void putIfAbsent() {
    Map<String, String> map = new HashMap<>();
    map.putIfAbsent("key", "oldValue");
    // 如果key存在,则忽略put操作
    map.putIfAbsent("key", "newValue");
    String value = map.get("key");
    System.out.println(value);
}

@Test
public void forEach() {
    Map<String, String> map = new HashMap<>();
    map.putIfAbsent("key1", "value1");
    map.putIfAbsent("key2", "value1");
    map.putIfAbsent("key3", "value1");

    map.forEach((key, value) -> System.out.println(key + ":" + value));
}

@Test
public void computeIfPresent() {
    Map<String, String> map = new HashMap<>();
    map.putIfAbsent("key1", "value1");

    // 如果存在则计算:先判断key是否存在,如果key存在,将BiFunction计算的结果作为key的新值重新put进去
    map.computeIfPresent("key1", (key, value) -> key + "=" + value);
    String value = map.get("key1");
    System.out.println(value);

    // 如果计算的结果为null,相当于从map中移除
    map.computeIfPresent("key1", (k, v) -> null);
    boolean contain = map.containsKey("key1");
    System.out.println(contain);
}

@Test
public void computeIfAbsent() {
    // 如果key不存在则将计算的结果put进去
    Map<String, String> map = new HashMap<>();
    map.computeIfAbsent("key2", v -> "value2");
    boolean contain = map.containsKey("key2");
    System.out.println(contain);
}

@Test
public void remove(){
    Map<String, String> map = new HashMap<>();
    map.putIfAbsent("key1", "value1");
    boolean result = map.remove("key1", "value2");
    System.out.println(result);
}

@Test
public void getOrDefault(){
    Map<String, String> map = new HashMap<>();
    String value = map.getOrDefault("key1", "default value");
    System.out.println(value);
}

@Test
public void merge(){
    Map<String, String> map = new HashMap<>();
    map.put("key1", "value1");

    map.merge("key1", "newValue", (value, newValue) -> value + "-" + newValue);
    String value = map.get("key1");
    // value1-newValue
    System.out.println(value);
}

Java 8 在包java.time下包含了一组全新的时间日期API。新的日期API和开源的Joda-Time库差不多,但又不完全一样。

Clock 时钟

Clock类提供了访问当前日期和时间的方法,Clock是时区敏感的,可以用来取代 System.currentTimeMillis() 来获取当前的微秒数。某一个特定的时间点也可以使用Instant类来表示,Instant类也可以用来创建老的java.util.Date对象。

@Test
public void clock(){
    Clock clock = Clock.systemDefaultZone();
    long millis = clock.millis();
    Instant instant = clock.instant();
    Date date = Date.from(instant);

    System.out.println(millis);
    System.out.println(date);
}

Timezones 时区

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

@Test
public void timezones(){
    // 获取所有可用的时区
    Set<String> availableZoneIds = ZoneId.getAvailableZoneIds();
    System.out.println(availableZoneIds);

    // 获取默认的时区 Asia/Shanghai
    ZoneId zoneId = ZoneId.systemDefault();
    System.out.println(zoneId);

    // 获取时区规则 ZoneRules[currentStandardOffset=+08:00]
    ZoneRules rules = zoneId.getRules();
    System.out.println(rules);
}

LocalTime 本地时间

LocalTime 定义了一个没有时区信息的时间,例如 晚上10点,或者 17:30:15。

@Test
public void localTime(){
    ZoneId zoneId = ZoneId.systemDefault();
    ZoneId zoneId2 = ZoneId.of("Etc/GMT+8");

    // 获取指定时区的当前时间
    LocalTime now = LocalTime.now(zoneId);
    LocalTime now2 = LocalTime.now(zoneId2);

    // 判断一个本地时间是否在另一个本地时间之前
    boolean isBefore = now.isBefore(now2);

    // 获取两个本地时间小时之差
    long hours = ChronoUnit.HOURS.between(now, now2);
    System.out.println(hours);

    // 获取两个本地时间分钟之差
    long minutes = ChronoUnit.MINUTES.between(now, now2);
    System.out.println(minutes);

    LocalTime localTime = LocalTime.of(23, 59, 59);
    System.out.println(localTime);       // 23:59:59

    DateTimeFormatter formatter = DateTimeFormatter.ofLocalizedTime(FormatStyle.SHORT).withLocale(Locale.GERMAN);
    LocalTime local = LocalTime.parse("13:37", formatter);
    System.out.println(local);  // 13:37
}

LocalDate 本地日期

LocalDate 表示了一个确切的日期,比如 2014-03-11。该对象值是不可变的,用起来和LocalTime基本一致。

@Test
public void localDate(){
    LocalDate today = LocalDate.now();
    LocalDate tomorrow = today.plus(1, ChronoUnit.DAYS);
    LocalDate yesterday = tomorrow.minusDays(2);

    System.out.println(today + "," + tomorrow + "," + yesterday);

    LocalDate independenceDay = LocalDate.of(2014, Month.JULY, 4);
    DayOfWeek dayOfWeek = independenceDay.getDayOfWeek();
    System.out.println(dayOfWeek);


    DateTimeFormatter germanFormatter = DateTimeFormatter.ofLocalizedDate(FormatStyle.MEDIUM)
                    .withLocale(Locale.GERMAN);
    LocalDate localDate = LocalDate.parse("24.12.2014", germanFormatter);
    System.out.println(localDate);   // 2014-12-24
}

LocalDateTime 本地日期时间

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

@Test
public void localDateTime(){
    LocalDateTime localDateTime = LocalDateTime.of(2014, Month.DECEMBER, 31, 23, 59, 59);
    DayOfWeek dayOfWeek = localDateTime.getDayOfWeek();
    System.out.println(dayOfWeek);

    Month month = localDateTime.getMonth();
    System.out.println(month);

    long minuteOfDay = localDateTime.getLong(ChronoField.MINUTE_OF_DAY);
    System.out.println(minuteOfDay);

    Instant instant = localDateTime.atZone(ZoneId.systemDefault()).toInstant();
    Date date = Date.from(instant);
    System.out.println(date);

    // DateTimeFormatter是不可变的,所以它是线程安全的
    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
    LocalDateTime parsed = LocalDateTime.parse("2018-05-07 07:13:00", formatter);
    String string = formatter.format(parsed);
    System.out.println(string);
}

猜你喜欢

转载自blog.csdn.net/vbirdbest/article/details/80268231