Total Pageviews

2014/10/27

How to use Google Guava to do Collection Filter

Requirement

We have a collection data (List of value object), we would like to find the objects which fyr is 101. 
Here has the collection data:
1:  dtoList=[  
2:  gov.nta.fms.dto.FmsSumRateDto@1a395e5a[fyr=101,accmon=01,alc=168407392348.53,alcavg=0.0809],   
3:  gov.nta.fms.dto.FmsSumRateDto@2c617429[fyr=101,accmon=02,alc=164834284030.53,alcavg=0.0792],   
4:  gov.nta.fms.dto.FmsSumRateDto@32114682[fyr=101,accmon=03,alc=180965071697.53,alcavg=0.0869],   
5:  gov.nta.fms.dto.FmsSumRateDto@c2c1a7c[fyr=101,accmon=04,alc=165659472439.53,alcavg=0.0796],   
6:  gov.nta.fms.dto.FmsSumRateDto@237ec922[fyr=101,accmon=05,alc=180372232532.53,alcavg=0.0866],   
7:  gov.nta.fms.dto.FmsSumRateDto@58c6e962[fyr=101,accmon=06,alc=176357775471.53,alcavg=0.0847],   
8:  gov.nta.fms.dto.FmsSumRateDto@6b044e76[fyr=101,accmon=07,alc=187368376522.53,alcavg=0.09],   
9:  gov.nta.fms.dto.FmsSumRateDto@67bb5bdd[fyr=101,accmon=08,alc=181824695849.53,alcavg=0.0873],   
10:  gov.nta.fms.dto.FmsSumRateDto@3d8c3f5f[fyr=101,accmon=09,alc=170308922011.45,alcavg=0.0818],   
11:  gov.nta.fms.dto.FmsSumRateDto@4294958a[fyr=101,accmon=10,alc=168308032423.95,alcavg=0.0808],   
12:  gov.nta.fms.dto.FmsSumRateDto@28e5b92c[fyr=101,accmon=11,alc=179491195734.95,alcavg=0.0862],   
13:  gov.nta.fms.dto.FmsSumRateDto@1e00cae[fyr=101,accmon=12,alc=157844063362.95,alcavg=0.0758],   
14:  gov.nta.fms.dto.FmsSumRateDto@1c607478[fyr=102,accmon=01,alc=170969213258.95,alcavg=0.0806],   
15:  gov.nta.fms.dto.FmsSumRateDto@7e80d6[fyr=102,accmon=02,alc=159840653037.95,alcavg=0.0753],   
16:  gov.nta.fms.dto.FmsSumRateDto@713c61da[fyr=102,accmon=03,alc=169579370948.95,alcavg=0.0799],   
17:  gov.nta.fms.dto.FmsSumRateDto@333c694a[fyr=102,accmon=04,alc=169624940547.95,alcavg=0.0799],   
18:  gov.nta.fms.dto.FmsSumRateDto@4324938d[fyr=102,accmon=05,alc=173064277154.95,alcavg=0.0816],   
19:  gov.nta.fms.dto.FmsSumRateDto@1653306b[fyr=102,accmon=06,alc=169616844409.95,alcavg=0.0799],   
20:  gov.nta.fms.dto.FmsSumRateDto@2f8fcc40[fyr=102,accmon=07,alc=175060305269.95,alcavg=0.0825],   
21:  gov.nta.fms.dto.FmsSumRateDto@9aa9625[fyr=102,accmon=08,alc=193638114494.95,alcavg=0.0913],   
22:  gov.nta.fms.dto.FmsSumRateDto@1e675c18[fyr=102,accmon=09,alc=198567712906.95,alcavg=0.0936],   
23:  gov.nta.fms.dto.FmsSumRateDto@13cf3b40[fyr=102,accmon=10,alc=181427982270.95,alcavg=0.0855],   
24:  gov.nta.fms.dto.FmsSumRateDto@a3af0e9[fyr=102,accmon=11,alc=187353709301.95,alcavg=0.0883],   
25:  gov.nta.fms.dto.FmsSumRateDto@239e563e[fyr=102,accmon=12,alc=172980508042.95,alcavg=0.0815]  
26:  ]  


How to Use Google Guava API
1. Create Predicate instance to determines a true or false value for a given input. (Line1~Line7)
2. Utilize Collection2.filter to return the elements of unfiltered that satisfy a predicate.(Line8~line9)

Here has the code snippet:
1:     Predicate predicate = new Predicate() {  
2:        @Override  
3:        public boolean apply(FmsSumRateDto input) {  
4:          //Determines true or false value for a given input.  
5:          return "101".equqls(input.getFyr());  
6:        }  
7:      };  
8:      //Returns the elements of unfiltered that satisfy a predicate.  
9:      Iterator result = Collections2.filter(dtoList, predicate).iterator();  
10:      while(result.hasNext()) {  
11:        log.debug("result="+result.next().toString());  
12:      }  

Here is the result which statisfy the predicate:
1:  result=gov.nta.fms.dto.FmsSumRateDto@1a395e5a[fyr=101,accmon=01,alc=168407392348.53,alcavg=0.0809]  
2:  result=gov.nta.fms.dto.FmsSumRateDto@2c617429[fyr=101,accmon=02,alc=164834284030.53,alcavg=0.0792]  
3:  result=gov.nta.fms.dto.FmsSumRateDto@32114682[fyr=101,accmon=03,alc=180965071697.53,alcavg=0.0869]  
4:  result=gov.nta.fms.dto.FmsSumRateDto@c2c1a7c[fyr=101,accmon=04,alc=165659472439.53,alcavg=0.0796]  
5:  result=gov.nta.fms.dto.FmsSumRateDto@237ec922[fyr=101,accmon=05,alc=180372232532.53,alcavg=0.0866]  
6:  result=gov.nta.fms.dto.FmsSumRateDto@58c6e962[fyr=101,accmon=06,alc=176357775471.53,alcavg=0.0847]  
7:  result=gov.nta.fms.dto.FmsSumRateDto@6b044e76[fyr=101,accmon=07,alc=187368376522.53,alcavg=0.09]  
8:  result=gov.nta.fms.dto.FmsSumRateDto@67bb5bdd[fyr=101,accmon=08,alc=181824695849.53,alcavg=0.0873]  
9:  result=gov.nta.fms.dto.FmsSumRateDto@3d8c3f5f[fyr=101,accmon=09,alc=170308922011.45,alcavg=0.0818]  
10:  result=gov.nta.fms.dto.FmsSumRateDto@4294958a[fyr=101,accmon=10,alc=168308032423.95,alcavg=0.0808]  
11:  result=gov.nta.fms.dto.FmsSumRateDto@28e5b92c[fyr=101,accmon=11,alc=179491195734.95,alcavg=0.0862]  
12:  result=gov.nta.fms.dto.FmsSumRateDto@1e00cae[fyr=101,accmon=12,alc=157844063362.95,alcavg=0.0758] 


Reference


No comments: