new HashSet<NestedItem>(theList).size()应该适合您的需要,因为它会Set自动删除重复的嵌套项目.
通用方式:
public static <E> int uniqueCount(final List<E> list) {
return new HashSet<E>(list).size();
}
Run Code Online (Sandbox Code Playgroud)
确保以您需要的方式实现嵌套类型的equals/hashCode方法.