0

I have two arraylists and will like to return the difference of the two. Below is my code snippet:

@ResponseBody
@RequestMapping(value="/unpaidfees", method=RequestMethod.GET, produces="application/json")
public List<Fee> unpaid(@PathVariable("studentid") Long studentid){
    Transactions tr = transServ.instalmentalstd(studentid);
    List<Fee> allfees = feesServ.allFees();

    if(tr != null){
        List<Fee> paidfees = transServ.paidfees(tr.getTranxid());
        allfees.removeAll(paidfees);
    }

    return allfees;     
}

In summary. I have an array list which contains all fees :

List<Fee> allfees = feesServ.allFees();

And the second arraylist which contains paid fees:

List<Fee> paidfees = transServ.paidfees(tr.getTranxid());

I will like to return a list of fees present in allfees only but not in paidfees. Please advise on how to achieve this or a better way to go about this.

Thank you for your time.

Mohammed Aouf Zouag
  • 17,042
  • 4
  • 41
  • 67
Femi Adigun
  • 59
  • 2
  • 10
  • Hi Sotirios Delimanolis. Thanks for your time. However, its not a duplicate because I tried the question you referred me to but it returned all fees and didnt work for me. – Femi Adigun Jan 08 '16 at 22:03
  • The issue was with absence of equals() in the Fee class/model. Incase you are reading this, dont suffer same way I have for 3 days and nights. Dont forget to add equals() and Hashcode to your entity else all the suggestions wont work for you. – Femi Adigun Jan 20 '16 at 07:38

2 Answers2

1

Use ArrayList#removeAll():

// Copy all fees
List<Fee> difference = new ArrayList<>(allFees);

// Keep only fees that are different
difference.removeAll(paidfees);
Mohammed Aouf Zouag
  • 17,042
  • 4
  • 41
  • 67
1

Use removeAll:

List<Fee> unpaidFees = new ArrayList<>(allfees);
unpaidFees.removeAll(paidfees);
Jean Logeart
  • 52,687
  • 11
  • 83
  • 118