II have been playing with the lambda expressions and they have all involved working with objects. There are also some classes in the SDK that have been created for working with primitives.
I tested some of the primitive classes supported here :
I tested some of the primitive classes supported here :
BooleanSupplier booleanSupplier = () -> Boolean.FALSE;The output is as below:
// will always return the value as false
System.out.println(booleanSupplier.getAsBoolean());
DoubleBinaryOperator sumDouble = (a, b) -> (a + b);
System.out.println(2.56 + " + " + 13.45 + " = " + sumDouble.applyAsDouble(2.56, 13.45));
final int[] val = new int[1];
IntConsumer numberSink = num -> val[0] = num;
numberSink.accept(1); // val[1] = 1
numberSink.accept(2);// val[1] = 2
numberSink.accept(3);// val[1] = 3
LongFunction<String> longToString = l -> l + "";
System.out.println(longToString.apply(189L));
LongPredicate longPredicate = l -> l != 0;
System.out.println("A non zero number ? " + longPredicate.test(134l));
false
2.56 + 13.45 = 16.009999999999998
189
A non zero number ? true
- There is a BooleanSupplier class that supplies boolean values. We also have int,long and double versions of the same.
- There is a DoubleBinaryOperator that is summing two double primitives. We also have int and long versions of the same.
- We created an IntConsumer which simply consumes all passed int values. I also observed a double and long version of the class.
- We used a LongFunction that takes as input long value and transforms it into a different type. We have double and int versions of it.
- Last I created a LongPredicate that is used to run tests on long values. We also have int and double versions of these.
DoubleToIntFunction dblToIntFunction1 = l -> ((int)(l*100)) +2;There are similar classes for converting from primitive long and int to double value. And for converting primitive double and int to long values.
DoubleToIntFunction dblToIntFunction2 = l -> ((int)(l)*100) +2;
System.out.println(dblToIntFunction1.applyAsInt(10.98));//1100
System.out.println(dblToIntFunction2.applyAsInt(10.98));//1002
IntUnaryOperator intSquarer = i -> i * i;
System.out.println("Square of 10 is " + intSquarer.applyAsInt(10)); // 100
List<String> contents = new ArrayList<>();
ObjIntConsumer<String> inserter = (term, times) ->
{
for (int i = 0; i < times; i++) {
contents.add(term);
}
};
inserter.accept("Test", 3);// add the string 3 times to the list
ToIntBiFunction<String, Short> computeLength = (str, shortNo) -> str.length() + shortNo;
System.out.println("The value is " + computeLength.applyAsInt("Hello ", newShort((short) 25)));
- The IntUnaryOperator will be used to apply a transformation on an int value to return an int value.
- The ObjIntConsumer consumes a Object and a primitive int value. Similary there are ObjDoubleConsumer and ObjLongConsumer for double and long primitive values respectively.
- The BiFunction interface has also got an int, long and double variotion where the function transforms the input arguments into primitive values.