3

I have to get the first element of an array., but it is possible that the element is empty; If the element is empty I put an empty field (I am trying to generate a pdf)

Here is my code now:

public void makePdf(Long id) throws IOException {
    Candidacy ca = candidacyRepository.findOne(id); 
    cos.beginText();

    cos.showText(
       ca.getInterviews().stream().map(Interview::getAgency).map(Agency::getAgencyName).collect( Collectors.toList()).get(0)!=null?ca.getInterviews().stream().map(Interview::getAgency).map(Agency::getAgencyName).collect( Collectors.toList()).get(0):"")); 

    cos.endText();
}

So I will wish not to prevent the generation of the pdf. Thank you very much for your support!

UPDATE

Sorry for the lack of precision: I also sort on the date.

public void makePdf(Long id) throws IOException {
        Candidacy ca = candidacyRepository.findOne(id); 
        cos.beginText();

        cos.showText(
           ca.getInterviews().stream().sorted((a,b)-> a.getInterviewDate().compareTo(b.getInterviewDate())).sorted((a,f)->f.getInterviewDate().compareTo(a.getInterviewDate())).sorted((b,f)->b.getInterviewDate().compareTo(f.getInterviewDate())).map(Interview::getAgency).map(Agency::getAgencyName).collect( Collectors.toList()).get(0)!=null?ca.getInterviews().stream().sorted((a,b)-> a.getInterviewDate().compareTo(b.getInterviewDate())).sorted((a,f)->f.getInterviewDate().compareTo(a.getInterviewDate())).sorted((b,f)->b.getInterviewDate().compareTo(f.getInterviewDate())).map(Interview::getAgency).map(Agency::getAgencyName).collect( Collectors.toList()).get(0):"")); 

        cos.endText();
    }

I get a NullPointerException:/ Thank you for you help

Adam D
  • 85
  • 2
  • 2
  • 9

1 Answers1

5

This code doesn't make sense. You are executing the same Stream pipeline twice, and each time you generate an entire List when you only need the first element of that List.

You can use findFirst to get the first element of the Stream.

EDIT :

After testing my original answer, it turned out it doesn't work. findFirst() throws a NullPointerException if the first element is null.

You can avoid that by setting the default value before calling findFirst() :

ca.getInterviews().stream()
                  .map(Interview::getAgency)
                  .map(Agency::getAgencyName)
                  .map(s->s!=null?s:"") // this will replace null with ""
                  .firstFirst()
                  .get();
Eran
  • 387,369
  • 54
  • 702
  • 768
  • 7
    I would use `.filter(s -> s != null)` or `.filter(Objects::nonNull)` then you will get the first non `null` value. – Peter Lawrey Sep 21 '16 at 13:33
  • 1
    @PeterLawrey I didn't think that was the intent - based on the OP's code, the intent was to get the first element and convert it to "" if it's null, not to filter out the `null` values. I might be wrong though. – Eran Sep 21 '16 at 13:35
  • 1
    I was thinking the use of `""` was a work around but you could be right. – Peter Lawrey Sep 21 '16 at 13:36
  • Sorry for the lack of precision: I must sort on three elements, That's why I have not used "findFirst()". – Adam D Sep 21 '16 at 13:53
  • 1
    @AdamD But you only take the first element of the list after sorting the stream and collecting to a List. `findFirst()` will still work (but all the elements of the Stream will be processed, in order to sort them). – Eran Sep 21 '16 at 13:56
  • @Eran In fact, I executed as you have shown me but I still have NullPointerException – Adam D Sep 21 '16 at 14:10
  • @AdamD Is it possible that `getAgency()` returns null? – Eran Sep 21 '16 at 17:26
  • @Eran , yes of course – Adam D Sep 21 '16 at 20:42
  • @AdamD And if `getAgency()` of the first `Interview` is null, what do you want to return? Perhaps it would be better to just filter out such elements (i.e. `...map(Interview::getAgency).filter(Objects::nonNull).map(Agency::getAgencyName)...`) – Eran Sep 22 '16 at 05:23