I've created a Test class which should help you with your question, you've got several problems in your code.
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
public class Test {
public static void main(String[] args) {
new Test().test();
}
void test() {
Set<Food> foods = new HashSet<>();
foods.add(new Food("a", Meal.BREAKFAST));
foods.add(new Food("b", Meal.BREAKFAST));
foods.add(new Food("c", Meal.LUNCH));
Set<List<Food>> mealPlans = getAllMealPlans(foods, 0);
mealPlans.forEach(f -> System.out.println(Arrays.toString(f.toArray())));
System.out.println("--------------------");
//I think what you might want is a Map from Meal to List of Foods
Map<Meal, List<Food>> mealPlans2 = getAllMealPlansMap(foods);
mealPlans2.entrySet().forEach(f ->{
System.out.println("Meal: "+f.getKey());
System.out.println(Arrays.toString(f.getValue().toArray()));
System.out.println("");
});
}
public static Set<List<Food>> getAllMealPlans(Set<Food> foods, int numMeals) { //numMeals unused?
Set<List<Food>> set = new HashSet<>();
Iterator<Food> iterator = foods.iterator(); // only instantiate the iterator once(!)
while (iterator.hasNext()) {
Food food = iterator.next(); //only call .next once(!)
List<Food> aList = new ArrayList<Food>();
if (food.meal == Meal.BREAKFAST) {
//set.remove(foods.iterator().next()); //why remove it from the set?.. and by foods.iterator() you create a NEW Iterator which is starting at index 0 again .. and check that you are calling .remove with the correct type
aList.add(food);
if (food.meal != Meal.LUNCH) {
//
}
}
if(!aList.isEmpty()) {//only add the list if its not empty
set.add(aList);
}
}
return set;
}
public static Map<Meal,List<Food>> getAllMealPlansMap(Set<Food> foods) {
return foods.stream().collect(Collectors.groupingBy(f -> f.meal));
}
enum Meal {
LUNCH, BREAKFAST
}
class Food {
String name;
Meal meal;
public Food(String name, Meal meal) {
super();
this.name = name;
this.meal = meal;
}
@Override
public String toString() {
return name + " " + meal;
}
}
}