Skip to main content

Posts

Showing posts with the label Programming

Predicate In Java 8 with examples | test(), and(), or(), negate() methods

  What is Predicate in Java 8?  Predicate in general meaning is a statement about something that is either true or false. In programming, predicates represent single argument functions that return a boolean value. Predicate is a functional interface representing a single argument function that returns a boolean value. If we want conditional check in our code, we can use Predicate.   Predicates in Java are implemented with interfaces. Predicate<T> is a generic functional interface representing a single argument function that returns a boolean value. It is located in the java.util.function package. It contains a Test (T t) method that evaluates the predicate on the given argument. public interface Predicate<T> {      public boolean test(T t);           }      test is only abstract method present in Predicate interface. It is return boolean value based on given condition....

Consumer and Biconsumer In Java 8 | accept and andThen() method

What is Consumer and BiConsumer in Java? Consumer in Java Consumer is Functional Interface which takes arguments only and return nothing. Consumer is Interface in Java Programming language which introduced in Java 8 and it is part of java.util.function. As a name suggest, it only consumes not return anything. means it consumes value and perform required operations. @FunctionalInterface public interface Consumer<T> {   void accept(T t); } Consumer only contains two methods : one is abstract and another is default. void accept (T t)  default Consumer<T> andThen(Consumer<? super T> after) You can see return type of accept method is void means it does not return anything. Consumer can be used in all contexts where an object needs to be consumed, Like taken as input, and some operation is to be performed on the object without returning any result. Since Consumer is a functional interface, hence it can be used as the assignment target for a lambda expression or a...

Functional Interface In Java 8 with Examples - @FunctionalInterface

What is Functional Interface in Java 8? | What is @FunctionalInterface In Java In Java 8 Functional Interface concept introduced. I will explain what is functional interface and why we use? So lets start.. What is Functional Interface?   functional Interface is Interface that only have one Abstract  method.  For Qualify functional Interface in java, class only have one Abstract Method.   Any Interface is called Functional Interface if it contains only one Abstract method in it.   functional interfaces are called SAM (Single Abstract Method) since they have only single abstract method and can have any default and static method.  One of the major benefits of functional interface is we can use lambda expressions to instantiate them. Learn about Lambda expression first so you have clear understanding about it. Lambda expression and its use cases Following Code of Functional Interface. @FunctionalInterface public interface FunctionalInterface { public sta...

Java 8 Stream Collect() Method with examples | Programming Blog

Java 8 Stream Api Collect Method What is collect() method in Java 8? Collect() method of java8 stream api is terminal method. It performs a mutable reduction operation on the element of the stream. A mutable reduction operation process the stream elements and then accumulate it into a mutable result container. Once the elements are processed, a combining function merges all the result containers to create the result. Learn Difference between Intermediate and Terminal Operations :- Intermediate and Terminal Operations in Java 8 Stream Api   There is two variant of collect method. collect(Supplier supplier, BiConsumer accumulator, BiConsumer combiner) collect(Collector collector) Where, supplier : It is a function that creates a new mutable result container. For the parallel execution, this function may be called multiple times and it must return a fresh value each time. accumulator : it is a stateless function that must fold an element into a result container. combiner : It is...

Java 8 Stream Map() Method With Examples | Programming Blog

Map() Method in Java 8 Stream Api What is Map() Function in Java 8 | Java 8 Map method In java 8, map is also very useful method like filter() , collect() and  reduce() method. What is map() function in Java? Returns a stream consisting of the results of applying the given function to the element of this function. map() function is in java.util.stream package. In simple word, map() method is used to transform one object into another by applying a function. So map() method takes a function as an argument. Stream.map(Function arg) map() function provides intermediate operation. Intermediate operation are lazy operation. it does not apply until you apply terminal operations like, forEach() or collect() methods. map() function invoked on a stream instance and after finish their processing, they give stream instance as output. Before java 8, if we want to convert List of String into upperCase or lowerCase or String of list to int then we have to write for loop but using map() ...

Java 8 Reduce() Method with examples | Stream API

Java 8 Reduce Method with Examples - Java 8 Stream API What is reduce() method in Java 8? Many times, we need to perform operations where a stream reduces to single resultant value, for example, maximum, minimum, sum, minus, product,  divide, etc. Reducing is the repeated process of combining all elements. sum(), min(), max(), count() etc. are some examples of reduce operations. reduce() explicitly asks you to specify how to reduce the data that made it through the stream.  Java 8 reduce method is terminal method. Stream.reduce() method combine elements of stream and produce single result. There is 3 variant of reduce method reduce(BinaryOperator accumulator) reduce(T indentity, BinaryOperator accumulator) reduce(U indentity, BiFunction accumulator, BinaryOperator combiner)  BinaryOperator - Represents an operation upon two operands of the same type, producing a result of the same type as the operands. This is a specialization of BiFunction for the case where the ...

Object Oriented Programming (OOP) concept in Java | Programming blog

OOPs Concept in java programming OOP :- OOP refers as Object Oriented Programming. OOP is a concept that is based on objects. As a name suggests it is Object oriented. Java is Object oriented programming language.  There are mainly four concepts in java :- Abstraction Encapsulation Inheritance Polymorphism Let's discuss all of above one by one. 1. Abstraction :- In simple term, Abstraction means hiding complexity and showing essential details to the user. Means using the things without knowing background details about it.  Abstraction is the technique of hiding implementation. Like, we use smartphone very efficiently and easy way, we don't need background details how it works. we simply use. Why should we use Abstraction? Reduce complexity Force users to extend implementation rather than modify Support cross platform by changing the implementation per platform In Java, we can use abstraction in two ways :- Using Abstract class Using Interface Learn more about Abstract class an...