Skip to main content

Posts

Showing posts from March, 2022

How to create Immutable Class or Object in Java? | Benefits of immutable class

Create Immutable Class in Java | Advantages of immutable class in java What is immutable class or object? In simple term, once the object of the class created its fields cannot be changed or modified. Like once Student object is created, it can not be changed. In java, all wrapper classes are immutable. like Boolean, String, Integer, Double, Long, Float. Why we need Immutable class? or Advantages of Immutable class When we does not want to change its fields after created. It is Thread safe. These objects are good for use as hash key (HashMap) Reference of immutable objects can be cached. How to create Immutable Class in Java? We can create Immutable class using simple following steps : Make class Final Make class fields Private and Final Do not write Setter method for fields   Create all arguments constructor Using above steps we can create Immutable class in Java. So lets go on code... // Final class final class Company {          // Private fiel...

Java Dequeue HackerRank Solution with Example

HackerRank Solution for Java Dequeue Problem Problem Description : In this problem, you are given N integers. You need to find the maximum number of unique integers among all the possible contiguous subarrays of size M. Sample 1 : Input : 6 3 5 3 5 2 3 2 Output : 3 Explanation : In the sample testcase, there are 4 subarrays of contiguous numbers. a1 = [5 3 5] - Has unique 2 numbers. a2 = [3 5 2] - Has unique 3 numbers. a3 = [5 2 3] - Has unique 3 numbers. a4 = [2 3 2] - Has unique 2 numbers. in these subarrays, there are 2, 3, 3, 2 unique numbers, respectively. The maximum amount of unique numbers among all possible contiguous subarrays is 3. Sample 2 : Input : 6 3 5 9 8 6 8 7 4 2 3 3 Output : 5 Explanation : a1 = [5 9 8 6 8] - Has unique 4 numbers. a2 = [9 8 6 8 7] - Has unique 4 numbers. a3 = [8 6 8 7 4] - Has unique 4 numbers. a4 = [6 8 7 4 2] - Has unique 5 numbers. a5 = [8 7 4 2 3] - Has unique 5 numbers. a6 = [7 4 2 3 3] - Has unique 4 numbers.  in these subarrays, there are ...

Write a program in Java to check if a character is an uppercase vowel or lowercase vowel or not

Java program to check given alphabet is an uppercase or lowercase vowel or not In this program, we just have to find given user input character is uppercase or lowercase vowel or not. We will seen two solution approach for this problem Using List Using Switch Case Example : Input : 'A' Output : Character is UpperCase Vowel  Input : 'e' Output : Character is LowerCase Vowel Input : 'j' Output : Character is Not a Vowel Solution Approach : We will use ArrayList for store vowels and check if given character is uppercase or lowercase vowel or not.  So lets jump on code. Solution 1 : Using List import java.util.ArrayList; import java.util.List; import java.util.Scanner; public class CheckVowelsCase {     public static void main(String[] args) {         Scanner sc = new Scanner(System.in);         System.out.println("Enter Character");         char ch = sc.next().charAt(0); ...

How to Upload and Download file from Database in Java servlet

Save and Download byte data from MySQL and PostgreSQL using Java Servlet In this article, we will seen how to convert file to byte array and save into Postgres and Mysql database. After that we also seen how to download saved file in database using file download dialog box. 1. Upload or Save file into database For saving file into database, we will perform following steps : First we will get inputStream from HttpServletRequest and store into InputStream. Convert InputStream to byte array. Store byte array to particular column into database. Using following approach we can Convert InputStream to Byte array in java 1. Using Apache Commons IO library           InputStream content = request.getInputStream();         byte[] bytes = IOUtils.toByteArray(content);         content.close(); 2. Using ByteArrayOutputStream           ByteArrayOutputStream buffer = new ByteArrayOutp...

Queue interface in Java with examples | Which are implementation classes of Queue?

Queue interface with ArrayDeque, LinkedList, PriorityQueue Implementation classes Queue Interface (java.util.queue) Queue is child interface of  Collection Framework in Java.  Queue provides First In First Out (FIFO) order on elements. How to use Queue interface? As Queue is interface, we must have class to implements queue. See below code for Queue implementation classes. Queue priorityQueue = new PriorityQueue(); Queue arrayDqueue = new ArrayDeque(); Queue linkedList = new LinkedList();  LinkedList class is also implementation of List Interface . Methods of Queue interface : add() - Insert the element in the queue. Throws IllegalStateException if no space available. offer() - Insert the element in the queue. element() - Return head of queue. Throws an exception if queue is empty. peek() - Return head of queue. Return null if queue is empty. poll() - Remove and return head of queue. Throws and exception of queue is empty. remove() - Remove and returns head of queue. Ret...