1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | //create a value object and add into list1 UnpaymentDataVo vo1 = new UnpaymentDataVo(); vo1.setDebtType("B"); vo1.setBudgetCode(new BigDecimal(1)); vo1.setDebitName("總預算"); vo1.setDamt(new BigDecimal(5156340000000L)); vo1.setCamt(new BigDecimal(3489209150690L)); vo1.setIamt(new BigDecimal(69360745739L)); vo1.setRamt(new BigDecimal(1597620103571L)); List<UnpaymentDataVo> list1 = new ArrayList<UnpaymentDataVo>(); list1.add(vo1); //create 2 value objects and add into list2 UnpaymentDataVo vo2 = new UnpaymentDataVo(); vo2.setDebtType("A"); vo2.setBudgetCode(new BigDecimal(2)); vo2.setDebitName("易淹水地區水患治理計劃第2期特別預算"); vo2.setDamt(new BigDecimal(7500000000L)); vo2.setCamt(new BigDecimal(0)); vo2.setIamt(new BigDecimal(637500000L)); vo2.setRamt(new BigDecimal(7500000000L)); UnpaymentDataVo vo3 = new UnpaymentDataVo(); vo3.setDebtType("A"); vo3.setBudgetCode(new BigDecimal(1)); vo3.setDebitName("振興經濟擴大公共建設特別預算"); vo3.setDamt(new BigDecimal(204400000000L)); vo3.setCamt(new BigDecimal(0)); vo3.setIamt(new BigDecimal(14465750000L)); vo3.setRamt(new BigDecimal(204400000000L)); List<UnpaymentDataVo> list2 = new ArrayList<UnpaymentDataVo>(); list2.add(vo2); list2.add(vo3); //print list1 and list2 log.debug("list1=" + list1.toString()); log.debug("list2=" + list2.toString()); |
The data structure looks like this:
1 2 3 | - list1=[UnpaymentDataVo(debtType=B, budgetCode=1, debitName=總預算, damt=5156340000000, camt=3489209150690, iamt=69360745739, ramt=1597620103571)] - list2=[UnpaymentDataVo(debtType=A, budgetCode=2, debitName=易淹水地區水患治理計劃第2期特別預算, damt=7500000000, camt=0, iamt=637500000, ramt=7500000000), UnpaymentDataVo(debtType=A, budgetCode=1, debitName=振興經濟擴大公共建設特別預算, damt=204400000000, camt=0, iamt=14465750000, ramt=204400000000)] |
If you would like to find out the data from list2 which condition is debt_type='A' and budget_code=2
1 2 3 4 5 6 7 8 9 10 11 12 | // find out the data from list2 which condition is debt_type='A' and budget_code=2 List<UnpaymentDataVo> searchResult = new ArrayList<UnpaymentDataVo>(); CollectionUtils.select(list2, new Predicate() { @Override public boolean evaluate(Object object) { UnpaymentDataVo vo = (UnpaymentDataVo) object; return "A".equals(vo.getDebtType()) && vo.getBudgetCode().compareTo(new BigDecimal(2)) == 0; } }, searchResult); log.debug("searchResult = " + searchResult.toString()); |
The search result looks like this:
If you would like to get a Collection containing the union of list1 and list2
1 2 | - searchResult = [UnpaymentDataVo(debtType=A, budgetCode=2, debitName=易淹水地區水患治理計劃第2期特別預算, damt=7500000000, camt=0, iamt=637500000, ramt=7500000000), ] |
1 2 3 4 | // Returns a Collection containing the union of the given Collections List<UnpaymentDataVo> unionResult = (List<UnpaymentDataVo>) CollectionUtils.union(list1, list2); log.debug("unionResult = " + unionResult.toString()); |
The union result looks like this:
1 2 3 | - unionResult = [UnpaymentDataVo(debtType=A, budgetCode=2, debitName=易淹水地區水患治理計劃第2期特別預算, damt=7500000000, camt=0, iamt=637500000, ramt=7500000000), UnpaymentDataVo(debtType=A, budgetCode=1, debitName=振興經濟擴大公共建設特別預算, damt=204400000000, camt=0, iamt=14465750000, ramt=204400000000), UnpaymentDataVo(debtType=B, budgetCode=1, debitName=總預算, damt=5156340000000, camt=3489209150690, iamt=69360745739, ramt=1597620103571)] |
If you would like to return a collection which contains unionResult - list2
1 2 3 4 | // Returns a new Collection containing a - b. List<UnpaymentDataVo> subtractResult = (List<UnpaymentDataVo>) CollectionUtils.subtract( unionResult, list2); log.debug("subtractResult = " + subtractResult.toString()); |
The subtract result looks like this:
1 | - subtractResult = [UnpaymentDataVo(debtType=B, budgetCode=1, debitName=總預算, damt=5156340000000, camt=3489209150690, iamt=69360745739, ramt=1597620103571)] |
Reference
[1] https://commons.apache.org/proper/commons-collections/javadocs/api-3.2.1/org/apache/commons/collections/CollectionUtils.html#subtract(java.util.Collection, java.util.Collection)