Total Pageviews

2013/11/25

How to do collection filter in Java

Requirement
Assume I have a collection of Cta612pDto, and this collection contains:
1:  [Cta612pDto [acc=0100, amt=13033356, seqnm=(*)年度預算支出, ypay=null, formattedAmt=null],   
2:  Cta612pDto [acc=0101, amt=3809733, seqnm=  一般政務支出                                , ypay=null, formattedAmt=null],   
3:  Cta612pDto [acc=0102, amt=3894280, seqnm=  國防支出                                  , ypay=null, formattedAmt=null],   
4:  Cta612pDto [acc=0103, amt=2532563, seqnm=  教育科學文化支出                              , ypay=null, formattedAmt=null],   
5:  Cta612pDto [acc=0104, amt=1655232, seqnm=  經濟發展支出                                , ypay=null, formattedAmt=null],   
6:  Cta612pDto [acc=0105, amt=460708, seqnm=  社會福利支出                                , ypay=null, formattedAmt=null],   
7:  Cta612pDto [acc=0106, amt=59111, seqnm=  社區發展及環境保護支出                           , ypay=null, formattedAmt=null],   
8:  Cta612pDto [acc=0107, amt=366285, seqnm=  退休撫卹支出                                , ypay=null, formattedAmt=null],   
9:  Cta612pDto [acc=0109, amt=255444, seqnm=  一般補助及其他支出                             , ypay=null, formattedAmt=null],   
10:  Cta612pDto [acc=0200, amt=130015, seqnm=(*)以前年度支出                                 , ypay=null, formattedAmt=null],   
11:  Cta612pDto [acc=0400, amt=14271298, seqnm=(*)特種基金及保管款支出                             , ypay=null, formattedAmt=null],   
12:  Cta612pDto [acc=0500, amt=0, seqnm=(*)國庫券及短期借款還本支出, ypay=null, formattedAmt=null],   
13:  Cta612pDto [acc=8888, amt=1539861, seqnm=null, ypay=null, formattedAmt=null],   
14:  Cta612pDto [acc=9800, amt=757028109, seqnm=合計, ypay=null, formattedAmt=null],   
15:  Cta612pDto [acc=9900, amt=849981138506, seqnm=累計, ypay=null, formattedAmt=null],   
16:  Cta612pDto [acc=9991, amt=1539861, seqnm=  支付註銷, ypay=null, formattedAmt=null],   
17:  Cta612pDto [acc=9992, amt=728053579, seqnm=  支出收回, ypay=null, formattedAmt=null],   
18:  Cta612pDto [acc=9999, amt=728053579, seqnm=null, ypay=null, formattedAmt=null]]  

We need to remove some java beans from this collection when acc is 8888 or 9999.

Solution
We can make good use of PredicateUtils.nonePredicate to define our conditions which provide by Apache commons Collection. It will create a new Predicate that returns true if none of the specified predicates are true. If the array of predicates is empty, then this predicate returns true. 
And also use CollectionUtils.filter to filter the collection by applying a Predicate to each element.
1:      BeanPropertyValueEqualsPredicate acc8888Predicate = new BeanPropertyValueEqualsPredicate(  
2:          "acc", "8888");  
3:      BeanPropertyValueEqualsPredicate acc9999Predicate = new BeanPropertyValueEqualsPredicate(  
4:          "acc", "9999");  
5:      Predicate predicate = PredicateUtils.nonePredicate(new Predicate[] { acc8888Predicate,  
6:          acc9999Predicate });  
7:      CollectionUtils.filter(dataList, predicate);  

After doing CollectionUtils.filter, you can find out two java beans had been removed from data list collection.
1:  [Cta612pDto [acc=0100, amt=13033356, seqnm=(*)年度預算支出, ypay=null, formattedAmt=13,033,356.00],   
2:  Cta612pDto [acc=0101, amt=3809733, seqnm=  一般政務支出                                , ypay=null, formattedAmt=3,809,733.00   ],   
3:  Cta612pDto [acc=0102, amt=3894280, seqnm=  國防支出                                  , ypay=null, formattedAmt=3,894,280.00   ],   
4:  Cta612pDto [acc=0103, amt=2532563, seqnm=  教育科學文化支出                              , ypay=null, formattedAmt=2,532,563.00   ],   
5:  Cta612pDto [acc=0104, amt=1655232, seqnm=  經濟發展支出                                , ypay=null, formattedAmt=1,655,232.00   ],   
6:  Cta612pDto [acc=0105, amt=460708, seqnm=  社會福利支出                                , ypay=null, formattedAmt=460,708.00   ],   
7:  Cta612pDto [acc=0106, amt=59111, seqnm=  社區發展及環境保護支出                           , ypay=null, formattedAmt=59,111.00   ],   
8:  Cta612pDto [acc=0107, amt=366285, seqnm=  退休撫卹支出                                , ypay=null, formattedAmt=366,285.00   ],   
9:  Cta612pDto [acc=0109, amt=255444, seqnm=  一般補助及其他支出                             , ypay=null, formattedAmt=255,444.00   ],   
10:  Cta612pDto [acc=0200, amt=130015, seqnm=(*)以前年度支出                                 , ypay=null, formattedAmt=130,015.00],   
11:  Cta612pDto [acc=0400, amt=14271298, seqnm=(*)特種基金及保管款支出                             , ypay=null, formattedAmt=14,271,298.00],   
12:  Cta612pDto [acc=0500, amt=0, seqnm=(*)國庫券及短期借款還本支出, ypay=null, formattedAmt=0.00],   
13:  Cta612pDto [acc=9800, amt=757028109, seqnm=合計, ypay=null, formattedAmt=757,028,109.00],   
14:  Cta612pDto [acc=9900, amt=849981138506, seqnm=累計, ypay=null, formattedAmt=849,981,138,506.00],   
15:  Cta612pDto [acc=9991, amt=1539861, seqnm=  支付註銷, ypay=null, formattedAmt=1,539,861.00   ],   
16:  Cta612pDto [acc=9992, amt=728053579, seqnm=  支出收回, ypay=null, formattedAmt=728,053,579.00   ]]  

No comments: