Java中Collection扩展之commons

11
11

以下是部分代码:

package tk.zhangwei.commons;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.collections4.Predicate;
import org.apache.commons.collections4.PredicateUtils;
import org.apache.commons.collections4.functors.EqualPredicate;
import org.apache.commons.collections4.functors.NotNullPredicate;
import org.apache.commons.collections4.functors.UniquePredicate;
import org.apache.commons.collections4.iterators.UniqueFilterIterator;
import org.apache.commons.collections4.list.PredicatedList;

/***
 * 相等,非空判断
 * @author zw
 *
 */
public class Demo01 {

    public static void main(String[] args) {
        System.out.println("*************非空判断*************");
        Predicate notNull =  NotNullPredicate.notNullPredicate();
        String str = "zz";
        System.out.println(notNull.evaluate(str));//非空为true 


        System.out.println("*************相等判断*************");
        Predicate<String> pre = EqualPredicate.equalPredicate("zhangwei");
        boolean boo = pre.evaluate("zhangwei");
        System.out.println(boo);


        System.out.println("*************唯一性判断*************");
        Predicate<Long> uniquePre = UniquePredicate.uniquePredicate();
        List<Long> list = PredicatedList.predicatedList(new ArrayList<Long>(), uniquePre);
        list.add(100L);
        list.add(200L);
        //list.add(100L);//出现重复值   抛出异常

        System.out.println("*************自定义性判断*************");
        Predicate<String> self =new Predicate<String>() {
            //自定义判别式
            @Override
            public boolean evaluate(String obj) {

                return obj.length()>5&&obj.length()<20;
            }
        };
        Predicate notNull1 =  NotNullPredicate.notNullPredicate();
        Predicate all =PredicateUtils.allPredicate(self,notNull1);//谁在前面就先判断谁


        List list1 = PredicatedList.predicatedList(new ArrayList(), all);
        list1.add("zhangwei");
        //list1.add(null);
        //list1.add("zw");
    }

}
package tk.zhangwei.commons;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.function.Predicate;

import org.apache.commons.collections4.Closure;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.collections4.functors.IfClosure;
/***
 * 函数式编程,Closure 闭包 封装特定的业务
 * 
 * @author zw
 *
 */

public class Demo02 {
    public static void main(String[] args) {
        ifClosure();
    }




    /***
     * 2选1,如果是打折商品,打9折。如果不是打折商品,满100减去20
     */

    public static void ifClosure(){
        List<Goods> list1 = new ArrayList<Goods>();
        list1.add(new Goods("2017夏装新款韩版宽松原宿bf风半袖情侣款ulzzang男士短袖T恤潮流", 19.9, true));
        list1.add(new Goods("情侣装夏装2017新款韩版潮流个性创意简约上衣一男女短袖服装短轴", 145, false));
        list1.add(new Goods("2017夏装新款韩版修身纯棉CK36情侣装短袖T恤女男装短款打底衫潮", 336, false));
        //满百减20
        Closure<Goods> cre = new Closure<Goods>(){

            @Override
            public void execute(Goods goods) {
                if(goods.getPrice()>100){
                goods.setPrice(goods.getPrice()-20);
                }
                }};
        //打9折
            Closure<Goods> count = new Closure<Goods>(){

                @Override
                public void execute(Goods goods) {
                    if(goods.isDiscount()){
                    goods.setPrice(goods.getPrice()*0.9);
                    }
                    }};
            //判断
            Predicate<Goods> pre =new Predicate<Goods>() {

                @Override
                public boolean test(Goods t) {

                    return t.isDiscount();
                }
            };
            //二选一
        //  Closure<Goods> ifclo = IfClosure.ifClosure(pre, count, cre);
            //  CollectionUtils.forAllDo(list1, ifclo);
            //查看操作后的数据

            for(Goods temp:list1){
                System.out.println(temp);
            }
    }

    /***
     * 基本操作
     */


    public static void basic(){
        // 数据
        List<Employee> list = new ArrayList<Employee>();
        list.add(new Employee("张伟", 12000));
        list.add(new Employee("老王", 10000));

        // 业务功能
        Closure<Employee> cols = new Closure<Employee>() {

            @Override
            public void execute(Employee emp) {

                emp.setSalary(emp.getSalary() * 1.2);

            }
        };
        CollectionUtils.forAllDo(list, cols);
        //操作后的数据
        Iterator<Employee> teremp = list.iterator();
        while (teremp.hasNext()) {
            Employee emp = teremp.next();
            String name = emp.getName();
            double salay = emp.getSalary();
            System.out.println(name+"---"+salay);
        }
    }
}
package tk.zhangwei.commons;

import java.util.Collection;
import java.util.HashSet;
import java.util.Set;

import org.apache.commons.collections4.CollectionUtils;

public class Demo03 {

    public static void main(String[] args) {
        Set<Integer> set = new HashSet<Integer>();
        set.add(1);
        set.add(2);
        set.add(3);


        Set<Integer> set1 = new HashSet<Integer>();
        set1.add(2);
        set1.add(3);
        set1.add(5);
        //并集
        System.out.println("*************并集**************");
        Collection<Integer> coll = CollectionUtils.union(set, set1);
        for(Integer integer:coll){
            System.out.println(integer);
        }
        //交集
        System.out.println("*************交集**************");
        coll=CollectionUtils.intersection(set, set1);
        for(Integer integer:coll){
            System.out.println(integer);
        }
        //差集
        System.out.println("*************差集**************");
        coll=CollectionUtils.subtract(set,set1);
        for(Integer integer:coll){
            System.out.println(integer);
        }

    }

}
package tk.zhangwei.commons;

import java.util.Queue;
import java.util.function.Predicate;

import org.apache.commons.collections4.Unmodifiable;
import org.apache.commons.collections4.functors.NotNullPredicate;
import org.apache.commons.collections4.queue.CircularFifoQueue;
import org.apache.commons.collections4.queue.PredicatedQueue;
import org.apache.commons.collections4.queue.UnmodifiableQueue;

/***
 * Queue队列
 * 1.循环队列
 * @author zw
 *
 */
public class Demo04 {
    public static void main(String[] args){
        readOnly();
    }

    /***
     * 断言队列
     */
    public static void predicate(){
        CircularFifoQueue<String> que = new CircularFifoQueue<String>(2);
        que.add("a");
        que.add("b");
        que.add("c");
            }
    /**
     * 只读序列
     */
    public static void readOnly(){
        CircularFifoQueue<String> que = new CircularFifoQueue<String>(2);
        que.add("a");
        que.add("b");
        que.add("c");
        Queue<String> readOnly =UnmodifiableQueue.unmodifiableQueue(que);
        readOnly.add("a");
    }
    /***
     * 
     * 循环队列
     */

    public static void circular(){
        //循环队列
        CircularFifoQueue<String> que = new CircularFifoQueue<String>(2);
        que.add("a");
        que.add("b");
        que.add("c");
        //查看
        for(String temp:que){
            System.out.println(temp);
        }
    }
}
package tk.zhangwei.commons;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.apache.commons.collections4.IterableMap;
import org.apache.commons.collections4.MapIterator;
import org.apache.commons.collections4.iterators.ArrayIterator;
import org.apache.commons.collections4.iterators.LoopingIterator;
import org.apache.commons.collections4.iterators.UniqueFilterIterator;
import org.apache.commons.collections4.map.HashedMap;
/***
 * 迭代器的扩展
 * @author zw
 *
 */
public class Demo05 {
    public static void main(String[] args) {
        arrayList();
    }

    /***
     * 数组迭代
     */
    public static void arrayList(){
        int[] a ={1,2,3,4,5};
        //数组迭代器
        //Iterator<Integer> it = new ArrayIterator<Integer>(a);

        //指定起始索引和结束索引
        Iterator<Integer> it = new ArrayIterator<Integer>(a,1,3);
        while(it.hasNext()){
            System.out.println(it.next());
        }

    }
    /***
     * 循环迭代器
     */
    public static void loopIt(){
        List<String> list =new ArrayList<String>();
        list.add("张伟");
        list.add("老王");
        list.add("王麻子");
        Iterator<String> it = new LoopingIterator<String>(list);
        for(int i=0;i<10;i++){
            System.out.println(it.next());
        }

    }
    /*** 
     * 去重迭代器
     */
    public static void uniqueIt(){
        List<String> list =new ArrayList<String>();
        list.add("a");
        list.add("b");
        list.add("a");
        Iterator<String> it = new UniqueFilterIterator<String>(list.iterator());
        while(it.hasNext()){
            String str = it.next();
            System.out.println(str);
        }
    }

    /*** 
     * map迭代器
     */

    public static void mapIt(){
        IterableMap<String, String> map = new HashedMap<String,String>();
        map.put("a", "i love you");
        map.put("b", "i hate you");
        map.put("c", "i miss you");

        MapIterator<String, String> it = map.mapIterator();
        while(it.hasNext()){
            String key = it.next();//这是移动游标
            String value = it.getValue();
            System.out.println(key+"--->"+value);
        }
    }
}
package tk.zhangwei.commons;

import org.apache.commons.collections4.BidiMap;
import org.apache.commons.collections4.MapIterator;
import org.apache.commons.collections4.bidimap.DualHashBidiMap;
import org.apache.commons.collections4.bidimap.DualTreeBidiMap;

/***
 * 
 * 双向Map: 要求键与值不能重复
 * @author zw
 *
 */
public class Demo06 {

    public static void main(String[] args) {
        treeMap();
    }
    /***
     * 有序的双向Map
     */
    public static void treeMap(){
        BidiMap<String, Long> map = new DualTreeBidiMap<String,Long>();
        map.put("张伟",17711111111L);
        map.put("老王",18814568536L);
        map.put("王麻子",15502912005L);
        //反转遍历输出
        MapIterator<Long, String> it =map.inverseBidiMap().mapIterator();
        while(it.hasNext()){
            Long key = it.next();
            String value = it.getValue();
            System.out.println(key+"--->"+value);
        }
    }
    /***
     * 无序的双向Map
     */
    public static void hashMap(){
        BidiMap<String, Long> map = new DualHashBidiMap<String,Long>();
        map.put("张伟",17711111111L);
        map.put("老王",18814568536L);
        map.put("王麻子",15502912005L);

        System.out.println(map.inverseBidiMap().get(15502912005L));

        //反转遍历输出
        MapIterator<Long, String> it =map.inverseBidiMap().mapIterator();
        while(it.hasNext()){
            Long key = it.next();
            String value = it.getValue();
            System.out.println(key+"--->"+value);
        }
    }
}
package tk.zhangwei.commons;

import java.util.Iterator;
import java.util.Set;

import org.apache.commons.collections4.Bag;
import org.apache.commons.collections4.bag.HashBag;
import org.apache.commons.collections4.bag.TreeBag;

/***
 * BAG包允许重复
 * HashBag  无序
 * TreeBag  有序
 * @author zw
 *
 */
public class Demo07 {
    public static void main(String[] args){
        // 统计单词出现的次数
        String[] arr = "I love you not for who you are, but for who I am before you".split(" ");
        Bag<String> bag = new TreeBag<String>();
        for (String temp : arr) {
            bag.add(temp);
        }
        Set<String> keys = bag.uniqueSet();
        for (String letters : keys) {
            System.out.println(letters + "--->" + bag.getCount(letters));
        }

    }
    /***
     * 有序
     */
    public static void treeBag(){
        Bag<Integer> bag = new TreeBag<Integer>();
        bag.add(520);
        bag.add(520, 5);// 增加520 5次
        bag.remove(520, 1);// 移除520一次
        bag.add(1314);
        bag.add(521);
        Iterator<Integer> it = bag.iterator();
        while (it.hasNext()) {
            System.out.println(it.next());
        }

    }



    /***
     * 无序
     */
    public static void hashBag(){
        Bag<Integer> bag = new HashBag<Integer>();
        bag.add(520);
        bag.add(520,5);//增加520 5次
        bag.remove(520,1);//移除520一次
        bag.add(1314);
        bag.add(521);
        Iterator<Integer> it =bag.iterator();
        while(it.hasNext()){
            System.out.println(it.next());
        }

    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值