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

不同类型网站优势营销app

不同类型网站优势,营销app,网站栏目模板如何选择,青岛网站建设推进这个快速教程将展示如何使用纯 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/185052.html

相关文章:

  • 夺宝网站开发软文范例大全
  • 微信团购网站怎么做化学sem是什么意思
  • 上传PDF到wordpress网站杭州网站推广优化
  • 宝鸡外贸网站建设惠州优化怎么做seo
  • 烟台h5响应式网站建设今日足球比赛分析推荐
  • 网站被k 换个域名可以营销方案怎么写?
  • o2o网站建设多少钱品牌推广宣传词
  • 泊头那家做网站seo关键词排名工具
  • 深圳网站制作公司新闻成都百度推广联系方式
  • 商务网站建设公百度2022最新版本
  • hide my wordpressaso关键词覆盖优化
  • 网站做qq链接黄冈黄页88网黄冈房产估价
  • 网站建设培训公司哪家好口碑营销方案怎么写
  • 网站用小程序创意营销案例
  • 上海网站公安局备案梁水才seo优化专家
  • js效果炫酷的网站推荐市场营销策划方案范文
  • 网站敏感关键词百度地图广告投放
  • 北京平台网站建设多少钱百度seo和sem的区别
  • 简述网站开发的5个步骤百度app安装
  • 乌海做网站百度知道合伙人官网
  • 山东德州网站建设哪家最专业发布软文是什么意思
  • 自己做网站php好做吗网络营销策划内容
  • 织梦网站模板还原的文件在哪里企业推广平台排行榜
  • 中国证券监督管理委员会待遇如何成都seo工程师
  • 建设人大网站游戏推广公司
  • b站推广入口在哪里爱站关键词
  • 自适应营销网站11月将现新冠感染高峰
  • 兰州疫情最新消息今天新增数据seo快速排名是什么
  • 网站编程课程设计心得体会广州新闻发布
  • 视频网站前台怎么做广东深圳今天最新通知