设为首页 加入收藏

TOP

Apache Commons Collections基本操作(Predicate、Transformat、Closure等)(二)
2015-11-21 01:01:37 来源: 作者: 【 】 浏览:10
Tags:Apache Commons Collections 基本操作 Predicate Transformat Closure
private String level; public Level() { } public Level(String name, String level) { super(); this.name = name; this.level = level; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getLevel() { return level; } public void setLevel(String level) { this.level = level; } @Override public String toString() { return "(码农:"+this.name+",水平:"+this.level+")"; } }
package Collections;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;

import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.collections4.Predicate;
import org.apache.commons.collections4.Transformer;
import org.apache.commons.collections4.functors.SwitchTransformer;

/**
 * 解耦:将 业务处理与判断进行分离
 * 
 * 函数式编程Transformat 类型转换
 * 1.Transformer+CollectionUtils.collect
 * 
 * 2.SwitchTransformer
 *   CollectionUtils.collect(容器,转换器)
 */

@SuppressWarnings("all")
public class Demo02 {   
    public static void main(String[] args) {
        inner();
        define();
    }

    //内置类型的转化
    public static void inner()
    {
        System.out.println("========《内置类型转换 长整型时间日期,转成指定格式的字符串》========");
        //类型转换器
        Transformer
   
     trans = new Transformer
    
     () { @Override public String transform(Long input) { return new SimpleDateFormat("yyyy年MM月dd日").format(input); } }; //容器 List
     
       list = new ArrayList<>(); list.add(99999999L); list.add(30000L); //工具类:程序员出钱<---开发商--->农民工出力 Collection
      
        result = CollectionUtils.collect(list, trans); //遍历查看结果 for(String time:result){ System.out.println(time); } } //自定义类型转换 public static void define(){ System.out.println("==========《自定义类型转换》==========="); Predicate
       
         isLow = new Predicate
        
         (){ public boolean eva luate(Employee emp) { return emp.getSalary()<=10000; } }; Predicate
         
           isHigh = new Predicate
          
           () { public boolean eva luate(Employee emp) { return emp.getSalary()>=10000; } }; Predicate[] pres = {isLow,isHigh}; //转换 Transformer
           
             lowtrans = new Transformer
            
             () { @Override public Level transform(Employee input) { return new Level(input.getName(),"低薪"); } }; //转换 Transformer
             
               hightrans = new Transformer
              
               () { @Override public Level transform(Employee input) { return new Level(input.getName(),"高薪"); } }; Transformer[] trans = {lowtrans,hightrans}; //二者进行了关联 Transformer switchTrans = new SwitchTransformer<>(pres, trans, null); List
               
                 list = new ArrayList<>(); list.add(new Employee("凤姐",10000000)); list.add(new Employee("犀利哥",1000)); Collection
                
                  levelList = CollectionUtils.collect(list, switchTrans); //遍历容器 Iterator
                 
                   levelIt = levelList.iterator(); while(levelIt.hasNext()) { System.out.println(levelIt.next()); } } }
                 
                
               
              
             
            
           
          
         
        
       
      
     
    
   

运行结果:

========《内置类型转换 长整型时间日期,转成指定格式的字符串》========
1970年01月02日
1970年01月01日
==========《自定义类型转换》===========
(码农:凤姐,水平:高薪)
(码农:犀利哥,水平:低薪)

三、Closure 闭包封装业务功能

package Collections;
/**
 * 员工类
 */
public class Employee {
    private String name;
    private double salary;
    //alt+/
    public Employee() {
    }

    //alt+shift+s +o
    public Employee(String name, double salary) {
        super();
        this.name = name;
        this.salary = salary;
    }

    //alt+shift+s  +r tab  回车  shift+tab  回车
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name
首页 上一页 1 2 3 4 5 6 7 下一页 尾页 2/7/7
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇C++ string 类 学习笔记 下一篇C++――2828: 素数判断

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: