当前位置: 首页 > wzjs >正文

不同类型网站优势比较好的免费网站

不同类型网站优势,比较好的免费网站,美国做3d+h动画的网站,简单代码大全这个快速教程将展示如何使用纯 Java、Guava、Apache Commons Collections 以及较新的 Java 8 Lambda 支持来移除 List 中的所有空元素。 1. 使用纯 Java 从列表中移除空值 Java 集合框架提供了一个简单的解决方案来移除 List 中的所有空元素——一个基本的 while 循环&#xff…

这个快速教程将展示如何使用纯 Java、Guava、Apache Commons Collections 以及较新的 Java 8 Lambda 支持来移除 List 中的所有空元素。


1. 使用纯 Java 从列表中移除空值


Java 集合框架提供了一个简单的解决方案来移除 List 中的所有空元素——一个基本的 while 循环:
@Test
public void givenListContainsNulls_whenRemovingNullsWithPlainJava_thenCorrect() {List<Integer> list = Lists.newArrayList(null, 1, null);while (list.remove(null));assertThat(list, hasSize(1));
}


此外,我们还可以使用以下简单方法:

@Test
public void givenListContainsNulls_whenRemovingNullsWithPlainJavaAlternative_thenCorrect() {List<Integer> list = Lists.newArrayList(null, 1, null);list.removeAll(Collections.singleton(null));assertThat(list, hasSize(1));
}


请注意,这两种解决方案都会修改源列表。

2. 使用 Google Guava 从列表中移除空值

我们也可以使用 Guava 和更函数式的做法,通过谓词来移除空值:

@Test
public void givenListContainsNulls_whenRemovingNullsWithGuavaV1_thenCorrect() {List<Integer> list = Lists.newArrayList(null, 1, null);Iterables.removeIf(list, Predicates.isNull());assertThat(list, hasSize(1));
}


如果不想修改源列表,Guava 将允许我们创建一个新的过滤列表:

@Test
public void givenListContainsNulls_whenRemovingNullsWithGuavaV2_thenCorrect() {List<Integer> list = Lists.newArrayList(null, 1, null, 2, 3);List<Integer> listWithoutNulls = Lists.newArrayList(Iterables.filter(list, Predicates.notNull()));assertThat(listWithoutNulls, hasSize(3));
}

3. 使用 Apache Commons Collections 从列表中移除空值

现在我们来看一个使用 Apache Commons Collections 库的简单解决方案,采用类似的函数式风格:

@Test
public void givenListContainsNulls_whenRemovingNullsWithCommonsCollections_thenCorrect() {List<Integer> list = Lists.newArrayList(null, 1, 2, null, 3, null);CollectionUtils.filter(list, PredicateUtils.notNullPredicate());assertThat(list, hasSize(3));
}

请注意,此解决方案也会修改原始列表。

4. 使用 Lambda 表达式从列表中移除空值(Java 8)


最后——让我们来看一个使用 Lambda 表达式过滤 List 的 Java 8 解决方案;这个过滤过程可以并行进行或串行进行:

@Test
public void givenListContainsNulls_whenFilteringParallel_thenCorrect() {List<Integer> list = Lists.newArrayList(null, 1, 2, null, 3, null);List<Integer> listWithoutNulls = list.parallelStream().filter(Objects::nonNull).collect(Collectors.toList());
}@Test
public void givenListContainsNulls_whenFilteringSerial_thenCorrect() {List<Integer> list = Lists.newArrayList(null, 1, 2, null, 3, null);List<Integer> listWithoutNulls = list.stream().filter(Objects::nonNull).collect(Collectors.toList());
}public void givenListContainsNulls_whenRemovingNullsWithRemoveIf_thenCorrect() {List<Integer> listWithoutNulls = Lists.newArrayList(null, 1, 2, null, 3, null);listWithoutNulls.removeIf(Objects::isNull);assertThat(listWithoutNulls, hasSize(3));
}

就是这样——一些快速且非常实用的解决方案,用于清除 List 中的所有空值元素。

5. 结论


在本文中,我们探索了使用 Java、Guava 或 Lambda 表达式从 List 中移除空值的不同方法。
http://www.dtcms.com/wzjs/361129.html

相关文章:

  • 中国住房和建设部厅查询网站广西南宁市有公司网站设计
  • 微信视频网站怎么做的自己有域名怎么建网站
  • 做一家视频网站吗上海网络排名优化
  • 北京网站设计多少钱seo外贸推广
  • wordpress摘要显示图片网站seo优化皆宣徐州百都网络不错
  • dede茶叶网站模板秒收录关键词代发
  • 怎么做二维码进网站营销技巧美剧
  • 重庆营销网站建设公司友情链接翻译
  • 中山技术支持中山网站建设福州网站排名提升
  • 建设网站图三亚百度推广公司
  • 饮料企业哪个网站做的比较好做网站公司哪家正规
  • 网站后台内容更换怎么做seo在线短视频发布页运营
  • 推拿网站制作seo课程培训班费用
  • 北京网站建设 一流今日世界杯比分预测最新
  • 新手自己做网站关键词排名优化价格
  • 网站备案必须做前置审批吗网站建设方案
  • 教育行业网站制作资源优化网站排名
  • 做怎样的网站能赚钱百度点击器下载
  • 网站建站后维护需要做哪些郑州好的seo外包公司
  • 登陆国外的网站要这么做站长工具seo综合查询工具
  • 网页模板免费源码荆州seo推广
  • 重庆网站推广转化率seo求职
  • 做网站营销公司网络怎么做推广
  • wordpress怎么当站长seo优化报告
  • 安顺网站建设seo哪里可以学
  • 微博网站可以做兼职吗百度推广手机登录
  • 做写真网站合法吗宁波seo推广优化怎么做
  • 可以做h5的网站有哪些西安seo外包公司
  • 公众号推文模板短视频seo优化排名
  • 微商城网站建设网络服务费计入什么科目