CollectionUtils工具类中常用方法
@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工具类中常用方法的更多相关文章
- StringUtils、CollectionUtils工具类的常用方法
唯能极于情,故能极于剑 欢迎来到 “程序牛CodeCow” 的博客,有问题请及时关注小编公众号 “CodeCow”,大家一起学习交流 下面将为大家演示StringUtils.CollectionUti ...
- CollectionUtils工具类的常用方法
集合判断: 例1: 判断集合是否为空: CollectionUtils.isEmpty(null): true CollectionUtils.isEmpty(new ArrayList()): t ...
- 通过CollectionUtils工具类判断集合是否为空,通过StringUtils工具类判断字符串是否为空
通过CollectionUtils工具类判断集合是否为空 先引入CollectionUtils工具类: import org.apache.commons.collections4.Collectio ...
- java代码之美(12)---CollectionUtils工具类
java代码之美(12)---CollectionUtils工具类 这篇讲的CollectionUtils工具类是在apache下的, 而不是springframework下的CollectionUt ...
- CollectionUtils工具类
CollectionUtils工具类 这篇讲的CollectionUtils工具类是在apache下的,可以使代码更加简洁和安全. 使用前需导入依赖 <dependency> <gr ...
- java代码(12) ---CollectionUtils工具类
CollectionUtils工具类 CollectionUtils工具类是在apache下的,而不是springframework下的CollectionUtils 个人觉得在真实项目中Collec ...
- 基于StringUtils工具类的常用方法介绍(必看篇)
前言:工作中看到项目组里的大牛写代码大量的用到了StringUtils工具类来做字符串的操作,便学习整理了一下,方便查阅. isEmpty(String str) 是否为空,空格字符为false is ...
- HttpTool.java(在java tool util工具类中已存在) 暂保留
HttpTool.java 该类为java源生态的http 请求工具,不依赖第三方jar包 ,即插即用. package kingtool; import java.io.BufferedReader ...
- 静态工具类中使用注解注入service
转载:http://blog.csdn.net/p793049488/article/details/37819121 一般需要在一个工具类中使用@Autowired 注解注入一个service.但是 ...
随机推荐
- VMware 虚拟化编程(2) — 虚拟磁盘文件类型详解
目录 目录 前文列表 虚拟磁盘文件 VMDK 用户可以创建的虚拟磁盘类型 VixDiskLib 中支持的虚拟磁盘类型 虚拟机文件类型 前文列表 VMware 虚拟化编程(1) - VMDK/VDDK/ ...
- Pandas 50题练习
f行的age改为1. df.loc['f', 'age'] = 1.5 这样比 df.loc['f']['age'] 好 计算df中每个种类animal的数量 df['animal'].value_c ...
- Visual Studio新增类模板修改
C:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\IDE\ItemTemplates\CSharp\Code\2052\Class ...
- 使用K近邻算法改进约会网站的配对效果
1 定义数据集导入函数 import numpy as np """ 函数说明:打开并解析文件,对数据进行分类:1 代表不喜欢,2 代表魅力一般,3 代表极具魅力 Par ...
- 应用安全-CTF-格式串漏洞
主要影响c库中print家族函数 - > printf,sprintf,fprintf等 利用: SIP请求URI中格式串
- 安全运维 - Linux系统攻击应急响应
Linux 应急相应 - 总纲 应急准备: 制定应急策略 组建应急团队 其他应急资源 安全事件处理: 痕迹数据获取 分析.锁定攻击源删除可疑账号关闭异常进程.端口禁用相应异常开机启动项删除异常定时任务 ...
- 浅谈Vue中的$set的使用
在我们使用vue进行开发的过程中,可能会遇到一种情况:当生成vue实例后,当再次给数据赋值时,有时候并不会自动更新到视图上去: 当我们去看vue文档的时候,会发现有这么一句话:如果在实例创建之后添加新 ...
- Mac016--安装kubernetes(k8s)
一.安装kubernetes(k8s) 参考: http://batizhao.github.io/2018/01/18/Running-Kubernetes-Locally-via-Minikube ...
- 如何理解ajax的同步和异步?
对于如下一段代码: var dataJson = {"ABC":'testABC'}; $.ajax({ url: "/MonkeyServ ...
- linux文件io与标准io
文件IO实际是API,Linux对文件操作主要流程为:打开(open),操作(write.read.lseek),关闭(close). 1.打开文件函数open(): 涉及的头文件: #includ ...