Skip to main content

Posts

Showing posts from July, 2020

Stable or Unstable Number Java Program

In this post we’ll see a Java program to check whether the number is stable or not. A stable number is a number in which each digit occurs the same number of time. For example 1010, 3355, 2020, 794479, in these numbers you can see that the frequency of each digit is same in the number. An unstable number is a number in which each digit does not occur the same number of time, for example 1011, 3356, 404, 794419. Check number Stable or Unstable Java Program Create an array of length 10 for digits 0 to 9. This array is used to store frequency of each digit. In the while loop frequency of each digit is calculated and stored in the array. When an integer array is created by default it will have 0 as value for each index. For some of the indices you will have values after calculating frequencies other will remain as 0. For example if 4422 is passed as number index 2 and 4 of the array will have value 2 all the other indices will have value 0. In the for loop you discard all

UnaryOperator Functional Interface Java Examples

In this post we’ll see examples of Java java.util.function.UnaryOperator functional interface. UnaryOperator functional interface represents an operation on a single operand returning a result of the same type as its operand. UnaryOperator extends java.util.function.Function interface and provides behavior for the case where the operand and result are of the same type. Since it extends Function so inherits all the methods of the Function interface- andThen(Function<? super R, ? extends V> after) - It is a default interface method which takes another Function as argument and returns a composed Function that performs, in sequence, first the operation of the calling Function followed by the after operation. compose(Function<? super V, ? extends T> before) - It is a default method in Function interface which takes another Function as argument and returns a composed Function that performs, in sequence, first the before operation then the operation of the calling Fu

Abstract Class in Java

Abstract class in Java is a class that is declared using abstract keyword . Abstract class can have regular methods (methods with method body) as well as abstract methods (methods without method body and having abstract specifier). Table of contents General form of abstract class in Java When is abstract class needed Abstract class in Java example Constraints when using Abstract class in Java Abstract class with no abstract method Abstract Classes Compared to Interfaces General form of abstract class in Java abstract class className{ .. .. } When is abstract class needed When designing your classes for an application you may have a situation where a super class can provide a general structure and common implementation for few methods while leaving implementation of methods to the sub classes where implementation differs. The methods that have to be implemented by the sub classes are specified as abstract methods in the super class. A sub clas

Interface in Java With Examples

Interface in Java helps to fully abstract the implementation of the class. Interfaces act as a contract for the class, detailing what methods can be invoked by any outside entity without actually providing what methods should do. The class implementing an interface has to provide the behavior (implement the methods defined in interface). Table of contents How is interface created Syntax of interface in Java Creating and implementing an interface in Java Features of interface in Java Interface extends another interface Nested interfaces How is interface created in Java An interface in Java is created by using the interface keyword . All fields in an interface are automatically public, static, and final . Methods in an interface have no body (only method signature), are public and abstract by default. Note that Java 8 onward interfaces can have default methods and static methods and Java 9 onward private methods in interface are also permitted. In t

ArrayBlockingQueue in Java With Examples

ArrayBlockingQueue is a bounded blocking queue which internally uses an array to store elements. This queue orders elements FIFO (first-in-first-out). The head of the queue is the element that has been on the queue the longest time. The tail of the queue is that element that has been on the queue the shortest time. Insertion of the new elements happens at the tail of the queue, and the queue retrieval operations obtain elements at the head of the queue. ArrayBlockingQueue in Java is an implementation of BlockingQueue interface and is part of java.util.concurrent package. Bounded blocking queue ArrayBlockingQueue in Java is a bounded blocking queue which internally uses a fixed-sized array to hold elements. Once created, capacity of the queue cannot be changed. Attempts to put an element into a full queue will result in the operation blocking; attempts to take an element from an empty queue will similarly block. ArrayBlockingQueue is thread-safe Java ArrayBlockingQueue i