Encountered a practical problem today
How to judge whether there are duplicate elements in a List
I thought it was impossible to compare them one by one
So I checked and found that I can use the characteristics of HashSet to do it
Because duplicate elements are not allowed in the Set collection
So get the following processing
/**
* 列表中是否包含重复元素
*
* @param list
* @return
*/
public static boolean isRepeatList(List list) {
if (list == null || list.isEmpty()) {
return false;
}
Set set = new HashSet<>(list);
if (set.size() == list.size()) {
return false;
} else {
return true;
}
}