过滤

ArrayUtil.filter方法用于编辑已有数组元素,只针对泛型数组操作,原始类型数组并未提供。 方法中Editor接口用于返回每个元素编辑后的值,返回null此元素将被抛弃。

例如:过滤数组,只保留偶数

Integer[] a = {1,2,3,4,5,6};
Integer[] filter = ArrayUtil.filter(a, new Editor<Integer>(){
@Override
public Integer edit(Integer t) {
return (t % 2 == 0) ? t : null
}});
Assert.assertArrayEquals(filter, new Integer[]{2,4,6});

//其中,ArrayUtil.filter() 传递了一个匿名内部类,在大括号里面进行方法的实现。

其中:

filter 方法

/**
* 过滤
*
* @param <T> 数组元素类型
* @param array 数组
* @param editor 编辑器接口
* @return 过滤后的数组
*/
public static <T> T[] filter(T[] array, Editor<T> editor) {
ArrayList<T> list = new ArrayList<T>();
T modified;
for (T t : array) {
modified = editor.edit(t);
if (null != modified) {
list.add(t);
}
}
return list.toArray(Arrays.copyOf(array, list.size()));
}
Editor 接口:
/**
* 编辑器接口,常用于对于集合中的元素做统一编辑<br>
* 此编辑器两个作用:
*
* <pre>
* 1、如果返回值为<code>null</code>,表示此值被抛弃
* 2、对对象做修改
* </pre>
*
* @param <T> 被编辑对象类型
* @author Looly
*/
public interface Editor<T> {
/**
* 修改过滤后的结果
*
* @param t 被过滤的对象
* @return 修改后的对象,如果被过滤返回<code>null</code>
*/
public T edit(T t);
}

Java常用工具类之ArrayUtil的更多相关文章

  1. JavaEE-实验一 Java常用工具类编程

    该博客仅专为我的小伙伴提供参考而附加,没空加上代码具体解析,望各位谅解 1.  使用类String类的分割split 将字符串  “Solutions to selected exercises ca ...

  2. JAVA常用工具类汇总

    一.功能方法目录清单: 1.getString(String sSource)的功能是判断参数是否为空,为空返回"",否则返回其值: 2.getString(int iSource ...

  3. Java常用工具类---XML工具类、数据验证工具类

    package com.jarvis.base.util; import java.io.File;import java.io.FileWriter;import java.io.IOExcepti ...

  4. [转]Java常用工具类集合

    转自:http://blog.csdn.net/justdb/article/details/8653166 数据库连接工具类——仅仅获得连接对象 ConnDB.java package com.ut ...

  5. java常用工具类(java技术交流群57388149)

    package com.itjh.javaUtil;   import java.util.ArrayList; import java.util.List;   /** * * String工具类. ...

  6. 项目经验分享——Java常用工具类集合 转

    http://blog.csdn.net/xyw591238/article/details/51678525 写在前面     本文涉及的工具类部分是自己编写,另一部分是在项目里收集的.工具类涉及数 ...

  7. java 常用工具类

    1. org.apache.commons.collections4包提供CollectionUtils.MapUtils.ListUtils.SetUtils等工具类: 2. org.apache. ...

  8. Java常用工具类之Excel导出

    package com.wazn.learn.util; import java.util.List; import java.util.Map; import org.apache.poi.hssf ...

  9. Java常用工具类之删除文件

    package com.wazn.learn.util; import java.io.File; /** * 删除文件工具类 * @author yangzhenyu * */ public cla ...

随机推荐

  1. 【LOJ】#2888. 「APIO2015」巴邻旁之桥 Palembang Bridges

    题解 发现我们选择一座桥会选择力\(\frac{s + t}{2}\)较近的一座桥 然后我们只需要按照\(s + t\)排序,然后枚举断点,左边取所有s和t的中位数,右边同理 动态求中位数用平衡树维护 ...

  2. 【LeetCode】287. Find the Duplicate Number

    Difficulty:medium  More:[目录]LeetCode Java实现 Description Given an array nums containing n + 1 integer ...

  3. Wireshark、Netcat

    Wireshark Wireshark是一个网络数据包分析软件,功能是截取网络数据包,并尽可能显示出最为详细的网络数据包数据.为了安全考虑,wireshark只能查看封包,而不能修改封包的内容,或者发 ...

  4. s3fs挂s3作为本地盘制作ftp使用

    一. 安装s3fs 安装s3fs-fuserhttps://github.com/s3fs-fuse/s3fs-fuse 二. 安装vsftpd #查看当前系统版本cat /etc/redhat-re ...

  5. 循序渐进学.Net Core Web Api开发系列【3】:WebApi开发概览

    系列目录 循序渐进学.Net Core Web Api开发系列目录 本系列涉及到的源码下载地址:https://github.com/seabluescn/Blog_WebApi 一.概述 目前我们已 ...

  6. android listview 优化

    韩梦飞沙  韩亚飞  313134555@qq.com  yue31313  han_meng_fei_sha android convertview作用 ======= 1,convertView复 ...

  7. JVM启动流程

    JVM启动流程 (1)在java中jvm是通过java或javaw命令启动的,后面跟加载的类名. (2)jvm在启动的时候先根据[当前路径和系统版本寻找jvm的配置文件jvm.cfg]装载配置. (3 ...

  8. Codeforces Round #394 (Div. 2) C. Dasha and Password 暴力

    C. Dasha and Password 题目连接: http://codeforces.com/contest/761/problem/C Description After overcoming ...

  9. JavaScript学习笔记[0]

    JavaScript学习笔记[0] 使用的是廖雪峰JavaScript教程. 数据类型 Number 表示数字,不区分浮点整形. === 比较时不转化数据类型. == 反之. NaN与任何值都不想等, ...

  10. word如何插入和删除脚注,尾注

    在我们日常使用word时,经常会使用到脚注/尾注来对文中某些文字或内容进行注释或是重点说明,效果挺好,挺直观和明显.但是很多人并不会使用,特别是当需要删除脚注事,就特别苦恼了,那根小横线怎么也删除不掉 ...