【发布时间】:2020-11-08 11:24:44
【问题描述】:
如何将事物列表添加到集合中?
当我执行 set.addAll 时出现错误
必填类型:集合
提供类型:列表
public static Set<List<Food>> getAllMealPlans(Set<Food> foods, int numMeals) {
Set<List<Food>> set = new HashSet<>();
List<Food> aList = new ArrayList<Food>(foods);
List<Food> sortedList = aList.stream().sorted(Comparator.comparing(a -> a.meal)).collect(Collectors.toList());
set.addAll(sortedList);
【问题讨论】:
-
这里的类型有点混乱.. 你是在 Set
- > 还是 Set
之后。如果是前者,那么你想要 set.add(list),如果是后者,那么 set.addAll(list) (并更正集合的类型)。