Velvet Star Monitor

Standout celebrity highlights with iconic style.

general

Cannot understand why this method is unresolved

Writer Andrew Mclaughlin

I am studying Spring with Spring In Action book, i have the code from the book where it works fine:

@Slf4j
@Controller
@RequestMapping("/design")
public class DesignTacoController { @GetMapping public String showDesignForm(Model model) { List<Ingredient> ingredients = Arrays.asList( new Ingredient("FLTO", "Flour Tortilla", Type.WRAP), new Ingredient("COTO", "Corn Tortilla", Type.WRAP), new Ingredient("GRBF", "Ground Beef", Type.PROTEIN), ); Type[] types = Ingredient.Type.values(); for (Type type : types) { model.addAttribute(type.toString().toLowerCase(), filterByType(ingredients, type)); } model.addAttribute("design", new Taco()); return "design"; }
} 

but when i typed it in my IDEA it says that method filterByType cannot be resolved, but there is no such problem in the book, and no any comment about this problem. I am new in the Spring, tried google a lot, but couldn't find any information about this problem and it source. Could you help me with this problem, a cannot move further because of it.screenshot from IDEA

5

2 Answers

It looks like this book just contains an error, it does not list the filterByType() method. It's not a Spring method. Here you go:

private List<Ingredient> filterByType(List<Ingredient> ingredients, Type type) { return ingredients.stream() .filter(x -> x.getType().equals(type)) .collect(Collectors.toList());
}

Source: Manning Publications

2

If you do not prefer readability:

 Arrays.stream(types).forEach(a -> model.addAttribute(a.toString().toLowerCase(Locale.ROOT), ingredients.stream().filter(i -> a.equals(i.getType())).collect(Collectors.toList())));

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy