Java 8 Lambda Sort With Variable Method Reference

32U

I have a statement:

searchResults.sort(Comparator.comparing(WCCTableRowData::getD));

where getD is an accessor in the class WCCTableRowData and searchResults is a list of WCCTableRowData. The WCCTableRowData class has accessors from getA through getZ. I need to be able to set the sort field on the fly from a passed in variable. Is there an elegant way to do this or will I need a series of if statements or similar?

UPDATE 1 Unfortunately, neither approach in the accepted answer worked though I think in general the direction is correct. With approach 2 I get:

Intelli Sense

With approach 1, row.getField does not pick up the getField method in WCCTableRowData class and I get similar "does not conform to upper bound(s)" error. I think the error is saying that WCCTableRowData class has to implement Comparable?

John Kugelman

One way is to add a method in WCCTableRowData that can be given a field name and returns the value of that field.

class WCCTableRowData {
    Comparable<?> getField(String name) { ... }
}

String name = "C";
searchResults.sort(Comparator.comparing(row -> row.getField(name)));

If you don't want to modify the class, then you could set up an external map.

Map<String, Function<WCCTableRowData, Comparable<?>>> getters = new HashMap<>();
getters.put("A", WCCTableRowData::getA);
getters.put("B", WCCTableRowData::getB);
getters.put("C", WCCTableRowData::getC);

String name = "C";
searchResults.sort(Comparator.comparing(getters.get(name)));

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

How is method reference syntax working in Java 8

From Java

Is method reference caching a good idea in Java 8?

From Java

How to get the MethodInfo of a Java 8 method reference?

From Dev

Java 8 method reference to static void method

From Dev

Why lambda expressions in Java 8 requires variables used inside it to use "final" modifier, but not when using method reference?

From Dev

Java 8 Method reference with generic types

From Dev

Java 8 method reference unhandled exception

From Dev

Java 8: Is it possible to assign a method reference to a variable?

From Dev

Invoking toString via method reference in Java 8

From Dev

Java 8 chained method reference?

From Dev

Java 8 ambiguous method reference for generic class

From Dev

How to write Java 8 method reference in IntelliJ

From Dev

Java 8: Lambda with variable arguments

From Dev

Java 8: Reference to [method] is ambiguous

From Dev

Why does this Java 8 method reference compile?

From Dev

Method reference for FileFilter Java 8

From Dev

Java Lambda method reference not working

From Dev

java8: method reference from another method reference

From Dev

C# equivalent to Java 8 "method reference"

From Dev

Method reference in Java 8

From Dev

External argument to method reference in Java 8

From Dev

Mapping Java 8 Stream to elements' method reference

From Dev

Variable method reference in java 8

From Dev

Can pass reference variable pointing to Lambda in Java

From Dev

"new" Keyword In Java Lambda Method Reference

From Dev

Method reference in Java 8

From Dev

Replace this lambda with a method reference. (sonar.java.source not set. Assuming 8 or greater.)

From Dev

How to transform this lambda code to method reference in Java?

From Dev

Is Java 8 instance method reference an equivalent to lambda call? In my case it is NOT

Related Related

  1. 1

    How is method reference syntax working in Java 8

  2. 2

    Is method reference caching a good idea in Java 8?

  3. 3

    How to get the MethodInfo of a Java 8 method reference?

  4. 4

    Java 8 method reference to static void method

  5. 5

    Why lambda expressions in Java 8 requires variables used inside it to use "final" modifier, but not when using method reference?

  6. 6

    Java 8 Method reference with generic types

  7. 7

    Java 8 method reference unhandled exception

  8. 8

    Java 8: Is it possible to assign a method reference to a variable?

  9. 9

    Invoking toString via method reference in Java 8

  10. 10

    Java 8 chained method reference?

  11. 11

    Java 8 ambiguous method reference for generic class

  12. 12

    How to write Java 8 method reference in IntelliJ

  13. 13

    Java 8: Lambda with variable arguments

  14. 14

    Java 8: Reference to [method] is ambiguous

  15. 15

    Why does this Java 8 method reference compile?

  16. 16

    Method reference for FileFilter Java 8

  17. 17

    Java Lambda method reference not working

  18. 18

    java8: method reference from another method reference

  19. 19

    C# equivalent to Java 8 "method reference"

  20. 20

    Method reference in Java 8

  21. 21

    External argument to method reference in Java 8

  22. 22

    Mapping Java 8 Stream to elements' method reference

  23. 23

    Variable method reference in java 8

  24. 24

    Can pass reference variable pointing to Lambda in Java

  25. 25

    "new" Keyword In Java Lambda Method Reference

  26. 26

    Method reference in Java 8

  27. 27

    Replace this lambda with a method reference. (sonar.java.source not set. Assuming 8 or greater.)

  28. 28

    How to transform this lambda code to method reference in Java?

  29. 29

    Is Java 8 instance method reference an equivalent to lambda call? In my case it is NOT

HotTag

Archive