Java – removes duplicate beans from the list
•
Java
I loop through a set of beans and check something and add it to the list I just want to add a specific 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()); } }
I got examples in the list
"test" "test" "test" "hello"
I just want a "test"
If I want to use a collection, how do I modify it so that I can add the components of the collection to the following:
This is how it is currently passed through list < finaleitembean > name 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 ); }
Solution
The interface list has a method named contains. If there is' occurrence 'in the list, it returns true. If it is not in the list, it returns false
if(!myList.contains(person)) myList.add(person);
It's easy You can also use sets, but I think lists are more flexible than sets, or that's what some developers believe
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码