I have two lists of my custom class
public class WiSeConSyncItem{
int processId=1;//unique
int syncStatus=0;//variable
}
And List<WiSeConSyncItem> mainItems=new ArrayList();
mainItems
will iterate and execute corresponding API's( take time). And another timer will add some data to mainItems
.
So if we added the second list to mainItems
, it may contain duplicated items. So I want to remove the duplicated item from the second list and add to the first list.
The remove duplicate function
public static List<WiSeConSyncItem> removeDuplicate(List<WiSeConSyncItem> syncItems) {
Set set = new TreeSet(new Comparator<WiSeConSyncItem>() {
@Override
public int compare(WiSeConSyncItem o, WiSeConSyncItem o1) {
if (o.getProcessId() == o1.getProcessId())
return 0;//to remove duplicate
else
return 1;
}
});
set.addAll(syncItems);
return new ArrayList<>(set);
}
I can't remove the items from
mainItems
because that objects may be executing by other threads. I can't useSet
orHashMap
because of this item came from another sdk. ( yeh I can create a new model from it, but please suggest from this content)