-5

I have 3 Array Lists as below, need to get all Lists those are with INDIVIDUAL using Java code

Array List: [08/16/2020 11:00, Y, INDIVIDUAL, Sam S, 2/15/1980, DOLLARS, Cash In]

Array List: [08/16/2020 11:00, Y, INDIVIDUAL, Sam S, 2/15/1980, DOLLARS, Cash In]

Array List: [08/16/2020 11:00, Y, INDIVIDUAL, Sam Kin, 2/15/1980, DOLLARS, Cash In]

Array List: [08/16/2020 11:00, Y, Business, King S, 2/15/1980, DOLLARS, Cash In]

From above I need to get INDIVIDUAL mentioned lists as output in Java code Please help

Murali N
  • 1
  • 2

2 Answers2

1

Assuming "INDIVIDUAL" always appears at index 2:

List<List<String>> individualLists = new ArrayList<>();
for (List<String> list: allLists) {
    if (list.get(2).equals("INDIVIDUAL")) {
        individualLists.add(list);
    }
}
Aziz Sonawalla
  • 2,482
  • 1
  • 5
  • 6
  • thanks for quick reply. all INDIVIDUAL& John Smith, need those rows ArrayList: [08/16/2020 11:00, Y, INDIVIDUAL, John Smith, 2/15/1980, CAD, Cash In] ArrayList: [08/16/2020 11:00, Y, INDIVIDUAL, John Smith, 2/15/1980, CAD, Cash In] ArrayList: [08/16/2020 11:00, Y, INDIVIDUAL, John Smith, 2/15/1980, CAD, Cash In] ArrayList: [08/16/2020 11:00, Y, BUSINESS, KIRAN KUMAR, 2/15/1980, CAD, Cash In] ArrayList: [08/16/2020 11:00, Y, INDIVIDUAL, KIRAN KUMAR, 2/15/1980, CAD, Cash In] – Murali N Aug 31 '20 at 15:24
0

if INDIVIDUAL is always in index 2 you can have function like:

ArrayList<ArrayList<String>> filter(ArrayList<ArrayList<String>> lists){
    ArrayList<ArrayList<String>> out = new ArrayList<>();
    for(ArrayList<String> list : lists){
        if(list.get(2).equal("INDIVIDUAL"))
            out.add(list);
    }
    return out;
}

if you dont know the individual index replace the if with a function of this kind:

boolean isIndividual(ArrayList<String> list){
    for(String str: list){
        if(str.equal('INDIVIDUAL`) == TRUE)
            return True;
    }
    return False;
} 
Finci
  • 108
  • 1
  • 10