2

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)

3
  • t is simply a variable you give for each instance in the topic stream. You can change t -> t.getId().equals(id) into foo -> foo.getId().equals(id) if you want to. Commented Jun 19, 2018 at 5:04
  • Always try to ask one question per post. Commented Jun 19, 2018 at 5:09
  • Thanks...I will keep in mind next time :) Commented Jun 19, 2018 at 5:30

2 Answers 2

3

That lambda in your filter combinator can be extracted into a variable (predicate):

Predicate<Topic> hasSameId = topic -> id.equals(topic.getId());

or into a method:

Boolean hasSameId(Topic topic) {
    return id.equals(topic.getId());
}

In either case it would be good practice though to pass id along by currying either version:

Function<String, Predicate<Topic>> hasSameId = id -> topic -> id.equals(topic.getId());

It can then be used like this:

topics.stream().filter(hasSameId.apply(id)).findFirst();
Sign up to request clarification or add additional context in comments.

Comments

1

You definitely need to learn more about JAVA 8 streams. Anyhow here is answer to your questions:

public Topic getTopics(String id) {
    return topics.stream().filter(t -> t.getId().equals(id)).findFirst().get();
}

is same as

public Topic getTopics(String id) {
     for (Topic curTopic:topics) {
          if (curTopic.getId().equals(id)) return curTopic;
     }
     return null;
}

Secondly, for your second question, you can use anything as formal parameter, so below will also work fine (see also Syntax of Lambda Expression):

public Topic getTopics(String id) {
    return topics.stream().filter(p -> p.getId().equals(id)).findFirst().get();
}

Hope that helps you :)

2 Comments

Not exactly same as, given that get() throws NoSuchElementException if nothing is found.
@Andreas The motive here is to give basic idea. All boundary scenarios can be amended as and when required.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.