3

In Java 8 I want to create something that returns an argument or creates an instance if the argument is null.

I could do this by creating a static method or a UnaryOperator. Are the following approaches technically the same or are there technical differences that I should be aware of with either approach:

Static Method

static Cat initOrReturn(Cat c) {
    if (c==null) {
        return new Cat();
    }
    return c;
}

Function

UnaryOperator<Cat> initOrReturn = c -> {
    if (c==null) {
        return new Cat();
    }
    return c;
}
4
  • 1
    Re, "...could be used in ... streams." And would that help you to solve your problem? Or would it help some other software developer with whom you are working to solve a problem? You are asking developers for their opinions, but those opinions are going to depend on what problem you are trying to solve. Commented Dec 11, 2017 at 16:48
  • P.S., instead of "function," you might want to say "functional object." Commented Dec 11, 2017 at 16:50
  • Is c -> c==null? new Cat(): c really worth a discussion about different ways of reusability? Commented Dec 19, 2017 at 10:10
  • The question was really about the technical considerations of using a static method vs a function. Commented Dec 20, 2017 at 11:04

2 Answers 2

3

First your code has syntax error, in the second block first line between c and { there should be a ->.

The second one creates an anonynous object, the first one only creates a static method.
So they're not the same.

Also, static methods can be used in stream API.
If you have:

class A {
  static Object a(Object x) { return x; /* replace with your code */ }
}

You can:

xxxList().stream().map(A::a)

Creating a method is often considered dirty, because it's globally visible.
It's recommended to use lambda expressions without declaring a variable.

Sign up to request clarification or add additional context in comments.

8 Comments

So it sounds like the only technical consideration when deciding which approach to take, is that the Function creates an additional object. If this is a concern, then go with the static method? Otherwise, they both are easy to read, achieve the objective and can be used in streams.
Creating a method is often considered dirty, because it's global visible.
Would you mind elaborating pls. Don't both approaches allow visibility restriction by the addition of visibility modifiers e.g. private etc?
Even it's private, it's accessible to the whole class. While a lambda expression can be a local variable, you can restrict its scope inside a method, or a for/while/if/else/dowhile block.
That's really helpful. Thank you.
|
0

You can think about function as a "value" - something that can be stored to variable, and passed around.

This "value" can be used as e.g. method parameter to (during runtime) dynamically change part of method implementation.

Take a look at this basic example. Hope that can illustrate idea:

static Number functionsUsageExample(Integer someValue, UnaryOperator<Number> unaryOperator) {
    if (someValue == 1) {
        //do something
    }
    Number result = unaryOperator.apply(someValue); // dynamically apply supplied implementation
    // do something else
    return result;
}

public static void main(String[] args) {
    UnaryOperator<Number> add = i -> i.doubleValue() + 20;
    UnaryOperator<Number> multiply = i -> i.intValue() * 3;

    var additionResult = functionsUsageExample(1, add);
    var multiplicationResult = functionsUsageExample(1, multiply);
    
    //additionResult value is:           21.0
    //multiplicationResult value is:     3
}

Function can be also used as a 'helper methods' stored inside method block. This way you will not corrupt class scope with method that is used only in one place.

Comments

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.