1 NoKey 2019-05-29 10:30:17 +08:00 ![]() list != null list.size != 0 |
![]() | 2 allanzhuo 2019-05-29 10:50:31 +08:00 Optional |
![]() | 3 FringJX 2019-05-29 11:13:13 +08:00 java 有集合工具类可以判断 |
![]() | 4 l8g 2019-05-29 11:15:19 +08:00 ![]() CollectionUtils.isNotEmpty |
5 zhangqilin 2019-05-29 11:18:12 +08:00 python? if xxx: # 不为空 |
6 oblivious 2019-05-29 11:18:20 +08:00 via iPhone if some_list: Do something else: Do something else |
![]() | 9 demo 2019-05-29 11:41:24 +08:00 list || [] |
![]() | 10 aijam 2019-05-29 11:50:52 +08:00 ![]() Effective Java, Item54: Return empty collections or arrays, not nulls |
11 securityCoding 2019-05-29 11:53:17 +08:00 楼上+1 |
![]() | 12 yalin 2019-05-29 11:55:36 +08:00 !CollectionUtils.isEmpty(list) |
![]() | 13 chendy 2019-05-29 12:08:31 +08:00 能用 `Collecitons.emplyList()` 的地方就用,使用方一个 `.isEmpty()` 就行了 |
![]() | 14 zrc 2019-05-29 12:43:06 +08:00 ![]() list = Optional.ofNull(list).orElse(Collecitons.emplyList()) |
15 jzmws 2019-05-29 14:23:45 +08:00 Optional 大法好 |
![]() | 16 mapper 2019-05-29 14:24:47 +08:00 目前用的最多的是 CollectionUtils.isNotEmpty(list), 该方法就等同于 list != null && list.size() > 0 |
17 SuperMild 2019-05-29 14:29:38 +08:00 老代码没办法,新代码上 Optional |
![]() | 18 chengxy 2019-05-29 15:42:47 +08:00 !!list.length |
19 jorneyr 2019-05-29 15:47:04 +08:00 我们是强制规定接口返回数据的时候,没有数据时必须返回空集合,不能返回 null,这样就不需要处理集合的时候进行 null 判断了,直接遍历。 MyBatis 的 Mapper 返回集合类型也是没有查询到数据时返回空集合。 |
![]() | 20 wysnylc 2019-05-29 15:50:12 +08:00 CollectionUtils |
![]() | 21 shakaraka PRO list.length <= 0 ? false : true |
![]() | 24 Takamine 2019-05-29 16:53:18 +08:00 if list : empty(list) list || [] CollectionUtils 等等。 |
25 dabaitu 2019-05-29 16:54:47 +08:00 via Android kotlin list.let |
26 Em5O7B1JGfjQnBry 2019-05-29 17:05:05 +08:00 via Android Rust、Haskell:没有 null,不能每次都判断 null,真羡慕你们这些成功的语言。 |
27 fengjianxinghun 2019-05-29 17:06:01 +08:00 Rust 没有 null,不需要判断 |
28 siteshen 2019-05-29 17:08:07 +08:00 这个问题有歧义太大,导致答案五花八门,建议楼主补充一下问题的细节: 1. 用什么编程语言? 2. 谁要避免空 list ?是 producer 避免接收空 list 还是 consumer 避免产生空 list ? 3. 什么是“空”?元素个数为零,还是空指针,还是两者都是“空”? |
29 chinjanry 2019-05-29 17:11:58 +08:00 List<Xxx> outputList = MoreObjects.firstNonNull(inputList, Lists.newArrayList()); |
30 wr410 2019-05-29 17:13:03 +08:00 为什么你们都可以随便使用第三方包? 我们通常只给用 jdk 和 org.apache 的包。 |
![]() | 32 8a9a09dw12 2019-05-29 17:51:03 +08:00 assert 了解一下 |
![]() | 33 Jrue0011 2019-05-29 17:56:52 +08:00 via iPhone @wr410 也没别的包吧,CollectionUtils 是 apache commons 的,Optional 和 Collections 是 jdk 的,只有 MoreObjects 好像是 guava 的… |
![]() | 35 linvaux 2019-05-29 18:40:36 +08:00 via Android 不用 list 不就好了,咳咳,认真点,用 guava 的 optional |
36 syasuker 2019-05-29 19:04:13 +08:00 Utils.isListEmpty |
37 lihongjie0209 2019-05-29 20:00:37 +08:00 集合就不应该是 null, 只应该是 empty |
![]() | 38 wombat 2019-05-29 20:53:59 +08:00 4 楼 |
![]() | 39 Aresxue 2019-05-30 09:36:29 +08:00 申明的时候赋值一个空列表, Collections.EMPTY_LIST 这个就是专用的空列表不会占用内存。 |
40 troywinter 2019-05-30 23:07:24 +08:00 尽信书不如无书,虽然 effective java 建议返回 emptyList(),但是还是要分清场景,如果你之后不需要对这个 list 继续 modify,那么使用 emptyList 没有问题,但如果你后续的业务逻辑需要进一步对这个 list 做 modify,那么就会出现 exception,因为 emptyList 返回了 List<T>类型,实际是 immutable 的,就会出现 exception。所以遇到这种问题还是要多看源码结合自己的实际需求做取舍,no silver bullet. |