@SuppressWarnings("rawtypes")
@Test
public void test1() {
  List<String> coll = new ArrayList<String>();
  coll.add("A");
  coll.add("B");
  coll.add("C");   List<String> coll1 = new ArrayList<String>();
  coll1.add("AA");
  coll1.add("BB");
  coll1.add("C");   //合并coll, coll1两个集合元素(相当于求两集合的并集)
  Collection union = CollectionUtils.union(coll, coll1);
  System.out.println("union="+union); //union=[AA, BB, A, B, C]   //去除coll集合中,在coll1集合中也有的元素,注意coll1 != null
  Collection subtract = CollectionUtils.subtract(coll, coll1);
  System.out.println("subtract="+subtract); //subtract=[A, B]   //合并coll,coll1集合,但要排除两集合共有的元素
  Collection disjunction = CollectionUtils.disjunction(coll, coll1);
  System.out.println("disjunction="+disjunction); //disjunction=[AA, BB, A, B]   //返回coll集合与coll1集合相同的元素(即是求两集合的交集)
  Collection intersection = CollectionUtils.intersection(coll, coll1);
  System.out.println("intersection="+intersection); //intersection=[C]   //coll, coll1只要有一个相同的元素,就返回true
  boolean containsAny = CollectionUtils.containsAny(coll, coll1);
  System.out.println("containsAny="+containsAny); //containsAny=true
} @SuppressWarnings("rawtypes") @Test
public void test2() {
  List<User> list = new ArrayList<User>();
  User u1 = new User("zqf1", "123");
  User u2 = new User("zqf2", "124");
  User u3 = new User("zqf3", "125");
  User u4 = new User("zqf4", "123");
  list.add(u1);
  list.add(u2);
  list.add(u3);
  list.add(u4);   Collection resultList_select = CollectionUtils.select(list, new Predicate() {
    @Override
    public boolean evaluate(Object object) {
      User u = (User) object;
      // 获取用户密码为123的User
      if (u.getPasswd().toString().equals("123")) {
        return true;
      }
        return false;
    }
  });   //打印结果
  System.out.println("resultList_select=" + resultList_select); // resultList_select=[User [username=zqf1, passwd=123], User [username=zqf4, passwd=123]]   List<String> coll = new ArrayList<String>();
  coll.add("A");
  coll.add("B");
  coll.add("C");   // 将list中passwd = 124的user对外过滤之后,其它的user对象添加到的coll集合中,返回结果是coll集合
  CollectionUtils.selectRejected(list, new Predicate() {
    @Override
    public boolean evaluate(Object object) {
      User u = (User) object;
      if (u.getPasswd().toString().equals("124")) {
        return true;
      }
      return false;
    }
  },coll);
  System.out.println("coll="+coll);
  //[A, B, C, User [username=zqf1, passwd=123], User [username=zqf3, passwd=125], User [username=zqf4, passwd=123]]   //匹配密码等于123的User个数
  int countMatches = CollectionUtils.countMatches(list, new Predicate() {
    @Override
    public boolean evaluate(Object object) {
      User u = (User) object;
      if (u.getPasswd().toString().equals("123")) {
        return true;
      }
      return false;
    }
  });
  System.out.println("countMatches="+countMatches);//countMatches=2   //匹配密码等于123的User个数
  CollectionUtils.filter(list, new Predicate() {
    @Override
    public boolean evaluate(Object object) {
    User u = (User) object;
    if (u.getPasswd().toString().equals("123")) {
      return true;
    }
    return false;
  }
  });
  System.out.println("filter="+list);//filter=[User [username=zqf1, passwd=123], User [username=zqf4, passwd=123]]
/**
* select:此方法是创建了一个新的集合,将满足条件的数据添加到这个新集合中去
* filter:此方法是在原本集合基础上,将不满足条件的数据remove,没有创建新集合
*/   // 转换
  CollectionUtils.transform(list, new Transformer() {
    @Override
    public Object transform(Object object) {
      User u = (User) object; //如果用户的姓名等于zqf1,那么就把它的密码改成AAAA
      if (u.getUsername().toString().equals("zqf1")) {
        u.setPasswd("AAAA");
      }
        return u;
    }
  });
    System.out.println("transform="+list);
    //transform=[User [username=zqf1, passwd=AAAA], User [username=zqf2, passwd=124], User [username=zqf3, passwd=125], User [username=zqf4, passwd=123]]   } /** User实体类 */   public class User implements Serializable{
  private static final long serialVersionUID = 1L;
  private String username;
  private String passwd;   public String getUsername() {
    return username;
  }   public void setUsername(String username) {
    this.username = username;
  }   public String getPasswd() {
    return passwd;
  }   public void setPasswd(String passwd) {
    this.passwd = passwd;
  }   public User(String username, String passwd) {
    this.username = username;
    this.passwd = passwd;
  }   public User() {
    super();
    // TODO Auto-generated constructor stub
  }   @Override
  public String toString() {
    StringBuilder builder = new StringBuilder();
    builder.append("User [username=");
    builder.append(username);
    builder.append(", passwd=");
    builder.append(passwd);
    builder.append("]");
    return builder.toString();
  }
}

CollectionUtils工具类中常用方法的更多相关文章

  1. StringUtils、CollectionUtils工具类的常用方法

    唯能极于情,故能极于剑 欢迎来到 “程序牛CodeCow” 的博客,有问题请及时关注小编公众号 “CodeCow”,大家一起学习交流 下面将为大家演示StringUtils.CollectionUti ...

  2. CollectionUtils工具类的常用方法

    集合判断:  例1: 判断集合是否为空: CollectionUtils.isEmpty(null): true CollectionUtils.isEmpty(new ArrayList()): t ...

  3. 通过CollectionUtils工具类判断集合是否为空,通过StringUtils工具类判断字符串是否为空

    通过CollectionUtils工具类判断集合是否为空 先引入CollectionUtils工具类: import org.apache.commons.collections4.Collectio ...

  4. java代码之美(12)---CollectionUtils工具类

    java代码之美(12)---CollectionUtils工具类 这篇讲的CollectionUtils工具类是在apache下的, 而不是springframework下的CollectionUt ...

  5. CollectionUtils工具类

    CollectionUtils工具类 这篇讲的CollectionUtils工具类是在apache下的,可以使代码更加简洁和安全. 使用前需导入依赖 <dependency> <gr ...

  6. java代码(12) ---CollectionUtils工具类

    CollectionUtils工具类 CollectionUtils工具类是在apache下的,而不是springframework下的CollectionUtils 个人觉得在真实项目中Collec ...

  7. 基于StringUtils工具类的常用方法介绍(必看篇)

    前言:工作中看到项目组里的大牛写代码大量的用到了StringUtils工具类来做字符串的操作,便学习整理了一下,方便查阅. isEmpty(String str) 是否为空,空格字符为false is ...

  8. HttpTool.java(在java tool util工具类中已存在) 暂保留

    HttpTool.java 该类为java源生态的http 请求工具,不依赖第三方jar包 ,即插即用. package kingtool; import java.io.BufferedReader ...

  9. 静态工具类中使用注解注入service

    转载:http://blog.csdn.net/p793049488/article/details/37819121 一般需要在一个工具类中使用@Autowired 注解注入一个service.但是 ...

随机推荐

  1. django中自定义404错误页面

    自定义404页面,如下5个步骤:1)使用自定义的404页面,必须在setting文件修改DEBUG = False(即关闭debug调试模式)2)必须在setting文件修改ALLOWED_HOSTS ...

  2. 测开之路九十四:css之盒子模型

    盒子模型 为了演示方便,把内容放到盒子里面 引用css 演示内容 外边距: 4个方向分开写 简写为一条指令,顺序为上右下左 简写为一条指令,第一个值为上下,第二个值为左右 简写为一条指令,只有一个值时 ...

  3. jmeter之自动重定向和跟随重定向用法

    jmeter工具里面有自动重定向和跟随重定向这2种选择,那么他们到底有啥区别呢? 目录 1.自动重定向和跟随重定向 2.举个例子 1.自动重定向和跟随重定向 01.3XX的请求一般要使用跟随重定向,2 ...

  4. linux(centos6.5)常用命令

    前言:由于项目项目使用的是linux服务器,因此会使用到较多linux命令,本文对centos下常用命令进行记录 1.vi的三种模式 2.解压缩相关 3.用户相关 4.文件相关 5.各种查看命令 1. ...

  5. 4 cdh 5.12 centos 6.10三节点安装

    4 cdh 5.12  centos 6.10 三节点安装 [root@hadoop1 opt]# cat /etc/redhat-release CentOS release 6.10 (Final ...

  6. 【GTS】关于GtsTetheringTestCases模块的几个失败项

    GTS---关于GtsTetheringTestCases模块的几个失败项 1.run gts -m GtsTetheringTestCases -t com.google.android.tethe ...

  7. 【ABAP系列】SAP ABAP诠释BDC的OK CODE含义

    公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[ABAP系列]SAP ABAP诠释BDC的OK ...

  8. js数组,运算符

  9. Flask使用原生sql语句

    安装pip install flask-sqlalchemy 创建数据库对象 # 设置数据库连接地址app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql://r ...

  10. Spring MVC-学习笔记(5)spring MVC的文件上传、下载、拦截器

    1.文件上传.      spring MVC为文件上传提供了直接的支持,这种支持是即插即用的MultipartResolver(多部分解析器)实现的.spring MVC使用Apache Commo ...