-1

I have a ArrayList of Accounts.The account names with either A0001... and Z0001..I pass this arraylist to api which need to filter names with Z200 and remove from the list without using a another collection class. Below are my classes

class Account {
private String accountName;
private long accountNumber;
private String accountType;

public String getAccountName() {
    return accountName;
}

public void setAccountName(String accountName) {
    this.accountName = accountName;
}

public long getAccountNumber() {
    return accountNumber;
}

public void setAccountNumber(long accountNumber) {
    this.accountNumber = accountNumber;
}

public String getAccountType() {
    return accountType;
}

public void setAccountType(String accountType) {
    this.accountType = accountType;
}

}

public class GenericTest {

public static List filterObjects(List accountList) {

    return list;
}

public static void main(String[] args) {
    List<Account> accList = new ArrayList<Account>();
    Account acc1 = new Account("A0001", 898989, "Savings");
    Account acc2 = new Account("A0002", 345126, "Current");
    Account acc3 = new Account("Z0001", 123467, "Savings");
    Account acc4 = new Account("Z0002", 879000, "Fixed");
    Account acc5 = new Account("Z0003", 898989, "Current");
    accList.add(acc1);
    accList.add(acc2);
    accList.add(acc3);
    accList.add(acc4);
    accList.add(acc5);

    GenericTest gt = new GenericTest();
    List<Account> filteredList = gt.filterObjects(accList);

    for (Account acc : filteredList) {
        System.out.println(acc.getAccountName());
    }
}

}

The filterObjects api should remove the account objects starting with Z and return the other objects without using another collection inside the api method.I have searched google and stackoverflow but didnt find a suitable solution.Please give me some ideas so i will work on those.Thanks in advance.

Ramesh
  • 43
  • 4
  • 1
    What do you mean by *without using another collection class*? Why would you need that anyway? – Manu Sep 11 '15 at 13:10
  • Hi Manu, This was asked me in an recent interview and the interviewer put a condition that I should not use another collection but to modify the same what has been passed and return after removing the objects starts with Z – Ramesh Sep 11 '15 at 13:15
  • That must be the easiest interview question ever. – Manu Sep 11 '15 at 13:18

1 Answers1

4

Loop over your collections and remove elements starting with a Z:

public static List<Account> filterObjects(List<Account> accountList) {
    for(int i = 0; i < accountList.size(); i++) {
        if(accountList.get(i).getAccountName().startsWith("Z")) {
            accountList.remove(i);
            i--; // Just removed an element, so jump back one index.
        }
    }
    return accountList;
}

Or with an Iterator:

public static List<Account> filterObjects(List<Account> accountList) {
    for (Iterator<Account> iterator = accountList.iterator(); iterator.hasNext();) {
        Account account = iterator.next();
        if (account.getAccountName().startsWith("Z")) {
            iterator.remove();
        }
    }
}
Manu
  • 1,474
  • 1
  • 12
  • 18
  • 1
    You should use an `Iterator` for this – Titus Sep 11 '15 at 13:18
  • 1
    It is not an option to use an iterator. Your first example will throw a ConcurrentModificationException. – bhspencer Sep 11 '15 at 13:26
  • 1
    Thanks Manu..I tried both versions and they both worked.Thanks a ton..Cheers – Ramesh Sep 11 '15 at 13:26
  • Really? You tried the first example and it worked. http://stackoverflow.com/questions/1675037/removing-items-from-a-collection-in-java-while-iterating-over-it – bhspencer Sep 11 '15 at 13:27
  • Yes bhspencer..i didnt get ConcurrentModificationException and get output as A0001 A0002 – Ramesh Sep 11 '15 at 13:33
  • It's not. You're confusing a standard for-loop with `foreach` – Manu Sep 11 '15 at 13:34
  • It is wildly know that you should not call remove on a list while traversing it. Consider this question with 400+ upvotes http://stackoverflow.com/questions/1196586/calling-remove-in-foreach-loop-in-java – bhspencer Sep 11 '15 at 13:35
  • @bhspencer That question discusses a `foreach` loop... As I pointed out before. – Manu Sep 11 '15 at 13:36
  • How about this question which is just taking about a for loop with 800+ upvotes http://stackoverflow.com/questions/223918/iterating-through-a-list-avoiding-concurrentmodificationexception-when-removing – bhspencer Sep 11 '15 at 13:38