Category: java
-
Record Patterns in Java
Pattern Matching is a new area where Java has been introducing quite a few changes recently. It started with instanceof operator. Previously in Java , if you had to access an object whose instance type you didnt know prior, you had to use instanceof operator this way: You had to do a cast operation as…
-
How to share data across multiple components for a single request in a Spring Boot application?
Let’s say that you make a REST API request to a spring boot web application. There is a security requirement that you need to log the user id across all the method calls in the application for this particular request. Something like this: So , if your application flow starts from a controller class and…
-
The Real Benefits of Java Lambdas
Java 8 introduced the concept of Lambda expressions. They provide a concise way to represent functions as objects. A lambda expression is essentially an anonymous function that can be treated as an object and passed around as an argument to methods or stored in variables. Here is an example: In this example, the MathOperation interface…
-
Variables – Java vs Javascript
Variables are critical to any programming language. It is where you store temporary data. Not all data needs to be stored in the database, whatever doesn’t need to be stored in the database and is needed temporarily during your program execution are stored in variables. Variables in Javascript differ from variables in Java in quite…
-
Java vs Javascript – Say Hello World!
One of the complaints about Java programming language is that it is verbose. Imagine you are new to programming. And you want to learn to program. The first thing most of us traditionally do is to print “Hello World!” . And to do this in Java you need to go through a lot of ordeal:…
-
try catch block in Java vs Javascript
In both Java and JavaScript, the try-catch block is used to handle exceptions that may occur in the code. The basic structure of a try-catch block in both languages is similar: a block of code is put in a “try” block, and any exceptions that are thrown in that block are caught and handled in…
-
Delete Node in a Linked List given only the node
Problem: You are given a node to delete from a linked list. But you are not given the head node. Delete it in O(1) time and O(1) space complexity. Constraints: For example, Given the linked list : 1 -> 4 -> 7 -> 8 And the node 7 to be deleted The output should be…
-
Odd Even Linked List
Problem: Given the head of a linked list , reorder the linked list so that the elements in odd indices are grouped together followed by the elements in event indices. The first index is assumed to be 1 (odd) and the second index even and so on. For example, For the below input: 1 ->…
-
Find first and last position of element in sorted array
Problem: Given a sorted integer array find the first and last position of given target element For example, If given array is [ 1,3,6,6,7,9] and target is 6 Then output is [2,3] which represents the first and last index position of the element 6 For the same input if the target is 8 Then the…
-
How to implement Trie Data Structure?
Trie is a datastructure useful to store set of words. It is a tree with a lot of children , one for each letter of the alphabets. So if you want to store lower case English letter words each node can have unto 26 child nodes. Every word is represented by as many nodes as…