@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(); } }
日拱一卒无有尽,功不唐捐终入海