使用
public Map<Integer, User> getUserByIds(Collection<Integer> ids) {
...
}
而不是
public List<User> getUserByIds(Collection<Integer> ids) {
...
}
原因是后者会引入一些困惑 :
1>id可以重复吗?重复时会去重吗?
2>返回的List的顺序和ids相关吗?
3>某个id不存在, List里是跳过还是null?
接口实现时应该做ids最大长度校验. 若超出则直接抛异常, 否则可能会因性能上的问题拖垮整个服务.
import BoundExceededException;
Map<Integer, User> getUserbyIds(List<Integer> ids) throws BoundExceededException {
if (ids.size() > bound) {
throw new BoundExceededException();
}
...
}
原文:https://www.cnblogs.com/yanch01/p/12622888.html