search

Home  >  Q&A  >  body text

Java使用List类的数据时,像判断某一个位置(N),是否为null值,更高效的写法.

PHPzPHPz2898 days ago524

reply all(4)I'll reply

  • PHP中文网

    PHP中文网2017-04-18 10:38:17

    Using Java8 only requires one line of code

    List<Integer> list = new ArrayList<>();
    if (list == null || list.isEmpty()){
        return Collections.emptyList();
    }
    return list.stream().filter(Objects::nonNull).limit(2).collect(Collectors.toList());

    reply
    0
  • PHPz

    PHPz2017-04-18 10:38:17

    ArrayListget(index)Don’t worry about the efficiency of the method. Your problem description and title seem unrelated. Can you understand it?

    reply
    0
  • ringa_lee

    ringa_lee2017-04-18 10:38:17

    When the number of data items is determined, the backend only needs to return two items, and then it can be traversed directly. There is no need to make it so complicated...

    reply
    0
  • PHP中文网

    PHP中文网2017-04-18 10:38:17

    What does it mean that your question is difficult to understand? ! !
    Do you mean something like “display in pages”? And dynamically return data based on the number of lists?
    If this is the case, then you can use ArrayList and use the sort method of ArrayLiast to sort. After sorting, you can pick whichever item you want without any confusion.

    reply
    0
  • Cancelreply