Search This Blog

Wednesday 4 November 2015

Primitives and Lambdas

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 :
BooleanSupplier booleanSupplier = () -> Boolean.FALSE;
// 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));
The output is as below:
false
2.56 + 13.45 = 16.009999999999998
189
A non zero number ? true
  1. There is a BooleanSupplier class that supplies boolean values. We also have int,long and double versions of the same.
  2. There is a DoubleBinaryOperator that is summing two double primitives. We also have int and long versions of the same.
  3. We created an IntConsumer which simply consumes all passed int values. I also observed a double and long version of the class.
  4. 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.
  5. Last I created a LongPredicate that is used to run tests on long values. We also have int and double versions of these.
There are also some converters that have been provided, eg for double class we have the DoubleToInt  and DoubleToLong interfaces.
DoubleToIntFunction dblToIntFunction1 = l -> ((int)(l*100)) +2;
DoubleToIntFunction dblToIntFunction2 = l -> ((int)(l)*100) +2;
System.out.println(dblToIntFunction1.applyAsInt(10.98));//1100
System.out.println(dblToIntFunction2.applyAsInt(10.98));//1002
There are similar classes for converting from primitive long and  int to double value. And for converting primitive double and int to long values.
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 ", new Short((short) 25)));
  1. The IntUnaryOperator will be used to apply a transformation on an int value to return an int value. 
  2. The ObjIntConsumer consumes a Object and a primitive int value. Similary there are ObjDoubleConsumer and ObjLongConsumer for double and long primitive values respectively.
  3. The BiFunction interface has also got an int, long and double variotion where the function transforms the input arguments into primitive values.

No comments:

Post a Comment