Skip to main content

Posts

Showing posts from May, 2021

How to call 3rd party API in Java using HttpURLConnection | Get and Post Request

Implementing GET and POST Request for calling third party API in Java   As a developer, often times we need to call third party APIs for getting or saving data. We can simply call GET request for getting data and POST request for save data or if needed to send parameters in body section.  In this article, we seen how we can use GET and POST request for calling third party Rest APIs. In java, we can simply call third party API using HttpURLConnection class and get response accordingly.  We can use following methods in HttpURLConnection class. GET POST HEAD OPTIONS PUT DELETE TRACE So lets see how we can call third party API. i am using https://jsonplaceholder.typicode.com   to make GET and POST calls. This website provides free API endpoints. GET request : Get request simply returns data (response). GET request does not accepts any body section data. See sample of GET requests : https://example.com/data  https://example.com/data/1 Calling GET request is easy. Bec...

How to find Squares of a Sorted Array in Java | Programming Blog

Java Solution for Find Squares of a Sorted Array | LeetCode Problem Given an integer array nums sorted in non-decreasing order, return an array of the squares of each number sorted in non-decreasing order . Example 1: Input: nums = [-4, -1, 0, 3, 10] Output: [0, 1, 9, 16, 100] Explanation: After squaring,      the array becomes [16, 1, 0, 9, 100]. After sorting, it becomes [0, 1, 9, 16, 100]. Example 2: Input: nums = [-7, -3, 2, 3, 11] Output: [4, 9, 9, 49, 121] Explanation: After squaring,      the array becomes [49, 9, 4, 9, 121]. After sorting, it becomes [ 4, 9, 9, 49, 121 ]. So lets see 3 solutions of this problem. Lets see how we can achieve O(n) time complexity.  Solution 1 : Using new array class Solution {     public int[] sortedSquares(int[] array) {                   int arrayLength = array.length;         int[] so...

How to find All Disappeared Numbers in an Java Array | Java LeetCode Solution

Find All Numbers that are not present in Java Array from 1 to n Problem Description : Given an array nums of n integers where nums[i] is in the range [1, n], return an array of all the integers in the range [1, n] that do not appear in nums array. Example 1 : Input: nums = [1, 3, 5, 1, 3] Output: [2, 4] Example 2 : Input: nums = [2, 4, 2, 4] Output: [1, 3] Example 3 : Input: nums = [4, 3, 2, 7, 8, 2, 3, 1] Output: [5, 6] Example 4 : Input: nums = [1, 1] Output: [2] In given problem, we have to return List of Integers that are not present in given array range [1 to n]. Lets see solution and its explanation step by step. Solution 1 : Using in-place array (Without use if extra space) import java.util.ArrayList; import java.util.List;  class Solution {     public List<Integer> findDisappearedNumbers(int[] nums) {                  List<Integer> list = new ArrayList<Integer>(); ...

Third Maximum Number LeetCode solution in Java | Programming Blog

How to find Third Maximum Number in given Java array? Problem Description : Given integer array nums , return the third maximum number in this array . If the third maximum does not exist, return the maximum number. Example 1 : Input: nums = [3,2,1] Output: 1 Explanation: The third maximum is 1. Example 2 : Input: nums = [1,2] Output: 2 Explanation: The third maximum does not exist, so the maximum (2) is returned instead. Example 3 : Input: nums = [2,2,3,1] Output: 1 Explanation: Note that the third maximum here means the third maximum distinct number. Both numbers with value 2 are both considered as second maximum. In this problem, we have to find 3rd maximum number in given array. If 3rd maximum is not present in array then we have to return maximum number that is present in array. So lets jump on solution and explanation. Solution 1 : Using Arrays.sort() class Solution {     public int thirdMax(int[] nums) {           // Sort array...

Height Checker LeetCode solution in Java | Programming Tutorials

How to Compare two arrays and Check both values are matched or not in Java Problem Description : A school is trying to take an annual photo of all the students. The students are asked to stand in a single file line in non-decreasing order by height. Let this ordering be represented by the integer array expected where expected[i] is the expected height of the i th student in line. You are given an integer array heights representing the current order that the students are standing in. Each heights[i] is the height of the i th student in line ( 0-indexed ). Return the number of indices where heights[i] != expected[i] . Example 1 : Input: heights = [1,1,4,2,1,3] Output: 3 Explanation: heights: [1,1, 4 ,2, 1 , 3 ] expected: [1,1, 1 ,2, 3 , 4 ] Indices 2, 4, and 5 do not match. Example 2 : Input: heights = [5,1,2,3,4] Output: 5 Explanation: heights: [ 5 , 1 , 2 , 3 , 4 ] expected: [ 1 , 2 , 3 , 4 , 5 ] All indices do not match. Example 3 : Input: heights = [1,2,3,4,5] ...

Supplier in Java 8 with Examples | get() method

What is supplier in Java 8? As a name suggest, Supplier is a Functional Interface which does not take any arguments but produces a value. It is part of java.util.function package and introduces in Java 8. Suppliers are useful when we don’t need to supply any value and obtain a result at the same time. Supplier is contains only one abstract method : get() Return type is T - the type of results supplied by this supplier. Lest see simple example of supplier interface. Example 1 : Print random number using Supplier Interface import java.util.function.Supplier; public class SupplierDemo {     public static void main(String[] args) {         Supplier<Double> supplier1 = () -> Math.random(); Supplier<Integer> supplier2 = () -> (int)(Math.random()*10); System.out.println("Random value from 0 to 1 : "+ supplier1.get()); System.out.println("Random value from 0 to 1 : "+ supplier1.get()); System.out.println("Random value ...

BiPredicate in Java 8 with examples | test(), and(), or() and negate() methods

What is BiPredicate? BiPredicate is same as  Predicate , but it accepts two arguments. BiPredicate is Functional interface which accepts two arguments and return boolean value. BiPredicate contains following methods :  test() : abstarct method and() : default method or() : default method negate() : default method So lets see examples of BiPredciate Example 1 : Check even number using BiPredicate import java.util.function.BiPredicate; public class BiPredicateDemo {     public static void main(String[] args) {                 BiPredicate<Integer, Integer> isEven = (number1, number2) -> {             return (number1 + number2) % 2 == 0;         };                 System.out.println(isEven.test(5, 6));         System.out.println(isEven...

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