commons中 CollectionsUtils的一些功能

package haohaoxuexi;
/**
* 函数式编程Closure 闭包封装业务功能
* 1. Closure
* CollectionUtils.forAllDo(容器,功能类对象)
*
* 2. IfClosure
* IfClosure.ifClosure(断言,功能1,功能2)
* CollectionUtils.forAllDo(容器,功能类对象)
*
* 3. WhileClosure
* WhileClosure.whileClosure(断言,功能,标识符)
* CollectionUtils.forAllDo(容器,功能类对象)
*
* 4. ChainedClosure
* ChainedClosure.chainedClosure(功能列表)
* CollectionUtils.forAllDo(容器,功能类对象)
*
*/

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

import org.apache.commons.collections4.Closure;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.collections4.Predicate;
import org.apache.commons.collections4.functors.ChainedClosure;
import org.apache.commons.collections4.functors.IfClosure;
import org.apache.commons.collections4.functors.WhileClosure;

public class lianxi33 {
public static void main(String[] args) {

basic();
System.out.println("+++++++++++++++++++++++++++");
isClosure();
System.out.println("+++++++++++++++++++++++++++");
whileClosure();
System.out.println("++++++++++++++++++++++++++++");
chainClousure();

}
public static void basic(){
//定义一个容器,放入内容
List<lianxi31> emList=new ArrayList<lianxi31>();
emList.add(new lianxi31("BJSXT",20000));
emList.add(new lianxi31("BJasdT",200000));
emList.add(new lianxi31("BJsd",2000));

//新建一个closure对象,规定出closure格式
Closure<lianxi31> colsClosure=new Closure<lianxi31>() {
public void execute(lianxi31 emp){
emp.setSalary(emp.getSalary()*1.2);
}};
//利用工具类,实现对容器中内容的集体控制
CollectionUtils.forAllDo(emList, colsClosure);
//利用迭代器进行遍历输出
Iterator<lianxi31> emIterator=emList.iterator();
while (emIterator.hasNext()) {
System.out.println(emIterator.next());
}

}
/**
* 二选一 如果打折商品,进行9折;否则满百减20
*/
public static void isClosure(){
//定义容器放入内容
List<lianxi34> emList=new ArrayList<lianxi34>();
emList.add(new lianxi34("asdasd",120,true));
emList.add(new lianxi34("asd",100,false));
emList.add(new lianxi34("aasd",10,false));
//定义closure
Closure<lianxi34> subClosure=new Closure<lianxi34>() {
public void execute(lianxi34 goods){
goods.setPrice(goods.getPrice()*1.2);
}
};
//满百减20
Closure<lianxi34> subtract=new Closure<lianxi34>() {
public void execute(lianxi34 goods){
if (goods.getPrice()>=100) {
goods.setPrice(goods.getPrice()-20);
}
}
};
//打九折
Closure<lianxi34> discount=new Closure<lianxi34>() {
public void execute(lianxi34 goods){
if (goods.getDiscount()) {
goods.setPrice(goods.getPrice()*0.9);
}
}
};
//判断
Predicate<lianxi34> preClosure=new Predicate<lianxi34>() {
@Override
public boolean evaluate(lianxi34 goods) {
return goods.getDiscount();
}
};
//利用ifclosure实现对容器的操作定义
Closure<lianxi34> ifClo=IfClosure.ifClosure(preClosure, discount,subtract);
//执行操作
CollectionUtils.forAllDo(emList, ifClo);
//利用迭代器遍历输出
Iterator<lianxi34> emIterator=emList.iterator();
while (emIterator.hasNext()) {
System.out.println(emIterator.next());;
}
}

/**
* 将工资不断上涨知道到达全部10000以上
*/
public static void whileClosure()
{
//数据
List<lianxi31> empList = new ArrayList<>();
empList.add(new lianxi31("周杰伦",20000));
empList.add(new lianxi31("范冰冰",30000));
empList.add(new lianxi31("黄晓明",5000));

//业务功能 每次上涨0.2
Closure<lianxi31> cols = new Closure<lianxi31>()
{
@Override
public void execute(lianxi31 emp) {
emp.setSalary(emp.getSalary()*1.2);
}

};

//判断
Predicate<lianxi31> empPre = new Predicate<lianxi31>() {
@Override
public boolean evaluate(lianxi31 emp) {
return emp.getSalary()<10000;
}
};

//false 表示while结构先判断后执行
//true 表示do..while先执行后判断
Closure<lianxi31> whileCols = WhileClosure.whileClosure(empPre,cols,false);


//工具类
CollectionUtils.forAllDo(empList, whileCols);

//操作后的数据
Iterator<lianxi31> empIt = empList.iterator();
while(empIt.hasNext())
{
System.out.println(empIt.next());
}
}
/**
*折上减 如果打折商品,先进行9折,如果还满百,再减20
*/
public static void chainClousure()
{
List<lianxi34> goodsList = new ArrayList<>();
goodsList.add(new lianxi34("Android视频",120,true));
goodsList.add(new lianxi34("javaee视频",110,false));
goodsList.add(new lianxi34("Spack视频",80,false));

//满百减20
Closure<lianxi34> subtract = new Closure<lianxi34>() {
@Override
public void execute(lianxi34 input) {
if(input.getPrice()>=100){
input.setPrice(input.getPrice()-20);
}
}
};

//打折
Closure<lianxi34> discount = new Closure<lianxi34>() {
@Override
public void execute(lianxi34 input) {
if(input.getDiscount()){
input.setPrice(input.getPrice()*0.9);
}
}
};

//链式操作
Closure<lianxi34> chinaClo = ChainedClosure.chainedClosure(discount,subtract);

//关联
CollectionUtils.forAllDo(goodsList,chinaClo);

//查看操作后的数据
for(lianxi34 temp:goodsList)
{
System.out.println(temp);
}
}
}

public class lianxi31 {
private String name;
private double salary;

public lianxi31() {
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public double getSalary() {
return salary;
}

public void setSalary(double salary) {
this.salary = salary;
}

public lianxi31(String name, double salary) {
super();
this.name = name;
this.salary = salary;
}
@Override
public String toString() {
return "码农:"+this.name+"工资"+this.salary;
}
}

public class lianxi34 {
private String name;
private double price;
private boolean discount;
public lianxi34() {
// TODO Auto-generated constructor stub
}
public lianxi34(String name, double price, boolean discount) {
super();
this.name = name;
this.price = price;
this.discount = discount;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public boolean getDiscount() {
return discount;
}
public void setDiscount(Boolean discount) {
this.discount = discount;
}
@Override
public String toString() {
return "商品:"+this.name+"价格:"+this.price+"是否打折:"+this.discount;
}

}

guava的jar包导入之后没法使用,再网上找了很久的解决方法,还是没有解决,所以找了commons的jar包,以上是其中的一些功能,可以用于对容器内的元素进行操作

原文地址:https://www.cnblogs.com/zx931880423/p/6819068.html