Java LongSupplier Interface with examples

 161 total views,  1 views today

Welcome to Java 8 Series. In this tutorial, you will learn about the Java LongSupplier Interface with examples.

LongSupplier is a Functional interface introduced as part of Java 8. It is available in java.util.function package.

What is the use of LongSupplier interface?

This interface is mainly used for just getting a long-valued result which is not based on the input. This interface doesn’t have any input arguments. Consider a case where we need to write a lambda expression which has no input argument and return a long-valued result. It just supplies the result of the long-valued type.

Example

public class StaticReference {
	public static void main(String[] args) {
		Operation ops = () -> {
			return 10;
		};
		long result = ops.getValue();
		System.out.println(result);
	}

	interface Operation {
		long getValue();
	}
}

When you look at the above example, we have a function interface with an abstract method which doesn’t have any input argument but returns a long value. So a lambda expression needs a target type which needs to be a functional interface and in this example, it is Operation interface.

Instead of creating our own function interface for this scenario, Java has provided us a LongSupplier interface. Let now see in-depth details on Java LongSupplier Interface with examples.

LongSupplier Interface

@FunctionalInterface
public interface LongSupplier

LongSupplier interface contains an operation that doesn’t have input argument and returns a long value

It is a functional interface and the functional method isgetAsLong().

Method description

long getAsLong()

Example of LongSupplier interface using lambda

public class StaticReference {
	public static void main(String[] args) {
		LongSupplier supplier = () -> {
			return 10;
		};
		long result = supplier.getAsLong();
		System.out.println(result);
	}
}

Example of LongSupplier interface using a Method reference

public class StaticReference {
	public static void main(String[] args) {
		LongSupplier supplier = StaticReference::get;
		double result = supplier.getAsLong();
		System.out.println(result);
	}

	static long get() {
		return 10;
	}
}

If you want to write a lambda expression with no arguments and returns a long-valued result, please use Java LongSupplier interface.

References

Java 8 LongSupplier official documentation

Thanks for reading. Please a comment and give us a thumbs up.

Tags :

About the Author

Rajasekar

Hey There, My name is Rajasekar and I am the author of this site. I hope you are liking my tutorials and references. Programming and learning new technologies are my passion. The ultimate idea of this site is to share my knowledge(I am still a learner :)) and help you out!. Please spread your words about us (staticreference.com) and give a thumbs up :) Feel free to contact me for any queries!.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.