I have 2 questions.
1.
I want to convert Lambda into a Java function, but do not know how to do it.
//GET Method For REST Application
public Topic getTopics(String id){
return topics.stream().filter(t -> t.getId().equals(id)).findFirst().get();
}
The topics is a list defined as:
private List<Topic> topics = new ArrayList<Topic>(
Arrays.asList(
new Topic("1","Spring Framework","Spring")
)
);
Class Topic is defined as:
public class Topic {
public String id;
public String name;
public String description;
public Topic() {}
public Topic(String id, String name, String description) {
super();
this.id = id;
this.name = name;
this.description = description;
}
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;
}
public String getId(){
return this.id;
}
}
2.
I have 1 more question regarding the code. It is about filter function. Why is it that only t can be used as predicate? Hovering over t gives me this definition
Topic t - project_path.getTopics(...).() -> {...} Predicate.test(Topic)
tis simply a variable you give for each instance in the topic stream. You can changet -> t.getId().equals(id)intofoo -> foo.getId().equals(id)if you want to.