我需要验证给定的 ID 列表不包含任何重复值。我的尝试可以在这里显示:
public void validate(RecordCollection collection)
throws BusinessException {
LinkedHashMap<Long, Long> existingIds = new LinkedHashMap<Long, Long>();
for (Record record : collection.getArrayList()) {
// check that you don't have two records with the same id
if (existingIds.containsKey(record.getID())) {
throw new BusinessException("records must be unique.",
ExceptionCodes.RECORDS_MUST_BE_UNIQUE);
}
//add the id to the map of existing ids
existingIds.put(record.getID(), vo.getID());
}
是否有更有效的方法来实施此验证?