Skip to main content

Posts

Showing posts from January, 2021

Java Generics HackerRank Solution | Programming Tutorial

HackerRank Solution for Java Generics Problem description :- Generic methods are a very efficient way to handle multiple datatypes using a single method. This problem will test your knowledge on Java Generic methods. Let's say you have an integer array and a string array. You have to write a single method printArray that can print all the elements of both arrays. The method should be able to accept both integer arrays or string arrays. You are given code in the editor. Complete the code so that it prints the following lines: Solution :- import java.lang.reflect.Method; class Printer {   <T> void printArray(T[] array) {        for (T value : array) {            System.out.println(value);        }    } } public class JavaGenerics {     public static void main( String args[] ) {         Printer myPrinter...

Java Hashset HackerRank Solution | Programming Blog

Java Hashset HackerRank Solution with Explanation   Problem Statement :- In computer science, a set is an abstract data type that can store certain values, without any particular order, and no repeated values. {1,2,3} is an example of a set, but {1,2,2} is not a set. Today you will learn how to use sets in java by solving this problem. You are given n pairs of strings. Two pairs (a,b) and (c,d) are identical if a = c and b = d. That also implies (a,b) is not same as (b,a). After taking each pair as input, you need to print number of unique pairs you currently have. See full problem description in HackerRank Website :- https://www.hackerrank.com/challenges/java-hashset/problem Let's see solution of problem. import java.util.HashSet; import java.util.Scanner; public class Solution {     public static void main(String[] args) {         Scanner s = new Scanner(System.in);         System.out.println("Enter tot...

What is Exception and Exception handling in Java | Programming Blog

What is Exception? Exception is event, which occurs during the execution of a program, that disrupts the normal flow of program's instructions. In simple word, Exception is unwanted event that interrupts the normal flow of program. When Exception is occurs during program execution it gets terminated. Why Exception occurs? There may be several reason for occurs the Exception. Like, When you try to assign value into array index that is not present. When you divide any number using 0. File is not present at given location. During casting. like String to Integer. So, there are so many reasons for occurs the Exception. See example of java.lang.ArithmeticException: / by zero Exception public class SimpleClass{     public static void main(String[] args) {                 int a = 10/0;     } } Output :- Exception in thread "main" java.lang.ArithmeticException: / by zero     at SimpleClass.ma...

Difference Between throw and throws in Java with Examples

  What is throw and throw keywords in Java? What is Difference between throw and throws? In java, throw and throws keyword are used for Exception Declaration. throw and throws keywords are mainly used for handle the Exception.  What is Exception and Exception handling in Java?   Exception and Exception handling in Java So let's see how both are different from each other. 1. throw As a name suggest, throw is used to throw the Exception in java. Using throw keyword we can throw exception explicitly. throw keyword used inside of method. It is used when it is required to throw an Exception logically.  We can throw only one Exception at a time. Syntax of throw :-      throw someThrowableObject Lets see example of throw keyword so you will get idea about it. Example 1 :- throw exception using throw keyword public class ExceptionDemo {     public static void main(String[] args) {         method();    ...

Top Interview Questions and Answers on Static Keyword | Programming Blog

Java Static keyword most asked Interview questions and answers (1) What means Static in Java? Ans :- Java static keyword is used to create a Class level variable in java. static variables and methods are part of the class, not the instances of the class.  Static is keyword in java and mainly used for memory management. Static keyword used in following :- Static class Static variable Static method Static Block    Learn more about these follow this link :- Static keyword in Java (2) Why main method is static in Java? Ans :- Because we can call static method without instantiated, means we does not have to create object of that. The public static void keyword mean the JVM (Java Virtual Machine) interpreter can call the program's main method to start without creating object of the class, and the program does not return data to the jvm interpreter when it ends. Read stackoverflow answer for more in depth details. https://stackoverflow.com/questions/146576/why-is-the-java...

Java Map HackerRank Solution | Java Solution

HackerRank Java Map Problem Solution In Java Problem Statement :- You are given a phone book that consists of people's names and their phone number. After that you will be given some person's name as query. For each query, print the phone number of that person. Input Format The first line will have an integer n denoting the number of entries in the phone book. Each entry consists of two lines: a name and the corresponding phone number. After these, there will be some queries. Each query will contain a person's name. Read the queries until end-of-file. Constraints: A person's name consists of only lower-case English letters and it may be in the format 'first-name last-name' or in the format 'first-name'. Each phone number has exactly 8 digits without any leading zeros. Output Format For each case, print "Not found" if the person has no entry in the phone book. Otherwise, print the person's name and phone number. See sample output...

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 ...

Armstrong Number In Java | Programming Blog

Armstrong Number in Java With N number   Armstrong Number in Java With N number What is Armstrong Number? When any positive number is equal to the sum of its own digits raised to the power of the number of digits.  In simple, Armstrong number is the sum of power of all digits in given number. 0, 1, 153, 370, 371, 407, 1634, 8208, 9474, 54748 are some example of Armstrong number. Lets understand with example so you understand properly. Example :- 370 = (3*3*3) + (7*7*7) + (0*0*0) = 370 1634 = (1*1*1*1) + (6*6*6*6) + (3*3*3*3) + (4*4*4*4) Example 1 :- Armstrong number for only 3 digits import java.util.Scanner; public class ArmstrongExample {     public static void main(String[] args) {                 Scanner sc = new Scanner(System.in);                 System.out.println("Enter Number");         int number = sc.ne...