使用流的列表的min非null LocalDateTime

时间:2017-09-13 07:07:36

标签: java java-8 java-stream

我知道如何获得列表的最小LocalDateTime,感谢:https://stackoverflow.com/a/20996009/270143

e.g。 LocalDateTime minLdt = list.stream().map(u -> u.getMyLocalDateTime()).min(LocalDateTime::compareTo).get();

虽然我的问题略有不同......

  1. 我希望分钟LocalDateTime不是null
  2. null如果他们都是null
  3. 我将如何以简洁的方式做到这一点?

2 个答案:

答案 0 :(得分:7)

你可以这样做:

Optional<LocalDateTime> op = list.stream()
        .filter(Objects::nonNull)
        .min(Comparator.naturalOrder());

absent值表示列表中只有空值(或列表为空)

答案 1 :(得分:3)

这样的事,也许?

[mainmenu]
  exten => s,1,Answer
  exten => s,2,SetMusicOnHold(default)
  exten => s,3,DigitTimeout,5
  exten => s,4,ResponseTimeout,10
  ;SAI menu - 1 for tech support, 2 for voicemail, 3 for echo test
  exten => s,5,Background(sai-welcome)
  exten => s,6,Background(sai-choose)

  ; Tech Support
  exten => 1,1,AGI(dima-test.agi)
  exten => 1,2,SetGlobalVar(ACCOUNTCODE=${callerid})
  exten => 1,3,SetVar(testcallerid=${callerid})
  exten => 1,4,Background(sai-reptech-welcome)
  exten => 1,5,Queue(rep-tech)

  ; Leave Voicemail
  exten => 2,1,VoicemailMain()
  exten => 2,2,Hangup


  ; Play Music-on-Hold
  exten => 5,1,MusicOnHold(default)
  exten => 5,2,Goto(mainmenu,s,6)
  ; #=hangup
  exten => #,1,Playback(sai-thanks)
  exten => #,2,Hangup

  exten => t,1,Goto(#,1)         ; If they take too long, give up
  exten => i,1,Playback(invalid) ; "That's not valid, try again"