apache工具類之CollectionUtils(待續更新)

來源:互聯網
上載者:User
1. CollectionUtils 並 交 補
    @Test    public void test3() {        List<Integer> h = new ArrayList<Integer>();        h.add(1);        h.add(2);        h.add(3);        h.add(6);        ;        List<Integer> i = new ArrayList<Integer>();        i.add(3);        i.add(3);        i.add(4);        i.add(5);        // 並集        System.out.println(CollectionUtils.union(i, h)); // [1, 2, 3, 3, 4, 5]        // 交集        System.out.println(CollectionUtils.intersection(i, h)); // [3]        // 交集的補集        System.out.println(CollectionUtils.disjunction(i, h)); // [1, 2, 3, 4, 5, 6]        // h減i的差        System.out.println(CollectionUtils.subtract(h, i)); // [1, 2, 6]        System.out.println(CollectionUtils.subtract(i, h)); // [3, 4, 5]        // 兩個集合間的操作        List<Integer> e = new ArrayList<Integer>();        e.add(2);        e.add(1);        List<Integer> f = new ArrayList<Integer>();        f.add(1);        f.add(2);        List<Integer> g = new ArrayList<Integer>();        g.add(12);        // 比較兩集合值        System.out.println(CollectionUtils.isEqualCollection(e, f)); // true        System.out.println(CollectionUtils.isEqualCollection(f, g)); // false    }
2. CollectionUtils.collect

案例:從一個對象的集合中,擷取某一屬性的集合。如,從人員資訊集合中擷取人員姓名的集合

import java.util.ArrayList;import java.util.HashSet;import java.util.List;import java.util.Set;import org.apache.commons.collections.CollectionUtils;import org.apache.commons.collections.Predicate;import org.apache.commons.collections.Transformer;import org.apache.commons.lang3.StringUtils;import org.junit.Test;public class CollectionTest {    @Test    public void test1(){        List<Student> stuList = new ArrayList<Student>();        stuList.add(new Student("wang","18"));        stuList.add(new Student("li","19"));        stuList.add(new Student("li","20"));        @SuppressWarnings("unchecked")        List<String> nameList = (List<String>) CollectionUtils.collect(                stuList, new Transformer() {                    public Object transform(Object arg0) {                        Student u = (Student) arg0;                        return u.getName();                    }                });        Set<String> nameSet = new HashSet<String>(nameList);        System.out.println(nameSet);                        //[wang, li]        System.out.println(StringUtils.join(nameSet.iterator(),","));   //wang,li    }}class Student{    String name;    String age;    public Student(){}    public Student(String name,String age){        this.name = name;        this.age = age;    }    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    public String getAge() {        return age;    }    public void setAge(String age) {        this.age = age;    }    @Override    public String toString() {        return "Student [name=" + name + ", age=" + age + "]";    }}
3. CollectionUtils.filter

案例:從一個對象集合中過濾出只符合要求的對象。如只保留叫 li 的人員資訊

    @Test    public void test2(){        List<Student> stuList = new ArrayList<Student>();        stuList.add(new Student("wang","18"));        stuList.add(new Student("li","19"));        stuList.add(new Student("li","20"));        CollectionUtils.filter(stuList, new Predicate(){            public boolean evaluate(Object arg0) {                Student v = (Student)arg0;                return StringUtils.isNotBlank(v.getName()) && "li".equals(v.getName());            }        });        System.out.println(stuList); //[Student [name=li, age=19], Student [name=li, age=20]]    }

**注意:這個Predicate類必須實現evaluate(Object obj) 方法,這個方法的傳回值只要是 true,說明這個對象是要保留下來的。如這個裡面的是取 姓名叫 li 的人,如果是其它特殊的、變動的名稱。可以另外新建立一個 實現 Predicate的類。 4 CollectionUtils.select

/** * 集合篩選 */@Testpublic void test11() {    List<Student> stuList = new ArrayList<Student>();    stuList.add(new Student("wang", "18"));    stuList.add(new Student("li", "19"));    stuList.add(new Student("li", "20"));    @SuppressWarnings("unchecked")    List<Student> wang = (List<Student>) CollectionUtils.select(stuList, new Predicate() {        @Override        public boolean evaluate(Object object) {            if (Student.class.isAssignableFrom(object.getClass())) {                Student param = (Student) object;                if (StringUtils.isNotBlank(param.getName()) && "wang".equalsIgnoreCase(param.getName())) {                    return true;                }            }            return false;        }    });    @SuppressWarnings("unchecked")    List<Student> li = (List<Student>) CollectionUtils.select(stuList, new Predicate() {        @Override        public boolean evaluate(Object object) {            if (Student.class.isAssignableFrom(object.getClass())) {                Student param = (Student) object;                if (StringUtils.isNotBlank(param.getName()) && "li".equalsIgnoreCase(param.getName())) {                    return true;                }            }            return false;        }    });    System.out.println(StringUtils.join(wang)); // [Student [name=wang, age=18, birthdaynull]]    System.out.println(StringUtils.join(li));  // [Student [name=li, age=19, birthdaynull], Student [name=li, age=20, birthdaynull]]}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.