In the tutorial do this:

TopicController

@RestController public class TopicController { @RequestMapping("/topics") public List<Topic> getAllTopics(){ return Arrays.asList( new Topic("spring", "Spring Framework", "Spring Framework Description"), new Topic("java", "Core Java", "Core Java Description"), new Topic("javascript", "Javascript", "Javascript Description") ); } } 

Topic

 package com.in.controller; public class Topic { private String id; private String name; private String description; public Topic(){ } public Topic(String id, String name, String description) { this.id = id; this.name = name; this.description = description; } public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getDescription() { return description; } public void setDescription(String description) { this.description = description; } } 

Why do we do like this through Arrays.asList ()?
Where does Arrays come from?
So that it can call asList ()?
Where is he?))

  return Arrays.asList(new Topic("spring", "Spring Framework", "Spring Framework Description"), new Topic("java", "Core Java", "Core Java Description"), new Topic("javascript", "Javascript", "Javascript Description") 

As I understand Arrays.asList, we represent an array in the form of a list, but I do not see an array in this example.

But if you remove Arrays.asList and leave it simple:

  @RequestMapping("/topics") public List<Topic> getAllTopics(){ return new Topic("spring", "Spring Framework", "Spring Framework Description"); new Topic("java", "Core Java", "Core Java Description"), new Topic("javascript", "Javascript", "Javascript Description") ); 

That compiler swears and says:

enter image description here

  • Have you tried to search for information on Arrays.asList ? What exactly is not clear to you with this method? - default locale
  • @defaultlocale I pointed out all the questions above - Ivan Petrovchenko
  • Your questions raise questions :) You seem to know what classes and methods are and even create your own. What is the difficulty with this method? Have you read the documentation for it (link in the comments above)? - default locale

1 answer 1

Before Java 9, it was not possible to create a list with values ​​at once (except for the double brace initialization method, which is doubtful for many reasons), so I had to either resort to cumbersome construction

 List<Topic> topics = new ArrayList<>(); topics.add(new Topic("spring", "Spring Framework", "Spring Framework Description")); topics.add(new Topic("java", "Core Java", "Core Java Description")); topics.add(new Topic("javascript", "Javascript", "Javascript Description")); return topics; 

Or to misuse the utilitarian class Arrays , designed for array operations:

 return Arrays.asList( new Topic("spring", "Spring Framework", "Spring Framework Description"), new Topic("java", "Core Java", "Core Java Description"), new Topic("javascript", "Javascript", "Javascript Description") ); 

The static method asList takes an array of variable-length arguments , constructs a list of them and returns it. Starting with Java 9, you can do without this crutch:

 return List.of( new Topic("spring", "Spring Framework", "Spring Framework Description"), new Topic("java", "Core Java", "Core Java Description"), new Topic("javascript", "Javascript", "Javascript Description") );