private List<Long> getIntersection(List<Long> source1, List<Long> source2) {
List<Long> ids = new ArrayList<>();
if (CollectionUtils.isEmpty(source1) || CollectionUtils.isEmpty(source2)) {
return ids;
}
for (int i = 0; i < source1.size(); i++) {
Long eventId = source1.get(i);
for (int j = 0; j < source2.size(); j++) {
if (eventId.longValue() == source2.get(j).longValue()) {
ids.add(eventId);
}
}
}
return ids;
}
拷贝对象
/**
* 复制对象
*
* @param source 源 要复制的对象
* @param target 目标 复制到此对象
* @param <T>
* @return
*/
public static <T> T copyToClazz(Object source, Class<T> target) {
if (source == null || target == null) {
return null;
}
try {
T newInstance = target.newInstance();
BeanUtils.copyProperties(source, newInstance);
return newInstance;
} catch (Exception e) {
throw new RuntimeException(e);
}
}
拷贝集合
/**
* 复制list
* 需要拷贝集合中的每一个元素
* @param source
* @param target
* @param <T>
* @param <K>
* @return
*/
public static <T, K> List<K> copyList(List<T> source, Class<K> target) {
if (null == source || source.isEmpty()) {
return Collections.emptyList();
}
return source.
stream().
map(e -> copyToClazz(e, target)).
collect(Collectors.toList());
}