Java Lambda to Method

98 views Asked by At

Given that I know little and nothing about java, exactly like my English,

I have a problem, I have this line of code and I had to delete the lambdas.

return articles (args -> {}, queryDef);

I used Android Studio, (Alt Enter) and it creates me

private com.shopify.buy3.Storefront.BlogQuery.ArticlesArgumentsDefinition GetArticlesArgumentsDefinition () {

         return args -> {};

     } 

always with lambdas.

How can I convert args -> {} in order to eliminate them?

Thank you

EDIT:

        public BlogQuery articles(ArticleConnectionQueryDefinition queryDef) {
        return articles(args -> {}, queryDef);
    }

    /**
    * List of the blog's articles.
    */
    public BlogQuery articles(ArticlesArgumentsDefinition argsDef, ArticleConnectionQueryDefinition queryDef) {
        startField("articles");

        ArticlesArguments args = new ArticlesArguments(_queryBuilder);
        argsDef.define(args);
        ArticlesArguments.end(args);

        _queryBuilder.append('{');
        queryDef.define(new ArticleConnectionQuery(_queryBuilder));
        _queryBuilder.append('}');

        return this;
    }
2

There are 2 answers

0
erickson On

You could define a conventional implementation of ArticlesArgumentsDefinition, though I don't recommend it. Why do you have to get rid of the lambda?

return articles(new ArticlesArgumentsDefinition() {
  @Override
  public void define(ArticlesArguments args) { }
});

Here, I've used an anonymous class, which is the closest pre-lambda equivalent to a real lambda, but this could be any kind of class.

0
JockX On

You are looking for "Replace lambda with anonymous class" intention available when doing Alt+Enter on -> symbol. It should replace your lambda with something like this:

return articles(new BlogQuery.ArticlesArgumentsDefinition() {
    @Override
    public void define(ArticlesArguments args) {
       // body of lambda
    }
});

I think you should be able to do Alt+Enter -> Replace lambda with anonymous class -> Fix all... to do it in one go.