0

There is the following code

Grid.Column<Person> firstNameColumn = grid.addColumn(Person::getFirstName).setHeader("First Name");
Grid.Column<Person> lastNameColumn = grid.addColumn(Person::getLastName).setHeader("Last Name");

I would like to rewrite it without ::. The reason is that I need to use a function like getLastName to have two parameters like getLastName(firstname, age).

Can you provide me a solution or how to search about it?

Thank you

Thorbjørn Ravn Andersen
  • 73,784
  • 33
  • 194
  • 347
nick
  • 229
  • 2
  • 9
  • 4
    `....addColumn(person -> person.getLastName(firstName, age))...` --- Having a method `getLastName` with parameters `firstName` and `age` seems suspicous. I would suggest to thorougly review this change. – Turing85 May 23 '21 at 07:11
  • I am just giving an example. I would like for this example to rewrite it the pre 8 version of java – nick May 23 '21 at 07:14
  • 4
    That is not what you originally asked for. If you want to migrate the code to pre-java8, I recommend [edit]ing the post and changing the actual question. --- May I ask why you want to migrate the code back to pre-java8? – Turing85 May 23 '21 at 07:20
  • 1
    Double colon is a concise shorthand for a lambda expression which you will see often in Streams and similar. – Thorbjørn Ravn Andersen May 23 '21 at 09:32
  • 1
    Does this answer your question? [:: (double colon) operator in Java 8](https://stackoverflow.com/questions/20001427/double-colon-operator-in-java-8) – cfrick May 23 '21 at 13:02

2 Answers2

4

The pre-Java 8 version would conventionally use an anonymous class like this:

grid.addColumn(new ValueProvider<Person, String>() {
    @Override
    public String apply(Person person) {
        return person.getLastName();
    }
})

But you don't need that to customize the method call. An ordinary lambda will do:

grid.addColumn(person -> person.getLastName(foo))

As you see, the latter is much more concise. Vaadin's API has been designed to embrace Java 8 features such as functional references, lambdas and streams. JDK8 and newer is also a requirement for modern Vaadin versions to function.

shmosel
  • 49,289
  • 6
  • 73
  • 138
0

You can use lambda

grid.addColumn(person -> person.getLastName(person.getFirstName(),person.getAge());

For more info, go to this site

Saravanan
  • 566
  • 5
  • 13