Any null safe alternative to ArrayList.addAll?

后端 未结 4 1676
旧时难觅i
旧时难觅i 2021-02-02 09:35

I was refactoring some old code of mine that I\'ve written and I stumbeled on this code:

    List fullImagePool = new ArrayList<>();
           


        
4条回答
  •  礼貌的吻别
    2021-02-02 09:46

    This refactors cleanly to

    for (OcmImageData elem : new List[] { style.getTestMH(), style.getTrousers() /* etc */}) {
        if (CollectionUtils.isNotEmpty(elem)) {
            fullImagePull.addAll(elem);
        }
    }
    

    To answer your original question, no, you will have to do your own null check. You can see Guava's methods will throw an NPE, and Apache's methods explicitly require the input to be not null.

提交回复
热议问题