1. 程式人生 > >commons中 CollectionsUtils的一些功能

commons中 CollectionsUtils的一些功能

goods oid do..while 元素 spa tor arr string 函數

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包,以上是其中的一些功能,可以用於對容器內的元素進行操作

commons中 CollectionsUtils的一些功能