Found 7442 Articles for Java

Difference between Collection.stream().forEach() and Collection.forEach() in Java

Himanshu shriv
Updated on 21-Jan-2020 08:02:10

934 Views

Collection.stream().forEach() and Collection.forEach() both are used to iterate over collection. Collection.forEach()  uses the collection’s iterator. Most of the collections doesn’t allow the structurally modification while iterating over them. If any element add or remove while iteration they will immediately throw concurrent modification exception. If Collection.forEach() is iterating over the synchronized collection then they will lock the segment of the collection and hold it across all the calls. Collection.stream().forEach() is also used for iterating the collection but it first convert the collection to the stream and then iterate over the stream of the collection therefore the processing order is undefined. It also throws ... Read More

How to implement IntBinaryOperator using lambda expression in Java?

raja
Updated on 14-Jul-2020 09:22:47

441 Views

IntBinaryOperator is a functional interface in Java 8 from java.util.function package. This interface expects two parameters of type int as input and produces an int type result. IntBinaryOperator can be used as an assignment target for a lambda expression or method reference. It contains only one abstract method: applyAsInt().Syntax@FunctionalInterface public interface IntBinaryOperator {  int applyAsInt(int left, int right) }Exampleimport java.util.function.*; public class IntBinaryOperatorTest {    public static void main(String[] args) {       IntBinaryOperator test1 = (a, b) -> a + b; // lambda expression       System.out.println("Addition of two parameters: " + test1.applyAsInt(10, 20));       IntFunction test2 = new IntFunction() {     ... Read More

How to implement Function interface with lambda expression in Java?

raja
Updated on 14-Jul-2020 09:16:29

3K+ Views

Function interface is a functional interface from java.util.function package. This interface expects one argument as input and produces a result. Function interface can be used as an assignment target for a lambda expression or method reference. It contains one abstract method: apply(), two default methods: andThen() and compose() and one static method: identity().Syntax@FunctionalInterface public interface Function {  R apply(T t); }Exampleimport java.util.function.Function; public class FunctionTest {    public static void main(String[] args) {       Function f1 = i -> i*4;   // lambda       System.out.println(f1.apply(3));       Function f2 = i -> i+4; // lambda   ... Read More

How To Install Apache Maven on Ubuntu

Sharon Christine
Updated on 01-Mar-2024 13:56:26

283 Views

Apache Maven is a software project management and comprehension tool. Based on the concept of a project object model (POM), Maven can manage a project’s build, reporting and documentation from a central piece of information. This article explains about how to install apache maven on Ubuntu.To install apache maven, it should require pre-installed java on Ubuntu. To verify java version, use the following command –$ java -versionThe sample output should be like this –openjdk version "1.8.0_111" OpenJDK Runtime Environment (build 1.8.0_111-8u111-b14-2ubuntu0.16.04.2-b14) OpenJDK 64-Bit Server VM (build 25.111-b14, mixed mode)If you wants to install java on Ubuntu, read this articleTo install ... Read More

How to implement DoubleSupplier using lambda and method reference in Java?

raja
Updated on 14-Jul-2020 09:12:01

504 Views

DoubleSupplier interface is a built-in functional interface defined in java.util.function package. This functional interface doesn’t expect any input but produces a double-valued output. DoubleSupplier interface can be used as an assignment target for lambda expression and method reference. This interface contains only one abstract method: getAsDouble().Syntax@FunctionalInterface public interface DoubleSupplier {    double getAsDouble(); }Example of Lambda Expressionimport java.util.concurrent.ThreadLocalRandom; import java.util.function.DoubleSupplier; public class DoubleSupplierLambdaTest {    public static void main(String args[]) {       DoubleSupplier getRandomDouble = () -> {    // lambda expression          double doubleVal = ThreadLocalRandom.current().nextDouble(0000, 9999);          return Math.round(doubleVal);       };       ... Read More

How to create our own/custom functional interface in Java?

raja
Updated on 14-Jul-2020 08:50:12

15K+ Views

The functional interface is a simple interface with only one abstract method. A lambda expression can be used through a functional interface in Java 8. We can declare our own/custom functional interface by defining the Single Abstract Method (SAM) in an interface.Syntaxinterface CustomInterface {    // abtstact method }Example@FunctionalInterface interface CustomFunctionalInterface {    void display(); } public class FunctionInterfaceLambdaTest {    public static void main(String args[]) {       // Using Anonymous inner class       CustomFunctionalInterface test1 = new CustomFunctionalInterface() {          public void display() {             System.out.println("Display using Anonymous inner class");         ... Read More

How to implement ObjLongConsumer interface using lambda expression in Java?

raja
Updated on 14-Jul-2020 08:42:51

303 Views

ObjLongConsumer is a functional interface from java.util.function package. This interface accepts an object-valued and long-valued argument as input but doesn't produce any output. ObjLongConsumer can be used as an assignment target for lambda expression and method reference and contains only one abstract method: accept().Syntax@FunctionalInterface public interface ObjLongConsumer {  void accept(T t, long value) }Exampleimport java.util.function.ObjLongConsumer; public class ObjLongConsumerTest {    public static void main(String[] args) {       ObjLongConsumer olc = (employee, number) -> {     // lambda expression          if(employee != null) {             System.out.println("Employee Name: " + employee.getEmpName());         ... Read More

How to implement LongFunction using lambda and method reference in Java?

raja
Updated on 14-Jul-2020 08:10:54

316 Views

LongFunction is an in-built functional interface defined in java.util.function package. This functional interface expects a long-valued parameter as input and produces a result. LongFunction interface can be used as an assignment target for a lambda expression or method reference. It contains only one abstract method: apply().Syntax@FunctionalInterface public interface LongFunction {  R apply(long value) }Exampleimport java.util.function.LongFunction; public class LongFunctionTest {    public static void main(String[] args) {       LongFunction function1 = (long i) -> { // lambda expression          return i + i;       };       System.out.println("Using Lambda Expression: " + function1.apply(10));       LongFunction ... Read More

XDM – The Download Manager for Linux that ramps up Your Speed to 500%

karthikeya Boyini
Updated on 17-Jan-2020 13:01:06

328 Views

Xtreme download supervisor (xdman) is an effective download supervisor for Linux, which is developed in Java programing language. It can increase download speeds of upto 500% and is an alternative for the windows IDM (Internet Download Manager). It is compatible with many popular internet browsers such as Firefox, Chrome, Opera.Before installing Xtreme Download supervisor, Check if Java is installed or not available by typing java -version in command line.$ java -versionThe sample output should be like this –openjdk version "1.8.0_91" OpenJDK Runtime Environment (build 1.8.0_91-8u91-b14-0ubuntu4~16.04.1-b14) OpenJDK 64-Bit Server VM (build 25.91-b14, mixed mode)Installing Xtreme Download Manager in LinuxTo put the ... Read More

What are the rules for a functional interface in Java?

raja
Updated on 14-Jul-2020 08:12:26

2K+ Views

A functional interface is a special kind of interface with exactly one abstract method in which lambda expression parameters and return types are matched. It provides target types for lambda expressions and method references.Rules for a functional interfaceA functional interface must have exactly one abstract method.A functional interface has any number of default methods because they are not abstract and implementation already provided by the same.A functional interface declares an abstract method overriding one of the public methods from java.lang.Object still considered as functional interface. The reason is any implementation class to this interface can have implementation for this abstract method either from ... Read More

Advertisements