I am looping through a set of beans and doing a check for something and adding to a list. I only want to add one occurrence of a particular item
List<PartsBean> beans = hotPartsDAO.getDeletedList(user);
List<FinalItemBean> finalItemList = new ArrayList<FinalItemBean>();
for (PartsBean bean : beans)
{
if (!bean.getFinalItem().isActive())
{
finalItemList.add(bean.getFinalItem());
}
}
In the list I get for example
"test"
"test"
"test"
"hello"
I just want 1 “test”
If I were to use a set then how would I modify this so that I can add the components of that set to this below:
This here is how its currently being passed over List<FinalItemBean> list
protected final void buildFinalItemFilterList( Action action, PartsDAO dao, List<FinalItemBean> list )
throws SQLException
{
List<FinalItemBean> finalItems = dao.getAllFinalItems( false );
FinalItemBean finalItem;
for (FinalItemBean e: list )
{
finalItem = dao.getFinalItemById(e.getId());
finalItems.add(finalItem);
}
Collections.sort( finalItems );
action.setRequestAttribute("finalItems", finalItems );
}
The interface list has a Method called contains, which return a true if that ‘occurrence’ is on the list and a false if is not in the list.
Like:
Pretty easy. You could also use a Set instead, but, I think lists are more flexible than sets, or that’s what some developers believe.