HTTP Methods , Restful API Methods , URI examples

GET METHOD :

HTTP GET http://oms.com/orders/1
HTTP GET http://oms.com/orders

POST METHOD :

HTTP POST http://oms.com/orders/
HTTP POST http://oms.com/users/1/products

The difference between the POST and PUT APIs can be observed in request URIs. POST requests are made on resource collections, whereas PUT requests are made on an individual resource.

PUT METHOD :
HTTP PUT http://oms.com/orders/1/gg@gmail.com
HTTP PUT http://oms.com/users/2/orders/2

DELETE METHOD :

HTTP DELETE http://oms.com/orders/2
HTTP DELETE http://oms.com/users/3

PATCH METHOD :

PATCH is used to partial update on a  resource not like PUT which is used for entire resource update.

HTTP PATCH http://oms.com/orders/1/users/2/hh@gmail.com



What is the difference between PUT and POST methods in Rest API ?

PUT method is idempotent. Which means it produces same result . How many times you call that mention the result will be same.

Where Post method will produce different result when ever you call that function.

Post method is used to create a new resource.

PUT method is used in general to update a resource .



LeetCode - Excel Sheet Column Title

Given a positive integer, return its corresponding column title as appear in an Excel sheet.
For example:
    1 -> A
    2 -> B
    3 -> C
    ...
    26 -> Z
    27 -> AA
    28 -> AB 
    ...
Example 1:
Input: 1
Output: "A"
Example 2:
Input: 28
Output: "AB"
Example 3:
Input: 701
Output: "ZY"
Java Solution :
class Solution {
    public String convertToTitle(int n) {
        
        StringBuilder sb = new StringBuilder();
        
        while(n > 0){
            n--;
            char ch = (char) (n%26 + 'A');
            n = n/26;
            sb.append(ch);
        }
        sb.reverse();
        return sb.toString();
        
    }
}

Top 10 Books to Learn Java

For Freshers / Beginners :

1. Head First Java Current Edition : 2nd  by Kathy Sierra

https://www.amazon.com/Head-First-Java-Kathy-Sierra/dp/0596009208

2. Clean Code by Robert C. Martin

https://www.amazon.com/Clean-Code-Handbook-Software-Craftsmanship/dp/0132350882

3. Java the Complete Reference Eleventh Edition  by Herbert Schildt

https://www.amazon.com/Java-Complete-Reference-Eleventh/dp/1260440230

4. Java Concurrency in Practice by Brain Goetz

https://www.amazon.com/Java-Concurrency-Practice-Brian-Goetz/dp/0321349601

5.  Effective Java by Joshua Bloch

https://www.amazon.com/Effective-Java-2nd-Joshua-Bloch/dp/0321356683


For Experienced Java Professionals:


6. Java A Beginner's Guide Eigth Edition by Herbert Schildt

https://www.amazon.com/Java-Beginners-Eighth-Herbert-Schildt/dp/1260440214

7.  Java Performance Definitive Guide  by Scott Oaks

https://www.amazon.com/Java-Performance-Definitive-Guide-Getting/dp/1449358454

8. Spring in action by Craig Walls 5th edition

https://www.amazon.com/Spring-Action-Craig-Walls/dp/1617294942

9.  Test Driven : TDD and Acceptance TDD for Java Developers 1st Edition

https://www.amazon.com/Test-Driven-Acceptance-Java-Developers/dp/1932394850

10. Mastering Java Machine Learning



https://www.amazon.com/Mastering-Java-Machine-Learning-architectures-ebook/dp/B01KOG6SW8














LeetCode - Moving Average from Data Stream

Given a stream of integers and a window size, calculate the moving average of all integers in the sliding window.
Example:
MovingAverage m = new MovingAverage(3);
m.next(1) = 1
m.next(10) = (1 + 10) / 2
m.next(3) = (1 + 10 + 3) / 3
m.next(5) = (10 + 3 + 5) / 3

Java Solution:
class MovingAverage {

    /** Initialize your data structure here. */
    private Queue<Integer> q;
    private double sum;
    private int maxSize;
    public MovingAverage(Integer size) {
        q = new LinkedList<Integer>();
        maxSize = size;
        sum = 0;
    }
    
    public double next(int val) {
        if(q.size() != maxSize){
            q.offer(val);
            sum+=val;
            return sum / q.size();
        }else{
            sum-=q.peek(); // subtracting from sum before removing the first element
            q.poll();
            q.offer(val);
            sum+=val;
            return sum / q.size();
        }
    }
}

/**
 * Your MovingAverage object will be instantiated and called as such:
 * MovingAverage obj = new MovingAverage(size);
 * double param_1 = obj.next(val);
 */

Java - Concurrency / Threads


What is a Process in Concurrency / Threads ?

A process is a Unit of Execution that has it's own memory space.

When ever we run an application , we are starting a new process . 

Example : Running a Java FX application.
                 Running any Java console applications .  Nothing but we are starting a new process .

Each instance  of Java Virtual machine runs as a process (For most of them).


When ever two applications or process's are running , they have their own memory space or heap .

Applications or process's never share the memory / heap between them.

Thread :

What is a Thread ?

Thread is a Unit of Execution with in a Process . 

A process can have multiple threads .  Default in Java we have at least one thread called main thread.

For UI we have JavaFX application thread.

Our code runs in main thread or JavaFX app thread (UI) or threads that are explicitly provided by us.

Every Java application / process has multiple threads like memory management , Input / Output operations (I/O operations) .  Which we don't create , provided by java itself.

Unlike process's , threads can share files and memory of it's process / application.

In addition to process's memory thread has it's own memory called thread stack which can't be shared to other thread. Only that particular thread can access it's thread stack.


                                           Application runs as a  Process -> Heap Memory

                                                            Thread -> Thread Stack

                                                                          ->  Thread 1
                                                           Process   ->  Thread 2
                                                                           ->   Thread 3


Can't we run our entire application in one single thread as how we are running our application as a single process ?

Consider a scenario that you a fetching some data from an application . Where the application has several databases and also some information that it fetches from internet .

If we execute entire data fetching from all databases and internet in one main thread it will take more time or more latency because it act's in a linear fashion . User's of application may think app is down or got stuck by the time response is provided by main thread.

Using multiple threads , each thread will work on different databases and one thread will fetch data from internet and in the mean while main thread can provide status updates to user.

Finally all information can be collated and end result can be  displayed to User.

Example : you are watching a movie and parallelly downloading a file . Both are two tasks / threads running in parallel.


Java provides thread related classes to achieve concurrency .

Threads execution will depend on JVM and operating system on which they are running.











Featured Post

H1B Visa Stamping at US Consulate

  H1B Visa Stamping at US Consulate If you are outside of the US, you need to apply for US Visa at a US Consulate or a US Embassy and get H1...