Newbie
Newbie

Reputation: 1724

Get distinct value by using ArrayList#contains() not working

I have a JComboBox call cmb_user_id, I get all the user ID from database and save it inside an array of objects called borrow_data, but there is some duplicated data in it, so I use .contains() method to filter it, below is my code:

ArrayList<String> al_id = new ArrayList<>();
cmb_user_id.addItem("Select One");
for(int i = 0; i < borrow_data.length; ++i)
{
    if(!al_id.contains(borrow_data[i].getUser().getId()));
    {
        cmb_user_id.addItem(borrow_data[i].getUser().getId());
        al_id.add(borrow_data[i].getUser().getId());
        System.out.println("Content: " + borrow_data[i].getUser().getId());
    }
}

But after all, all data are still saved into cmb_user_id, including the duplicated data, my filter doesn't work at all.

Upvotes: 0

Views: 91

Answers (1)

Jorn Vernee
Jorn Vernee

Reputation: 33875

if(!al_id.contains(borrow_data[i].getUser().getId()));

Has a ; at the of it, meaning that the following {...} will always execute.

Upvotes: 4

Related Questions