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.但是 ...
随机推荐
- 四、robotframework生成几种随机数
1.random()生成0<=n<1之间的随机实数--它会生成一个随机的浮点数,范围是在0.0~1.0之间.: ${num} evaluate random.random() ra ...
- fatal: repository 'xxxx' not found
环境:centOS7 背景:公司代码仓库迁移,因而配置的jenkins自动打包git地址也要跟着变化. 问题描述:git clone http xxxx.git后报错: fatal: reposit ...
- write()与writelines()
f = open('user','a+') f.write('abcde') #write只能写字符串 f.writelines(['444','rrrr','uuu']) #writeline ...
- python接口自动化:响应内容中json字符串对象的处理
实现代码如下: import json #定义一个字典.一个列表.两个字符串({}中必须是双引号) di1={"} di2=[{"}] di3='{"name" ...
- [SDOI2016]征途 —— 斜率优化DP
时隔多年没有碰斜率优化了... 想当年被斜率优化虐的死去活来,现在看看...也就那样吧. Pine开始了从S地到T地的征途. 从S地到T地的路可以划分成n段,相邻两段路的分界点设有休息站. Pine计 ...
- python+selenium的WebElement对象操作
webelement对象操作 webelement对象是selenium中所有元素的父类,也就是webelement对象拥有的方法,其它元素对象都会有: 只是不同的对象在调用特定方法时,效果是不一样的 ...
- Vue—非父子组件间的传值(Bus/发布订阅模式/观察者模式/总线)
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- Python学习-第三天-面向对象编程基础
Python学习-第三天-面向对象编程基础 类和对象 简单的说,类是对象的蓝图和模板,而对象是类的实例.这个解释虽然有点像用概念在解释概念,但是从这句话我们至少可以看出,类是抽象的概念,而对象是具体的 ...
- [AtCoder ARC076] F Exhausted?
霍尔定理 + 线段树? 咱学学霍尔定理... 霍尔定理和二分图完美匹配有关,具体而言,就是定义了二分图存在完美匹配的充要条件: 不妨设当前二分图左端集合为 X ,右端集合为 Y ,X 与 Y 之间的边 ...
- python 模块导入import和import from区别
模块就是一个.py文件,在名字空间下导入模块导入import和import from,那么python 模块导入import和import from区别是什么呢 1,import 导入模块 impor ...