0

I want to return double type value in the given Primitive Functional Interface:

  DoubleFunction<double> db1=(x)-> x+2;         
  System.out.println(db1.apply(23.6));

This gives me an error,but the below code is correct:

  DoubleFunction<String> db1=(x)-> x +" is now a String";         
  System.out.println(db1.apply(23.6));

I want to know why the generic type should be String? Can anyone give me an example which returns int/double in Primitive Functional Interface?

Pavneet_Singh
  • 36,884
  • 5
  • 53
  • 68

4 Answers4

4

Generic always works with class so use

DoubleFunction<Double>
//             ^ mean class , you can't use primitive 

For further reading Generics

Pavneet_Singh
  • 36,884
  • 5
  • 53
  • 68
1

In your case you probably want a DoubleUnaryOperator which only operates on primitives, whereas a DoubleFunction<Double> will box/unbox the values.

DoubleUnaryOperator db1= x-> x + 2; //note: no need for brackets
System.out.println(db1.applyAsDouble(23.6));
assylias
  • 321,522
  • 82
  • 660
  • 783
1

Specifically for double->double use DoubleUnaryOperator instead, but in general you can always create your own interface if there is no suitable one.

Inability to support primitives as generic parameters is the only reason DoubleFunction exists in the first place instead of just Function<double, SomeType>.

Alexey Romanov
  • 167,066
  • 35
  • 309
  • 487
  • 1
    Although it has been answered an year ago. But let me add few key points: 1. Java does not support primitive types for generics in Function But if you use wrapper classes like Integer Double Function that will be worst kind of programming practice. Because performance will be hampered by Autoboxing and autoUnboxing during. – Umar Tahir Nov 25 '19 at 11:00
  • 1
    2. In order to deal with this Java has provided us buit-in primitive type functional interfaces. For Function we have been given 15 different primitive Function interfaces. You can go through all of them by following this cheat sheet ( https://dzone.com/articles/cheatsheet-java-functional-interfaces ) – Umar Tahir Nov 25 '19 at 11:05
  • 1
    Apart from these 15 different primitive Function Interfaces we are also given UnaryOperator and BinaryOperator interfaces. These UnraryOperator and BinaryOperator Interfaces are child interfaces of Function and BiFunction interfaces. These UniaryOperator and BinaryOperator also have their primitive version of interfaces i.e. IntUniaryOperator DoubleUniaryOperator etc – Umar Tahir Nov 25 '19 at 11:11
  • Since Java SE-8 has covered almost all the scenarios for Function functional interfaces. You will almost never need to define your own custom Function interface. Seeing your secnario I recommend as recommended by accepted answer to use DoubleUnaryOperator. **But please keep in mind almost all of your scenarios will be covered by Builtin-primitive Function interfaces.You wont need to create your own** – Umar Tahir Nov 25 '19 at 11:16
  • @UmarTahir Except if you want to 1. use any primitive but `long`, `int` or `double`; 2. have a binary function or predicate with primitive arguments; etc. – Alexey Romanov Nov 25 '19 at 12:06
  • @AlexeyRomanov you are right for primitive types other then long, int double we should create new functional interface as implicit type casting can be cause of performance issue. But for predicates there exist IntPredicate, LongPredicate and DoublePredicate. – Umar Tahir Nov 25 '19 at 12:43
  • 1
    @UmarTahir binary in that comment refers to both function and predicate, we don't have anything for e.g. `(int, int)->boolean`. – Alexey Romanov Nov 25 '19 at 13:16
0

Try boxing it, i.e.

 DoubleFunction<Double> db1=(x)-> x+2;

as you were using the primitive double, not the Class Double

Dragonthoughts
  • 2,180
  • 8
  • 25
  • 28