Skip to main content

Java 8 - Functions - Function Types - Primitive Args Functions

  • There are cases when the performance cost of auto boxing is significant.
  • When dealing with a lot of primitive values, avoid auto boxing by creating lambdas that use primitives.
  • The examples shown below use int primitive type, but there are corresponding versions for the double and long primitive types, just replace Int with Double or Long.

public class FunctionsWithPrimitiveArgs {

     function to convert int to any type
    IntFunction<String> intToString = i -> Integer.toString(i);

     function to convert any type to int
    ToIntFunction<String> parseInt = str -> Integer.valueOf(str);

     function that takes an int to return a boolean i.e a Predicate accepting int input
    IntPredicate isEven = i -> i % 2 == 0;

     function that takes two inputs to return an int
    ToIntBiFunction<String,String> maxLength = (left, right) -> Math.max(left.length(), right.length());

     function that takes an int to return nothing
    IntConsumer printInt = i ->;

     function that takes as input an object and an int to return nothing
    ObjIntConsumer<String> printParsedIntWithRadix = (str,radix) ->"output is: " + Integer.parseInt(str,radix));

     function that takes no input and returns an int
    IntSupplier randomInt = () -> new Random().nextInt();

     function that acts on an int to return an int
    IntUnaryOperator negateInt = i -> -1 * i;

     function that acts on two ints to return an int
    IntBinaryOperator multiplyInt = (x,y) -> x*y;

     function that takes an int input to return a double
    IntToDoubleFunction intAsDouble = i -> Integer.valueOf(i).doubleValue();

     function that takes a double to return an int output
    DoubleToIntFunction doubleAsInt = d -> Double.valueOf(d).intValue();

     function that takes an in int input to return a long output
    IntToLongFunction intAsLong = i -> Integer.valueOf(i).longValue();

     function that takes a long input to return an int output
    LongToIntFunction longAsInt = x -> Long.valueOf(x).intValue();



Popular posts from this blog

Java 8 - Streams - Introduction

Streams provide extended functionality to collections. All collection operations except operations on Map can be extended using streams.Streams can either be sequential or parallel.Operations on sequential streams are performed on a single thread.Operations on parallel streams are performed concurrently on multiple threads, and so are faster than sequential streams (although item order is affected). NOTE that parallel streams are CPU intensive and should be used with care as it can lead to starvation of other processes in your application.Operations on streams are grouped into intermediate (transformations) and terminal (actions).Stream processing is triggered only by terminal operations, while intermediate operations return a new stream.
an empty stream
Stream emptyStream = Stream.empty();

creating a stream from array
Stream<Integer> streamFromArray = Stream.of(new Integer[]{1,2,3});
Stream<Integer> streamFromArray2 = Integer[]{1,2,3});

creating a stream…

Java 8 - Streams - Parallel Stream

Operations on parallel streams are performed concurrently on multiple threads and so are faster than sequential streams (although item order is affected)However, operations on parallel streams can be ordered using 'stream.forEachOrdered'
parallel Integer Stream
Stream<Integer> parallelIntegerStream = new ArrayList<>(Arrays.asList(1,2,3)).parallelStream();

another parallel Integer Stream
Stream<Integer> parallelIntegerStream2 = IntStream.range(0, 10).parallel().boxed(); 

parallel String Stream
Stream<String> parallelStringStream = Arrays.asList("ab","acd","db","dab","ea").parallelStream();


Consumer<Stream> orderedParallelStreamConsumer = stream -> stream.forEachOrdered(item ->;




Consumer<Stream<String>> unorderedParalle…

Java 8 - Streams - Actions - Reduce

reduce operation on stream provides a way to accumulate the result of an operation which traverses all items in the stream.The traversal starts from the leftmost item in the stream.There are three implementations of the reduce operation.The first implementation uses the first item in the stream as initial accumulator.The second implementation expects an initial accumulator input.The third implementation provides a way to return an accumulator which has a different type from the stream items. This is similar to foldLeft in other functional languages e.g. Scala.
int stream one to three IntStream oneToThree = IntStream.of(1, 2, 3);

int stream one to two
IntStream oneToTwo = IntStream.of(1, 2);

string stream
Stream stringStream = Stream.of("a", "ab", "abc", "abcd");

USAGE"first implementation - reducing oneToThree Integer stream by addition: " + oneToThree.reduce((acc, item) -> acc+item …