Common-collections

来源:互联网 发布:怎么制作京东秒杀软件 编辑:程序博客网 时间:2024/06/15 17:37
package cn.toltech.cn.toltech.comm.collections;import cn.toltech.cn.toltech.comm.beanutils.po.T_DynaBean;import org.apache.commons.beanutils.BeanPropertyValueChangeClosure;import org.apache.commons.collections.*;import org.apache.commons.collections.bag.HashBag;import org.apache.commons.collections.bidimap.TreeBidiMap;import org.apache.commons.collections.map.HashedMap;import org.apache.commons.collections.map.LinkedMap;import java.util.ArrayList;import java.util.List;/** * Created by sz0816 on 14-10-17. */public class TestCollections {    public static void main(String []args){        TestCollections testCollections = new TestCollections();//        testCollections.testIterableMap();//        testCollections.testOrderedMap();//        testCollections.testBidiMap();//        testCollections.testBag();        testCollections.testCollectionUtilsAllDo();    }    /***     * 使用IterableMap可以进行迭代Map集合     */    public void testIterableMap(){        IterableMap map = new HashedMap();        map.put("1","小叶子");        map.put("2","小名");        MapIterator mapIterator = map.mapIterator();        while (mapIterator.hasNext()){            System.out.println(mapIterator.next());            System.out.println(mapIterator.getKey()+"=="+mapIterator.getValue());        }    }    /***     * 进行排好顺序的Map集合     */    public void testOrderedMap(){        OrderedMap map = new LinkedMap();        map.put("1","小叶子");        map.put("2","小伙子");        map.put("3","小凳子");        System.out.println(map.nextKey("2")); //获取key为2的上一个key        System.out.println(map.previousKey("2")); //获取key为2的下一个key    }    /****     * 双向链表,可以通过key来获取value,也可以通过value来获取key     * 可以将key和value进行交换     */    public void testBidiMap(){        BidiMap bidi = new TreeBidiMap();        bidi.put("SIX", "6");        bidi.get("SIX");  // returns "6"        bidi.getKey("6");  // returns "SIX"//        bidi.removeValue("6");  // removes the mapping//        bidi.remove("SEX"); //删除key为SEX对应的value//        System.out.println(bidi.getKey("6"));//        System.out.println(bidi.get("SEX"));        BidiMap inverse = bidi.inverseBidiMap();  // returns a map with keys and values swapped        System.out.println(inverse.get("6"));    }    /**     * 可以进行包内的数据进行相加     */    public void testBag(){        Bag bag = new HashBag();        bag.add("yezi",1);        bag.add("yezi",3);        System.out.println(bag.getCount("yezi"));        System.out.println(bag.remove("yezi",12));        System.out.println(bag.getCount("yezi"));    }    /***     * 配合Common-BeanUtils使用     */    public void testCollectionUtilsAllDo(){        BeanPropertyValueChangeClosure closure = new BeanPropertyValueChangeClosure("name","111111");        List<T_DynaBean> list = new ArrayList<T_DynaBean>();        T_DynaBean bean = null;        for(int i=0;i<10;i++){            bean = new T_DynaBean();            bean.setName("-"+i);            bean.setId(i);            list.add(bean);        }        CollectionUtils.forAllDo(list, closure);        for(int i=0;i<list.size();i++){            bean = list.get(i);            System.out.println(bean.getName());        }    }}

0 0
原创粉丝点击