<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Rajesh Mishra</title>
    <description>The latest articles on DEV Community by Rajesh Mishra (@rajesh1761).</description>
    <link>https://dev.to/rajesh1761</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F386291%2F3a705353-b889-4de6-8aac-ee5b91fcf935.png</url>
      <title>DEV Community: Rajesh Mishra</title>
      <link>https://dev.to/rajesh1761</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/rajesh1761"/>
    <language>en</language>
    <item>
      <title>Simplify JSON Data Like Never Before!</title>
      <dc:creator>Rajesh Mishra</dc:creator>
      <pubDate>Fri, 28 Mar 2025 20:03:21 +0000</pubDate>
      <link>https://dev.to/rajesh1761/simplify-json-data-like-never-before-2d1</link>
      <guid>https://dev.to/rajesh1761/simplify-json-data-like-never-before-2d1</guid>
      <description>&lt;p&gt;Are you tired of dealing with complex, unreadable JSON data? Say hello to &lt;a href="https://jsonvisualizer.com/" rel="noopener noreferrer"&gt;JSON Visualizer&lt;/a&gt; your go-to tool for formatting, analyzing, querying, transforming JSON.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Interactive Graphs – Convert JSON into easy-to-read visual structures.&lt;/li&gt;
&lt;li&gt;Smart Formatting – Beautify and optimize JSON instantly.&lt;/li&gt;
&lt;li&gt;Validation &amp;amp; Debugging – Identify errors in real time.&lt;/li&gt;
&lt;li&gt;Seamless Integration – Works smoothly across various formats and environments.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Whether you're a developer, data analyst, or just working with JSON files, this tool makes your life easier!&lt;/p&gt;

&lt;p&gt;Try it now &lt;a href="https://jsonvisualizer.com/" rel="noopener noreferrer"&gt;https://jsonvisualizer.com/&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Let us know how JSON Visualizer has helped you! Drop a comment or share your thoughts. &lt;/p&gt;

</description>
      <category>json</category>
      <category>datascience</category>
      <category>webdev</category>
      <category>deeplearning</category>
    </item>
    <item>
      <title>Building a Scalable Notification System: A Deep Dive into Design and Architecture</title>
      <dc:creator>Rajesh Mishra</dc:creator>
      <pubDate>Sun, 18 Aug 2024 20:49:13 +0000</pubDate>
      <link>https://dev.to/rajesh1761/building-a-scalable-notification-system-a-deep-dive-into-design-and-architecture-5c9i</link>
      <guid>https://dev.to/rajesh1761/building-a-scalable-notification-system-a-deep-dive-into-design-and-architecture-5c9i</guid>
      <description>&lt;p&gt;In today’s fast-paced digital world, ensuring timely and reliable communication with users is crucial for businesses. Whether it’s a transaction alert, a promotional email, or an SMS notification, the need for a system that can handle massive volumes of messages efficiently is more important than ever. Enter the Scalable Notification System—a robust architecture designed to manage and deliver notifications seamlessly across various channels. In this blog post, we’ll break down the components and workflow of this system to show you how it all comes together.&lt;/p&gt;

&lt;h4&gt;&lt;strong&gt;What is a Scalable Notification System?&lt;/strong&gt;&lt;/h4&gt;

&lt;p&gt;A Scalable Notification System is an architecture built to handle high volumes of notifications, ensuring that every message, whether critical or non-critical, reaches the end user through the appropriate channel—be it email, SMS, or social media. The system is designed to prioritize critical messages while ensuring non-critical ones are delivered without disrupting the flow of important communications.&lt;/p&gt;

&lt;h4&gt;&lt;strong&gt;Key Components of the System&lt;/strong&gt;&lt;/h4&gt;

&lt;ol&gt;
    &lt;li&gt;
&lt;strong&gt;Data Source (DB or File):&lt;/strong&gt; Every notification starts with data. The system pulls this data from a database or file, which serves as the source of information for the notifications. This data includes everything from transaction details to promotional content, and it’s fed directly into the system for processing.&lt;/li&gt;
    &lt;li&gt;
&lt;strong&gt;Scalable Queuing Node (SQN):&lt;/strong&gt; At the heart of the system lies the Scalable Queuing Node (SQN). Think of it as the traffic cop that directs messages into the right lanes—critical or non-critical. The SQN manages the flow of messages, categorizing them based on their urgency, and ensures that the system can handle varying loads by scaling up as needed.&lt;/li&gt;
    &lt;li&gt;
&lt;strong&gt;Handling Critical Messages (Transactional Messages):&lt;/strong&gt; Critical messages, like transaction alerts or reminders, are given top priority in this system. These messages are funneled into dedicated queues, where they are processed by specialized workers. This ensures they are sent out quickly and efficiently, minimizing any delays.&lt;/li&gt;
    &lt;li&gt;
&lt;strong&gt;Managing Non-Critical Messages (Promotional Messages):&lt;/strong&gt; Non-critical messages, such as promotional emails or marketing SMS, are handled separately. These messages don’t require immediate attention, allowing the system to process them in a way that doesn’t interfere with the delivery of critical notifications.&lt;/li&gt;
    &lt;li&gt;
&lt;strong&gt;Queues: The Backbone of Delivery&lt;/strong&gt; To manage the delivery of messages to various channels, the system uses different queues:
&lt;ul&gt;
    &lt;li&gt;
&lt;strong&gt;Social Media Queue:&lt;/strong&gt; For notifications that need to be posted on social media platforms.&lt;/li&gt;
    &lt;li&gt;
&lt;strong&gt;Email Queue:&lt;/strong&gt; Specifically for email notifications.&lt;/li&gt;
    &lt;li&gt;
&lt;strong&gt;SMS Queue:&lt;/strong&gt; For text messages that need to be sent out.&lt;/li&gt;
    &lt;li&gt;
&lt;strong&gt;Priority Queues:&lt;/strong&gt; Dedicated to handling the most critical messages that need to be sent out immediately. Each queue ensures that messages are processed in the correct order and delivered through the appropriate channels.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
    &lt;li&gt;
&lt;strong&gt;Sender API: The Final Step in Delivery&lt;/strong&gt; The Sender API is the bridge between the system and the end user. Once a message has been processed and queued, the Sender API takes over, delivering the notification to the user through the chosen channel. Whether it’s an email, a text message, or a social media update, the Sender API ensures that every notification reaches its destination.&lt;/li&gt;
    &lt;li&gt;
&lt;strong&gt;End User: The Ultimate Recipient&lt;/strong&gt; The end goal of the system is to ensure that notifications reach the end user without delay. The architecture is designed to deliver messages in a timely and efficient manner, ensuring that users receive the information they need when they need it.&lt;/li&gt;
&lt;/ol&gt;

&lt;h4&gt;&lt;strong&gt;Scalability and Reliability&lt;/strong&gt;&lt;/h4&gt;

&lt;p&gt;One of the standout features of this notification system is its scalability. As the volume of messages increases, the system can scale horizontally by adding more workers and resources. This ensures that performance remains consistent, even during peak times.&lt;/p&gt;

&lt;p&gt;By segregating critical and non-critical messages, the system ensures that important notifications are never delayed. The use of multiple queues and worker nodes allows the system to manage a large volume of messages efficiently, maintaining a high level of reliability.&lt;/p&gt;

&lt;h4&gt;&lt;strong&gt;How It All Works: The Operational Flow&lt;/strong&gt;&lt;/h4&gt;

&lt;ol&gt;
    &lt;li&gt;
&lt;strong&gt;Data Input:&lt;/strong&gt; The process begins with data input from a source (database or file). Messages are then categorized into critical and non-critical types.&lt;/li&gt;
    &lt;li&gt;
&lt;strong&gt;Message Categorization:&lt;/strong&gt; The SQN processes the incoming data, directing critical messages into priority queues and non-critical messages into other queues.&lt;/li&gt;
    &lt;li&gt;
&lt;strong&gt;Queue Processing:&lt;/strong&gt; Workers assigned to each queue process the messages in the order of priority, ensuring that critical messages are handled first.&lt;/li&gt;
    &lt;li&gt;
&lt;strong&gt;Message Delivery:&lt;/strong&gt; The Sender API retrieves messages from the queues and delivers them to the end user via the appropriate channels.&lt;/li&gt;
    &lt;li&gt;
&lt;strong&gt;User Reception:&lt;/strong&gt; Finally, the end user receives the notification through their preferred platform, whether it’s social media, email, or SMS.&lt;/li&gt;
&lt;/ol&gt;

&lt;h4&gt;&lt;strong&gt;Conclusion&lt;/strong&gt;&lt;/h4&gt;

&lt;p&gt;The Scalable Notification System is a powerful solution for managing high volumes of notifications. Its modular design allows it to scale effortlessly, making it an ideal choice for businesses that require reliable and efficient message delivery. Whether you’re sending critical alerts or promotional content, this system ensures that your messages reach the right people at the right time.&lt;/p&gt;

&lt;p&gt;Read more about &lt;a href="https://howtostartprogramming.in/tag/system-design/" rel="noopener noreferrer"&gt;system design&lt;/a&gt; on &lt;a href="https://howtostartprogramming.in/" rel="noopener noreferrer"&gt;howtostartprogramming.in&lt;/a&gt;&lt;/p&gt;

</description>
    </item>
    <item>
      <title>What is Solid Design Principle in Java?</title>
      <dc:creator>Rajesh Mishra</dc:creator>
      <pubDate>Sun, 11 Aug 2024 21:20:28 +0000</pubDate>
      <link>https://dev.to/rajesh1761/what-is-solid-design-principle-in-java-134j</link>
      <guid>https://dev.to/rajesh1761/what-is-solid-design-principle-in-java-134j</guid>
      <description>&lt;p&gt;The &lt;strong&gt;&lt;a href="https://howtostartprogramming.in/what-is-solid-design-principle-in-java/" rel="noopener noreferrer"&gt;SOLID&lt;/a&gt;&lt;/strong&gt; design principles in Java are a set of guidelines that help developers create robust, scalable, and maintainable software. These principles are foundational in object-oriented programming and play a critical role in ensuring that your codebase remains flexible and adaptable as it evolves. Let’s break down what &lt;strong&gt;SOLID&lt;/strong&gt; means and why it’s essential:&lt;/p&gt;

&lt;h4&gt;1. &lt;strong&gt;S - Single Responsibility Principle (SRP)&lt;/strong&gt;
&lt;/h4&gt;

&lt;ul&gt;
    &lt;li&gt;
&lt;strong&gt;Definition&lt;/strong&gt;: A class should have only one reason to change, meaning it should only have one responsibility or job.&lt;/li&gt;
    &lt;li&gt;
&lt;strong&gt;Why It Matters&lt;/strong&gt;: This principle makes your code easier to understand, test, and maintain. When a class is focused on a single task, it reduces the risk of bugs and makes it easier to refactor without affecting other parts of your system.&lt;/li&gt;
    &lt;li&gt;
&lt;strong&gt;Example&lt;/strong&gt;: &lt;a href="https://howtostartprogramming.in/single-responsibility-principle-srp/" rel="noopener noreferrer"&gt;SRP&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;2. &lt;strong&gt;O - Open/Closed Principle (OCP)&lt;/strong&gt;
&lt;/h4&gt;

&lt;ul&gt;
    &lt;li&gt;
&lt;strong&gt;Definition&lt;/strong&gt;: Software entities (classes, modules, functions, etc.) should be open for extension but closed for modification.&lt;/li&gt;
    &lt;li&gt;
&lt;strong&gt;Why It Matters&lt;/strong&gt;: This principle allows you to extend the behavior of a class without modifying its existing code. It’s crucial for building flexible and scalable systems, as it reduces the chances of introducing new bugs when you add new features.&lt;/li&gt;
    &lt;li&gt;
&lt;strong&gt;Example&lt;/strong&gt;: &lt;a href="https://howtostartprogramming.in/open-closed-principle-ocp/" rel="noopener noreferrer"&gt;OCP&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;3. &lt;strong&gt;L - Liskov Substitution Principle (LSP)&lt;/strong&gt;
&lt;/h4&gt;

&lt;ul&gt;
    &lt;li&gt;
&lt;strong&gt;Definition&lt;/strong&gt;: Subtypes must be substitutable for their base types without altering the correctness of the program.&lt;/li&gt;
    &lt;li&gt;
&lt;strong&gt;Why It Matters&lt;/strong&gt;: This principle ensures that derived classes enhance, rather than replace, the functionality of their base classes. It helps in maintaining the integrity of your code and ensures that components remain interchangeable.&lt;/li&gt;
    &lt;li&gt;
&lt;strong&gt;Example&lt;/strong&gt;: &lt;a href="https://howtostartprogramming.in/liskov-substitution-principle-lsp/" rel="noopener noreferrer"&gt;LSP&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;4. &lt;strong&gt;I - Interface Segregation Principle (ISP)&lt;/strong&gt;
&lt;/h4&gt;

&lt;ul&gt;
    &lt;li&gt;
&lt;strong&gt;Definition&lt;/strong&gt;: No client should be forced to depend on methods it does not use. Essentially, interfaces should be specific to the needs of the client.&lt;/li&gt;
    &lt;li&gt;
&lt;strong&gt;Why It Matters&lt;/strong&gt;: This principle reduces the burden on implementing classes and prevents them from being bloated with unnecessary methods. It makes your code more modular, easier to understand, and simpler to maintain.&lt;/li&gt;
    &lt;li&gt;
&lt;strong&gt;Example&lt;/strong&gt;: &lt;a href="https://howtostartprogramming.in/interface-segregation-principle-isp/" rel="noopener noreferrer"&gt;ISP&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;5. &lt;strong&gt;D - Dependency Inversion Principle (DIP)&lt;/strong&gt;
&lt;/h4&gt;

&lt;ul&gt;
    &lt;li&gt;
&lt;strong&gt;Definition&lt;/strong&gt;: High-level modules should not depend on low-level modules. Both should depend on abstractions. Also, abstractions should not depend on details; details should depend on abstractions.&lt;/li&gt;
    &lt;li&gt;
&lt;strong&gt;Why It Matters&lt;/strong&gt;: This principle helps in decoupling your code, making it more resilient to changes and easier to manage. It encourages the use of interfaces or abstract classes, leading to a more flexible and testable codebase.&lt;/li&gt;
    &lt;li&gt;
&lt;strong&gt;Example&lt;/strong&gt;: &lt;a href="https://howtostartprogramming.in/dependency-inversion-principle-dip/" rel="noopener noreferrer"&gt;DIP&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;Why SOLID Principles are Essential in Java Development&lt;/h3&gt;

&lt;p&gt;By adhering to the &lt;strong&gt;SOLID&lt;/strong&gt; principles, you create a codebase that is:&lt;/p&gt;

&lt;ul&gt;
    &lt;li&gt;
&lt;strong&gt;Maintainable&lt;/strong&gt;: Easier to modify and extend as your project grows.&lt;/li&gt;
    &lt;li&gt;
&lt;strong&gt;Understandable&lt;/strong&gt;: Clearer structure, making it easier for other developers to read and contribute.&lt;/li&gt;
    &lt;li&gt;
&lt;strong&gt;Testable&lt;/strong&gt;: Isolated responsibilities make it easier to write unit tests and reduce bugs.&lt;/li&gt;
    &lt;li&gt;
&lt;strong&gt;Flexible&lt;/strong&gt;: Adaptable to new requirements with minimal impact on existing code.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In essence, the &lt;strong&gt;&lt;a href="https://howtostartprogramming.in/what-is-solid-design-principle-in-java/" rel="noopener noreferrer"&gt;SOLID&lt;/a&gt;&lt;/strong&gt; principles are the cornerstone of writing clean, efficient, and sustainable &lt;a href="https://howtostartprogramming.in/category/java/" rel="noopener noreferrer"&gt;&lt;strong&gt;Java code&lt;/strong&gt;&lt;/a&gt;. They empower developers to build systems that are not only functional, but also scalable and easy to maintain in the long run.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>How to find out the differences between 2 objects in Java (2023)</title>
      <dc:creator>Rajesh Mishra</dc:creator>
      <pubDate>Mon, 07 Aug 2023 21:40:45 +0000</pubDate>
      <link>https://dev.to/rajesh1761/how-to-find-out-the-differences-between-2-objects-in-java-2023-5bph</link>
      <guid>https://dev.to/rajesh1761/how-to-find-out-the-differences-between-2-objects-in-java-2023-5bph</guid>
      <description>&lt;p&gt;In this post, we will showcase a step-by-step guide on how to detect and present distinctions between two java objects by leveraging a set of classes and interfaces provided by Apache Commons Lang.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Classes, Interfaces, and pom.xml&lt;/strong&gt;&lt;br&gt;
The classes and interfaces to be used for this post are as follows:&lt;/p&gt;

&lt;p&gt;org.apache.commons.lang3.builder.Diff&lt;br&gt;
org.apache.commons.lang3.builder.DiffResult&lt;br&gt;
org.apache.commons.lang3.builder.DiffBuilder&lt;br&gt;
org.apache.commons.lang3.builder.Diffable&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Scenario&lt;/strong&gt;&lt;br&gt;
We have two Employee Object for our use case, and we need to list the differences between them. The list must show the affected field and the old-new (from Employee 1 to Employee 2 values.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Employee Object 1&lt;/strong&gt;&lt;br&gt;
For the Employee Object, we have the following data.&lt;/p&gt;

&lt;p&gt;Name = Ankit&lt;br&gt;
ID = 1&lt;br&gt;
Designation = Assistant Vice President&lt;br&gt;
Address = Mumbai&lt;br&gt;
Employee Object 2&lt;br&gt;
For the Employee Object, we have the following data.&lt;/p&gt;

&lt;p&gt;Name = Ankit&lt;br&gt;
ID = 1&lt;br&gt;
Designation = Vice President&lt;br&gt;
Address = Pune&lt;/p&gt;

&lt;p&gt;&lt;a href="https://howtostartprogramming.in/how-to-find-out-the-differences-between-two-objects-in-java/"&gt;Read complete tutorial&lt;/a&gt;&lt;/p&gt;

</description>
      <category>howtostartprogramming</category>
      <category>java</category>
      <category>java8</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>How To Create Custom Annotation In Java</title>
      <dc:creator>Rajesh Mishra</dc:creator>
      <pubDate>Sun, 18 Dec 2022 20:46:07 +0000</pubDate>
      <link>https://dev.to/rajesh1761/how-to-create-custom-annotation-in-java-3he5</link>
      <guid>https://dev.to/rajesh1761/how-to-create-custom-annotation-in-java-3he5</guid>
      <description>&lt;h3&gt;Create Custom Annotation:&lt;/h3&gt;

&lt;p&gt;In java, creating an annotation is &lt;b&gt;&lt;a class="mentioned-user" href="https://dev.to/interface"&gt;@interface&lt;/a&gt;&lt;/b&gt; is used to create an Annotation.&lt;/p&gt;

&lt;pre&gt;public &lt;a class="mentioned-user" href="https://dev.to/interface"&gt;@interface&lt;/a&gt; MyAnnotation{

}&lt;/pre&gt;

&lt;p&gt;We can also define methods inside an annotation.&lt;/p&gt;

&lt;pre&gt;public &lt;a class="mentioned-user" href="https://dev.to/interface"&gt;@interface&lt;/a&gt; MyAnnotation{
   int value();
}&lt;/pre&gt;

&lt;p&gt;&lt;b&gt;Note:&lt;/b&gt; The methods of an annotation should adhere to the following rules:&lt;/p&gt;

&lt;ul&gt;
    &lt;li&gt;Method declaration should not have any parameters&lt;/li&gt;
    &lt;li&gt;Method declaration should not have any throws clause&lt;/li&gt;
    &lt;li&gt;Method return type should be of primitives, String, Class, Enum, Annotations, and Arrays of the preceding types&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Also, we can provide a default value for the methods inside an annotation.&lt;/p&gt;

&lt;pre&gt;public &lt;a class="mentioned-user" href="https://dev.to/interface"&gt;@interface&lt;/a&gt; MyAnnotation{
   int value() default 1;
}&lt;/pre&gt;

&lt;h3&gt;&lt;b&gt;Types of Annotations:&lt;/b&gt;&lt;/h3&gt;

&lt;ul&gt;
    &lt;li&gt;&lt;a href="https://howtostartprogramming.in/how-to-create-custom-annotation-in-java/" rel="noopener noreferrer"&gt;Market Annotation&lt;/a&gt;&lt;/li&gt;
    &lt;li&gt;&lt;a href="https://howtostartprogramming.in/how-to-create-custom-annotation-in-java/" rel="noopener noreferrer"&gt;Single - Value Annotation&lt;/a&gt;&lt;/li&gt;
    &lt;li&gt;&lt;a href="https://howtostartprogramming.in/how-to-create-custom-annotation-in-java/" rel="noopener noreferrer"&gt;Multi - Value Annotation&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>discuss</category>
      <category>mentorship</category>
      <category>webdev</category>
      <category>learning</category>
    </item>
    <item>
      <title>Algorithm Code Complexity</title>
      <dc:creator>Rajesh Mishra</dc:creator>
      <pubDate>Sun, 14 Aug 2022 17:07:21 +0000</pubDate>
      <link>https://dev.to/rajesh1761/algorithm-code-complexity-3ol0</link>
      <guid>https://dev.to/rajesh1761/algorithm-code-complexity-3ol0</guid>
      <description>&lt;p&gt;&lt;a href="https://howtostartprogramming.in/algorithm-code-complexity/"&gt;&lt;strong&gt;Code Complexity&lt;/strong&gt; &lt;/a&gt;compute it by using the control flow graph of the program. Cyclomatic complexity &lt;b&gt;measures the number of nested conditions within the code, such as those created by for, if/else, switch, while, and until&lt;/b&gt;. The greater the number of conditions (as in example b from above), the greater the complexity.&lt;/p&gt;

&lt;p&gt; &lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td colspan="4"&gt;&lt;strong&gt;Time complexity:&lt;/strong&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt; Worst Case Scenario&lt;/td&gt;
&lt;td&gt;Average Case Scenario&lt;/td&gt;
&lt;td&gt;Best Case Scenario&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Accessing an element&lt;/td&gt;
&lt;td&gt;O(1)&lt;/td&gt;
&lt;td&gt;O(1)&lt;/td&gt;
&lt;td&gt;O(1)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Updating an element&lt;/td&gt;
&lt;td&gt;O(1)&lt;/td&gt;
&lt;td&gt;O(1)&lt;/td&gt;
&lt;td&gt;O(1)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Deleting an element&lt;/td&gt;
&lt;td&gt;O(n)&lt;/td&gt;
&lt;td&gt;O(n)&lt;/td&gt;
&lt;td&gt;O(1)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Inserting an element&lt;/td&gt;
&lt;td&gt;O(n)&lt;/td&gt;
&lt;td&gt;O(n)&lt;/td&gt;
&lt;td&gt;O(1)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Searching for an element&lt;/td&gt;
&lt;td&gt;O(n)&lt;/td&gt;
&lt;td&gt;O(n)&lt;/td&gt;
&lt;td&gt;O(1)&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;





&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td colspan="5"&gt; &lt;strong&gt; Algorithm complexity:  &lt;/strong&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;Worst Case&lt;/td&gt;
&lt;td&gt;Average Case&lt;/td&gt;
&lt;td&gt;Best Case&lt;/td&gt;
&lt;td&gt;Space Complexity&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Quicksort&lt;/td&gt;
&lt;td&gt;O(n2)&lt;/td&gt;
&lt;td&gt;O(n log(n))&lt;/td&gt;
&lt;td&gt;O(n log(n))&lt;/td&gt;
&lt;td&gt;O(log(n))&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Mergesort&lt;/td&gt;
&lt;td&gt;O(n log(n))&lt;/td&gt;
&lt;td&gt;O(n log(n))&lt;/td&gt;
&lt;td&gt;O(n log(n))&lt;/td&gt;
&lt;td&gt;O(n)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Heapsort&lt;/td&gt;
&lt;td&gt;O(n log(n))&lt;/td&gt;
&lt;td&gt;O(n log(n))&lt;/td&gt;
&lt;td&gt;O(n log(n))&lt;/td&gt;
&lt;td&gt;O(1)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Bubble Sort&lt;/td&gt;
&lt;td&gt;O(n2)&lt;/td&gt;
&lt;td&gt;O(n2)&lt;/td&gt;
&lt;td&gt;O(n)&lt;/td&gt;
&lt;td&gt;O(1)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Insertion Sort&lt;/td&gt;
&lt;td&gt;O(n2)&lt;/td&gt;
&lt;td&gt;O(n2)&lt;/td&gt;
&lt;td&gt;O(n)&lt;/td&gt;
&lt;td&gt;O(1)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Selection Sort&lt;/td&gt;
&lt;td&gt;O(n2)&lt;/td&gt;
&lt;td&gt;O(n2)&lt;/td&gt;
&lt;td&gt;O(n2)&lt;/td&gt;
&lt;td&gt;O(1)&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;


&lt;p&gt;Source: &lt;a href="https://howtostartprogramming.in/category/algorithm/" rel="dofollow"&gt;Java Algos&lt;/a&gt;&lt;br&gt;&lt;br&gt;
&lt;/p&gt;


</description>
    </item>
    <item>
      <title>Java 8 Map vs flatmap method</title>
      <dc:creator>Rajesh Mishra</dc:creator>
      <pubDate>Wed, 20 Jan 2021 10:09:44 +0000</pubDate>
      <link>https://dev.to/rajesh1761/java-8-map-vs-flatmap-method-1ca9</link>
      <guid>https://dev.to/rajesh1761/java-8-map-vs-flatmap-method-1ca9</guid>
      <description>

&lt;h3&gt;Map vs FlatMap:&lt;/h3&gt;

&lt;p&gt;Java 8 stream API provides &lt;strong&gt;map()&lt;/strong&gt; and &lt;strong&gt;flatMap()&lt;/strong&gt; methods. Both these methods are intermediate methods and returns another stream as part of the output.&lt;/p&gt;

&lt;ul&gt;
    &lt;li&gt;
&lt;strong&gt;map()&lt;/strong&gt; method use for transformation&lt;/li&gt;
    &lt;li&gt;
&lt;strong&gt;flatMap()&lt;/strong&gt; used for transformation &amp;amp; flattering&lt;/li&gt;
    &lt;li&gt;
&lt;strong&gt;flatMap()&lt;/strong&gt;-&amp;gt; &lt;strong&gt;map()&lt;/strong&gt;+&lt;strong&gt;flatring&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;map():&lt;/h4&gt;

&lt;ul&gt;
    &lt;li&gt;
&lt;strong&gt;map()&lt;/strong&gt; takes Stream&amp;lt;T&amp;gt; as input and return Stream&amp;lt;R&amp;gt;&lt;/li&gt;
    &lt;li&gt;Stream&amp;lt;R&amp;gt; map (String&amp;lt;T&amp;gt; input)&lt;/li&gt;
    &lt;li&gt;it's mapper function produces single value for each input value. hence it also called as one-to-one mapping.&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;flatMap():&lt;/h4&gt;

&lt;ul&gt;
    &lt;li&gt;
&lt;strong&gt;flatMap()&lt;/strong&gt; takes Stream&amp;lt;Stream&amp;lt;T&amp;gt;&amp;gt; as input and return Stream&amp;lt;R&amp;gt;&lt;/li&gt;
    &lt;li&gt;Stream&amp;lt;R&amp;gt; map (String&amp;lt;T&amp;gt; input)&lt;/li&gt;
    &lt;li&gt;it's mapper function produces multiple value for each input value. hence it also called as one-to-many mapping.&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;map()&lt;/th&gt;
&lt;th&gt;flatMap()&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;It processes stream of values.&lt;/td&gt;
&lt;td&gt;It processes stream of stream of values.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;It does only mapping.&lt;/td&gt;
&lt;td&gt;It performs mapping as well as flattening.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;It’s mapper function produces single value for each input value.&lt;/td&gt;
&lt;td&gt;It’s mapper function produces multiple values for each input value.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;It is a One-To-One mapping.&lt;/td&gt;
&lt;td&gt;It is a One-To-Many mapping.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Data Transformation : From Stream to Stream&lt;/td&gt;
&lt;td&gt;Data Transformation : From Stream&amp;lt;Stream to Stream&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Use this method when the mapper function is producing a single value for each input value.&lt;/td&gt;
&lt;td&gt;Use this method when the mapper function is producing multiple values for each input value.&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Let's See the below example for better understanding&lt;/p&gt;

&lt;h4&gt;Employee.java&lt;/h4&gt;

&lt;pre&gt;import java.util.List;

public class Employee {

    private int id;
    private String name;
    private String email;
    private List&amp;lt;String&amp;gt; mobileNumbers;

    public Customer() {
    }

    public Employee(int id, String name, String email, List&amp;lt;String&amp;gt; mobileNumbers) {
        this.id = id;
        this.name = name;
        this.email = email;
        this.mobileNumbers = mobileNumbers;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public List&amp;lt;String&amp;gt; getMobileNumbers() {
        return mobileNumbers;
    }

    public void setMobileNumbers(List&amp;lt;String&amp;gt; mobileNumbers) {
        this.mobileNumbers = mobileNumbers;
    }
}&lt;/pre&gt;

&lt;h4&gt;Repository.java&lt;/h4&gt;

&lt;pre&gt;import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class Repository {


    public static List&amp;lt;Employee&amp;gt; getAll() {
        return Stream.of(
                new Employee(001, "Nadeem", "Nadeem@localhost.com", Arrays.asList("737937955", "9812356")),
                new Employee(002, "Shyam", "Shyam@localhost.com", Arrays.asList("65563865", "987654")),
                new Employee(003, "Punneet", "Punneet@localhost.com", Arrays.asList("9412345", "43256")),
                new Employee(004, "Kailash", "Kailash@localhost.com", Arrays.asList("9812345678", "1234456546"))
        ).collect(Collectors.toList());
    }

}&lt;/pre&gt;

&lt;h4&gt;MapFlatMapExample.java&lt;/h4&gt;

&lt;pre&gt;import java.util.List;
import java.util.stream.Collectors;

public class MapFlatMapExample {
    public static void main(String[] args) {

        List&amp;lt;Employee&amp;gt; employee = Repository.getAll();

        List&amp;lt;String&amp;gt; emails = employee.stream()
                .map(employee -&amp;gt; employee.getEmail())
                .collect(Collectors.toList());
        System.out.println(emails);
        List&amp;lt;List&amp;lt;String&amp;gt;&amp;gt; mobileNumbers = customers.
                stream().map(employee -&amp;gt; employee.getMobileNumbers())
                .collect(Collectors.toList());
        System.out.println(mobileNumbers);

        List&amp;lt;String&amp;gt; mobiles = customers.stream()
                .flatMap(employee -&amp;gt; employee.getMobileNumbers().stream())
                .collect(Collectors.toList());
        System.out.println(mobiles);
    }
}&lt;/pre&gt;

&lt;p&gt;&lt;strong&gt;Output:&lt;/strong&gt;&lt;/p&gt;

&lt;pre&gt;Nadeem@localhost.com,Shyam@localhost.com,Punneet@localhost.com,Kailash@localhost.com

737937955,9812356,65563865,987654,9412345,43256,9812345678,1234456546&lt;/pre&gt;

&lt;p&gt; &lt;/p&gt;





</description>
      <category>java</category>
      <category>flatmap</category>
      <category>map</category>
      <category>flatmapvsmap</category>
    </item>
    <item>
      <title>String in java</title>
      <dc:creator>Rajesh Mishra</dc:creator>
      <pubDate>Wed, 20 Jan 2021 09:48:47 +0000</pubDate>
      <link>https://dev.to/rajesh1761/string-in-java-omd</link>
      <guid>https://dev.to/rajesh1761/string-in-java-omd</guid>
      <description>

&lt;h1&gt;What is String?&lt;/h1&gt;

&lt;h3&gt;String Objects are immutable in nature&lt;/h3&gt;

&lt;p&gt;This one is a very important point for interview perspective. What does it mean when we say String is immutable. Immutability is – Once we assign some value to &lt;strong&gt;String&lt;/strong&gt;, that value can never be changed.&lt;/p&gt;

&lt;h3&gt;String implements Serializable, Comparable and CharSequence interfaces.&lt;/h3&gt;

&lt;p&gt;We override &lt;strong&gt;compareTo&lt;/strong&gt;(Object object) method in String class and define custom logic.&lt;/p&gt;

&lt;h3&gt;String overrides equals() and hashCode() methods.&lt;/h3&gt;

&lt;p&gt;This is another important point related to String, which makes String differ from &lt;strong&gt;StringBuffer&lt;/strong&gt;. In String equals() and &lt;strong&gt;hashcode&lt;/strong&gt;() is overridden and it has been defined custom logic inside &lt;strong&gt;equals&lt;/strong&gt;() and &lt;strong&gt;hashcode&lt;/strong&gt;(). Let’s see code snippet, how equals() and &lt;strong&gt;hashcode&lt;/strong&gt;() is overridden.&lt;/p&gt;

&lt;h3&gt;String Constants Pool.&lt;/h3&gt;

&lt;p&gt;String works on String constant pool – String uses special memory location for reusability of String literals that is called String constant pool.&lt;/p&gt;

&lt;h4&gt;String Interview questions with examples:&lt;/h4&gt;

&lt;ul&gt;
    &lt;li&gt;String constant pool in Java&lt;/li&gt;
    &lt;li&gt;&lt;a href="http://immutable_string_in_java"&gt;Why is String immutable in java&lt;/a&gt;&lt;/li&gt;
    &lt;li&gt;Comparing of two String using == and equals()&lt;/li&gt;
    &lt;li&gt;Important methods of String&lt;/li&gt;
    &lt;li&gt;How will we define an immutable class in java&lt;/li&gt;
    &lt;li&gt;&lt;a href="https://onlinetutorials.tech/how-to-create-an-immutable-class-with-mutable-object-references-in-java/"&gt;How to create an immutable class with mutable object references in Java&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id="string_constant_pool_in_java"&gt;String Constant Pool In Java with Example:&lt;/h2&gt;

&lt;p&gt;String uses a special memory location to reuse of String objects, is called String Constant Pool. Suppose we create a &lt;em&gt;&lt;strong&gt;String object without new keyword&lt;/strong&gt;&lt;/em&gt; i.e &lt;em&gt;String s1 = “onlinetutorials”&lt;/em&gt; and again we do &lt;em&gt;String s2 = “onlinetutorials”&lt;/em&gt; and &lt;em&gt;String s3 = “onlinetutorials”&lt;/em&gt;. Here String will create only one object with value i.e "&lt;em&gt;onlinetutorials&lt;/em&gt;". We will have three reference &lt;em&gt;s1, s2, and s3&lt;/em&gt; which are pointing the same object “onlinetutorials”.&lt;/p&gt;

&lt;h3&gt;How it works:&lt;/h3&gt;

&lt;ul&gt;
    &lt;li&gt;When first time we do String s1 = “onlinetutorials”, JVM will create a new object in String Constant Pool and s1 will refer to that object i.e “onlinetutorials”.&lt;/li&gt;
    &lt;li&gt;When we do the second time String s2 = “onlinetutorials”, JVM will check is there any object with value “onlinetutorials” already there in String Constant pool. As of now yes we have already “onlinetutorials” is there in String Constant pool, so it will not create a new object, just s2 reference variable will point to that existing “onlinetutorials” object. The same process will happen for String s3 = “onlinetutorials”.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;Creating String object with new operator (How it works):&lt;/h3&gt;

&lt;p&gt;String str1 = new String(“onlinetutorials”);&lt;/p&gt;

&lt;p&gt;Here the first JVM will check, is there any object available with the name “&lt;strong&gt;onlinetutorials&lt;/strong&gt;” in String Constant Pool or not, if not it will create one object inside String Constant Pool and it will also create another object outside of String Constant Pool and str1 will point this object (the object which has been created outside of String Constant Pool).&lt;/p&gt;

&lt;pre&gt;public class StringExample {
    public static void main(String[] args) {

        String s1 = "OnlineTutorials";
        String s2 = s1 + "tech";
        s1.concat("technology");
        s2.concat(s1);
        s1 += "Java";
        System.out.println(s1 + "   " + s2);

    }
}&lt;/pre&gt;

&lt;h3 id="immutable_string_in_java"&gt;Immutable String in Java:&lt;/h3&gt;

&lt;p&gt;As we know String uses the string constant pool for the memory location. Suppose we have multiple reference variables, all are pointing to the same object, if String is not immutable and we change the value of any reference then all others reference will get affected.&lt;/p&gt;

&lt;blockquote&gt;&lt;em&gt;Suppose now we are doing &lt;strong&gt;str1&lt;/strong&gt; = “&lt;strong&gt;onlinetutorials&lt;/strong&gt;” and if String is not immutable, &lt;strong&gt;str2&lt;/strong&gt; and &lt;strong&gt;str3&lt;/strong&gt; will start pointing to “&lt;strong&gt;onlinetutorials&lt;/strong&gt;”. As of now, we have three objects, it might possible we have a hundred or thousands of objects, all will start to point “&lt;strong&gt;onlinetutorials&lt;/strong&gt;”. That’s the very big problem.&lt;/em&gt;&lt;/blockquote&gt;

&lt;p&gt;Since String is immutable, it is safe to use in multithreading. This avoids the use of synchronization for thread safety, String is implicitly thread safe in nature.&lt;/p&gt;

&lt;h3 id="comapring_of_two_string_using_operators_and_equals_method"&gt;Comparing of two String using == and equals():&lt;/h3&gt;

&lt;p&gt;Before proceeding let’s see differences between &lt;strong&gt;== and equals()&lt;/strong&gt;. &lt;strong&gt;== operator&lt;/strong&gt; &lt;em&gt;&lt;strong&gt;checks for references&lt;/strong&gt;&lt;/em&gt;, if two different reference variable is pointing the same object == will return true, while equals() method is overridden in String class and it will &lt;em&gt;&lt;strong&gt;check for actual content&lt;/strong&gt;&lt;/em&gt;.&lt;/p&gt;

&lt;pre&gt;public class StringExample {
    public static void main(String[] args) {

        String s1 = "onlinetutorials";
        String s2 = "onlinetutorials";

        System.out.println(s1 == s2);
        System.out.println(s1.equals(s2));

    }
}&lt;/pre&gt;

&lt;p&gt;Output:&lt;/p&gt;

&lt;p&gt;&lt;em&gt;&lt;strong&gt;true&lt;/strong&gt;&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;&lt;strong&gt;true&lt;/strong&gt;&lt;/em&gt;&lt;/p&gt;

&lt;h3 id="important_methods_of_string"&gt;Important methods of String:&lt;/h3&gt;

&lt;h4&gt;public boolean equals(Object o)&lt;/h4&gt;

&lt;p&gt;This is a very frequently used method, which is overridden in String class. It checks for content, if the content is equal it returns true else it returns false. Let’s see a simple example –&lt;/p&gt;

&lt;pre&gt;public class StringEquals {
public static void main(String[] args) {

String s1 = new String("onlinetutorials");
String s2 = new String("onlinetutorials");

System.out.println(s1.equals(s2));

}
}&lt;/pre&gt;

&lt;h4&gt;public boolean equalsIgnoreCase(String anotherString)&lt;/h4&gt;

&lt;p&gt;equalsIgnoreCase() will return true if all character in given String objects is the same, even an object contains the capital letters.&lt;/p&gt;

&lt;pre&gt;public class StringEqualsIgnore {
public static void main(String[] args) {

String s1 = "onlinetutorials";
String s2 = "ONLIETUTORIALS";

System.out.println(s1.equalsIgnoreCase(s2));

}
}&lt;/pre&gt;

&lt;h4&gt;public String[] split(String regex)&lt;/h4&gt;

&lt;p&gt;split() method returns String array. We can split any string on the basis of some regex value like space, comma or hyphen(or other regex value).&lt;/p&gt;

&lt;pre&gt;public class StringSplit {
public static void main(String[] args) {

String str= "online tutorials tech";

String[] splitArray = str.split(" ");
for(String s: splitArray){
System.out.println(s);
}

}
}&lt;/pre&gt;

&lt;h4 id="how_define_immutable_class_in_java"&gt;How will we define an immutable class in java:&lt;/h4&gt;

&lt;ol&gt;
    &lt;li&gt;Define class as final.&lt;/li&gt;
    &lt;li&gt;Make fields as private and final.&lt;/li&gt;
    &lt;li&gt;Make the constructor private and create an instance within factory methods.&lt;/li&gt;
    &lt;li&gt;Initialize the field through the constructor.&lt;/li&gt;
    &lt;li&gt;Don’t define the setter method.&lt;/li&gt;
    &lt;li&gt;provide getter method for the corresponding fields.&lt;/li&gt;
&lt;/ol&gt;

&lt;pre&gt;final class ImmutableExample {
    private final String name;

    private ImmutableExample(String name) {
        this.name = name;
    }

    public static ImmutableExample getInstance() {
        return new ImmutableExample("ram");
    }

    public String getName() {
        return name;
    }

}

public class Test {
    public static void main(String[] args) {
        ImmutableExample m = ImmutableExample.getInstance();
        System.out.println("name  :-" + m.getName());

    }
}&lt;/pre&gt;

&lt;p&gt;you can also refer &lt;a href="https://onlinetutorials.tech/how-to-create-an-immutable-class-with-mutable-object-references-in-java/"&gt;How to create an immutable class with mutable object references in Java&lt;/a&gt;&lt;/p&gt;





</description>
      <category>stringinjava</category>
      <category>immutable</category>
      <category>contantspool</category>
    </item>
    <item>
      <title>Sorting using Comparator in java with example</title>
      <dc:creator>Rajesh Mishra</dc:creator>
      <pubDate>Mon, 16 Nov 2020 17:53:18 +0000</pubDate>
      <link>https://dev.to/rajesh1761/sorting-using-comparator-in-java-with-example-2d43</link>
      <guid>https://dev.to/rajesh1761/sorting-using-comparator-in-java-with-example-2d43</guid>
      <description>

&lt;p&gt;&lt;strong&gt;Table of Contents&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
    &lt;li&gt;Basic points about Comparator interface.&lt;/li&gt;
    &lt;li&gt;Example of sorting using Comparator in java using anonymous class.&lt;/li&gt;
    &lt;li&gt;Example of sorting using Comparator in java implementing Comparator interface.&lt;/li&gt;
    &lt;li&gt;Example of Sorting using Comparator and Java 8.&lt;/li&gt;
    &lt;li&gt;Soring list of an object using Comparator.comparing().&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id="Basic_points_about_Comparator_interface"&gt;Basic points about Comparator interface.&lt;/h2&gt;

&lt;p&gt;Comparator is an interface available in java.util package, which provides multiple sorting(means we can sort the basis of id and name at the same time).&lt;/p&gt;

&lt;p&gt;The &lt;strong&gt;Comparator&lt;/strong&gt; &lt;strong&gt;interface&lt;/strong&gt; contains &lt;strong&gt;compare(Object o1, Object o2)&lt;/strong&gt; method which we can override and keep sorting logic there.&lt;/p&gt;

&lt;p&gt;For sorting using &lt;strong&gt;Comparator&lt;/strong&gt; interface &lt;strong&gt;we need to use Collections.sort(List list, Comparator c)&lt;/strong&gt; method.&lt;/p&gt;

&lt;h3&gt;&lt;strong&gt;Different ways of sorting using Comparator interface.&lt;/strong&gt;&lt;/h3&gt;

&lt;h4&gt;Override compare() method.&lt;/h4&gt;

&lt;pre&gt;@Override public int compare(Object o1, Object o2) {
 Employee e1 = (Employee) o1;
 Employee e2 = (Employee) o2;
 return e1.getName().compareTo(e2.getName());
}&lt;/pre&gt;
&lt;h4&gt;Using Java 8 lambda expression.&lt;/h4&gt;
&lt;pre&gt;Collections.sort(employeeList, (employee1, employee2) -&amp;gt; employee1.getName().compareTo(employee2.getName()));&lt;/pre&gt;
&lt;h4&gt;Using Comparator.comparing().&lt;/h4&gt;
&lt;pre&gt;employeeList.sort(Comparator.comparing(Employee::getId));&lt;/pre&gt;
 
&lt;h2 id="Example_of_sorting_using_Comparator_in_java_using_anonymous_class"&gt;Example of sorting using Comparator in java using anonymous class.&lt;/h2&gt;
In this example, we are going to sort Employee type of list on the basis of the name without implementing Comparator(Employee class will not implement Comparator).
&lt;pre&gt;import java.util.Collections;
import java.util.*;
class Employee {
 private String name;
 private int id;
 public Employee() {}
 public Employee(String name, int id) {
  super();
  this.name = name;
  this.id = id;
 }
 public String getName() {
  return name;
 }
 public void setName(String name) {
  this.name = name;
 }
 public int getId() {
  return id;
 }
 public void setId(int id) {
  this.id = id;
 }
}
===============================================================
public class SortingUsingComparator {
 public static void main(String[] args) throws Exception {
  List emplist = new ArrayList &amp;lt; &amp;gt; ();
  emplist.add(new Employee("mohan", 5));
  emplist.add(new Employee("radhika", 1));
  emplist.add(new Employee("gopi", 3));
  emplist.add(new Employee("krishna", 2));
  System.out.println("sorting using comparator by name ------");
  Collections.sort(emplist, new Comparator() {
   @Override public int compare(Object o1, Object o2) {
    Employee e1 = (Employee) o1;
    Employee e2 = (Employee) o2;
    return e1.getName().compareTo(e2.getName());
   }
  });
  for (Employee e: emplist) {
   System.out.println(e.getId() + " " + e.getName());
  }
  System.out.println("sorting using comparator by id --------");
  Collections.sort(emplist, new Comparator() {
   @Override public int compare(Object o1, Object o2) {
    Employee e1 = (Employee) o1;
    Employee e2 = (Employee) o2;
    return e1.getId() - e2.getId();
   }
  });
  for (Employee e: emplist) {
   System.out.println(e.getId() + " " + e.getName());
  }
 }
}&lt;/pre&gt;


&lt;blockquote&gt;
&lt;strong&gt;sorting using comparator by name ——&lt;/strong&gt;
&lt;pre&gt;3 gopi
2 krishna
5 mohan
1 radhika
&lt;/pre&gt;

&lt;p&gt;&lt;strong&gt;sorting using comparator by id ——–&lt;/strong&gt;&lt;/p&gt;

&lt;pre&gt;1 radhika
2 krishna
3 gopi
5 mohan
&lt;/pre&gt;


&lt;/blockquote&gt;

&lt;h2 id="Example_of_sorting_using_Comparator_in_java_implementing_Comparator_interface"&gt;Example of sorting using Comparator in java implementing Comparator interface.&lt;/h2&gt;

&lt;p&gt;we can define a separate class which will implement Comparator interface.&lt;/p&gt;

&lt;pre&gt;import java.util.*;
import java.io.*;
class Employee {
 String name;
 int id;
 Employee(String name, int id) {
  this.name = name;
  this.id = id;
 }
}
class NameComparator implements Comparator {
 @Override public int compare(Object o1, Object o2) {
  Employee e1 = (Employee) o1;
  Employee e2 = (Employee) o2;
  return e1.name.compareTo(e2.name);
 }
}
class IdComparator implements Comparator {
 @Override public int compare(Object o1, Object o2) {
  Employee e1 = (Employee) o1;
  Employee e2 = (Employee) o2;
  return e2.id - e1.id;
 }
}
public class SortByUsingComparator {
 public static void main(String[] args) {
  List emplist = new ArrayList();
  emplist.add(new Employee("rakesh", 105));
  emplist.add(new Employee("bittu", 103));
  emplist.add(new Employee("tituu", 110));
  emplist.add(new Employee("asharaf", 108));
  System.out.println("sorting by name-------");
  Collections.sort(emplist, new NameComparator());
  Iterator itr = emplist.iterator();
  while (itr.hasNext()) {
   Employee emp = (Employee) itr.next();
   System.out.println(emp.name + " " + emp.id);
  }
  System.out.println("sorting by id---------");
  Collections.sort(emplist, new IdComparator());
  Iterator itr2 = emplist.iterator();
  while (itr2.hasNext()) {
   Employee emp = (Employee) itr2.next();
   System.out.println(emp.name + " " + emp.id);
  }
 }
}&lt;/pre&gt;
&lt;blockquote&gt;
&lt;strong&gt;The output of the above program is –&lt;/strong&gt;
&lt;pre&gt;anjali 108
bittu 103
rakesh 105
shibha 110
&lt;/pre&gt;

&lt;p&gt;&lt;strong&gt;sorting by id———&lt;/strong&gt;&lt;/p&gt;

&lt;pre&gt;shibha 110
anjali 108
rakesh 105
bittu 103
&lt;/pre&gt;




&lt;/blockquote&gt;

&lt;h2 id="Example_of_Sorting_using_Comparator_and_Java_8"&gt;Example of Sorting using Comparator and Java 8.&lt;/h2&gt;

&lt;pre&gt;import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
class Employee {
 private String name;
 private int id;
 Employee(String name, int id) {
  this.name = name;
  this.id = id;
 }
 public String getName() {
  return name;
 }
 public void setName(String name) {
  this.name = name;
 }
 public int getId() {
  return id;
 }
 public void setId(int id) {
  this.id = id;
 }
}
public class SortingUsingJava8 {
 public static void main(String[] args) {
  List employeeList = new ArrayList();
  employeeList.add(new Employee("rakesh", 105));
  employeeList.add(new Employee("bittu", 103));
  employeeList.add(new Employee("shibha", 110));
  employeeList.add(new Employee("anjali", 108));
  System.out.println("sorting on basis of name-------");
  Collections.sort(employeeList, (employee1, employee2) -&amp;gt; employee1.getName().compareTo(employee2.getName()));// Lambda expression for sorting on basis of name
  for (Employee employee: employeeList) {
   System.out.println(employee.getName() + " " + employee.getId());
  }
  Lambda expression
  for sorting on basis of id System.out.println("sorting on basis of id-------");
  Collections.sort(employeeList, (employee1, employee2) -&amp;gt; employee1.getId() - employee2.getId());// Lambda expression for sorting on basis of id
  for (Employee employee: employeeList) {
   System.out.println(employee.getName() + " " + employee.getId());
  }
 }
}&lt;/pre&gt;

&lt;blockquote&gt;
&lt;pre&gt;The output of above program.

&lt;/pre&gt;

&lt;p&gt;&lt;strong&gt;Sorting on basis of name——-&lt;/strong&gt;&lt;/p&gt;

&lt;pre&gt;anjali 108
bittu 103
rakesh 105
shibha 110

&lt;/pre&gt;

&lt;p&gt;&lt;strong&gt;Sorting on basis of id——-&lt;/strong&gt;&lt;/p&gt;

&lt;pre&gt;bittu 103
rakesh 105
anjali 108
shibha 110
&lt;/pre&gt;




&lt;/blockquote&gt;

&lt;h2 id="Soring_list_of_an_object_using_Comparator_comparing()"&gt;Soring list of an object using Comparator.comparing().&lt;/h2&gt;

&lt;pre&gt;import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
class Employee {
 private String name;
 private int id;
 Employee(String name, int id) {
  this.name = name;
  this.id = id;
 }
 public String getName() {
  return name;
 }
 public void setName(String name) {
  this.name = name;
 }
 public int getId() {
  return id;
 }
 public void setId(int id) {
  this.id = id;
 }
}
public class SortingUsingJava8 {
 public static void main(String[] args) {
  List employeeList = new ArrayList();
  employeeList.add(new Employee("rakesh", 105));
  employeeList.add(new Employee("bittu", 103));
  employeeList.add(new Employee("shibha", 110));
  employeeList.add(new Employee("anjali", 108));
  System.out.println("sorting on basis of name-------");
  employeeList.sort(Comparator.comparing(Employee::getName));
  for (Employee employee: employeeList) {
   System.out.println(employee.getName() + " " + employee.getId());
  }
  System.out.println("sorting on basis of id-------");
  employeeList.sort(Comparator.comparing(Employee::getId));
  for (Employee employee: employeeList) {
   System.out.println(employee.getName() + " " + employee.getId());
  }
 }
}&lt;/pre&gt;

&lt;pre&gt;The output of the above program. Sorting on basis of name: anjali 108 bittu 103 rakesh 105 shibha 110 Sorting on basis of id: bittu 103 rakesh 105 anjali 108 shibha 110&lt;/pre&gt;

&lt;p&gt;&lt;br&gt;
Learn more about&lt;strong&gt; &lt;a href="https://onlinetutorials.tech/java-collection/"&gt;collection in java&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;





</description>
      <category>collection</category>
      <category>java</category>
      <category>collections</category>
      <category>collectionsinjava</category>
    </item>
    <item>
      <title>@PathVariable and @RequestParam annotations in Spring Boot Part 2</title>
      <dc:creator>Rajesh Mishra</dc:creator>
      <pubDate>Mon, 16 Nov 2020 17:51:38 +0000</pubDate>
      <link>https://dev.to/rajesh1761/pathvariable-and-requestparam-annotations-in-spring-boot-part-2-48mg</link>
      <guid>https://dev.to/rajesh1761/pathvariable-and-requestparam-annotations-in-spring-boot-part-2-48mg</guid>
      <description>

&lt;p&gt;&lt;strong&gt;Table of Contents:&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
    &lt;li&gt;Basic points about @PathVariable and @RequestParam annotations.&lt;/li&gt;
    &lt;li&gt;@PathVariable annotations Example in Spring Boot.&lt;/li&gt;
    &lt;li&gt;@RequestParam example in Spring Boot.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id="PathVariable_and_@RequestParam_annotations"&gt;Basic points about @PathVariable and @RequestParam annotations.&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;@PathVariable annotation-&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
    &lt;li&gt;This annotation introduced in Spring  3.0, available in &lt;strong&gt;org.springframework.web.bind.annotation package&lt;/strong&gt;.&lt;/li&gt;
    &lt;li&gt;Optional elements  ( name, required, value).&lt;/li&gt;
    &lt;li&gt;This annotation used as a method parameter.&lt;/li&gt;
    &lt;li&gt;It takes placeholder value from &lt;strong&gt;URI&lt;/strong&gt;.&lt;/li&gt;
    &lt;li&gt;The example of rest &lt;strong&gt;URI&lt;/strong&gt; when we use &lt;strong&gt;@PathVariable&lt;/strong&gt; – &lt;strong&gt;http://localhost:9093/rest/listofbooks/{bookId}&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;@RequestaParam annotation-&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
    &lt;li&gt;This annotation introduced in Spring  2.5, available in org.springframework.web.bind.annotation package.&lt;/li&gt;
    &lt;li&gt;Optional elements  (name, required, value).&lt;/li&gt;
    &lt;li&gt;This annotation used as a method parameter.&lt;/li&gt;
    &lt;li&gt;It takes parameter value from URI.&lt;/li&gt;
    &lt;li&gt;The example of rest URI when we use @RequestParam –&lt;/li&gt;
&lt;/ul&gt;

&lt;pre&gt;– http://localhost:9093/rest/listofbooks/{bookId}/book?bookName=book1&lt;/pre&gt;

&lt;h2 id="PathVariable_annotations"&gt;@PathVariable annotations Example in Spring Boot.&lt;/h2&gt;

&lt;p&gt;prerequisites –&lt;/p&gt;

&lt;ul&gt;
    &lt;li&gt;JDK 1.8&lt;/li&gt;
    &lt;li&gt;Eclipse&lt;/li&gt;
    &lt;li&gt;maven&lt;/li&gt;
    &lt;li&gt;postman&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Create maven project, Don’t forget to check ‘Create a simple project (skip)’click on next. Fill all details(GroupId – pathvariableexample, ArtifactId – pathvariableexample and name – pathvariableexample) and click on finish. Keep packaging as the jar.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;POM.XML&lt;/strong&gt;&lt;/p&gt;

&lt;pre&gt;&amp;lt;project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"&amp;gt;
  &amp;lt;modelVersion&amp;gt;4.0.0&amp;lt;/modelVersion&amp;gt;
  &amp;lt;groupId&amp;gt;pathvariableexample&amp;lt;/groupId&amp;gt;
  &amp;lt;artifactId&amp;gt;pathvariableexample&amp;lt;/artifactId&amp;gt;
  &amp;lt;version&amp;gt;0.0.1-SNAPSHOT&amp;lt;/version&amp;gt;
  &amp;lt;name&amp;gt;pathvariableexample&amp;lt;/name&amp;gt;  
  &amp;lt;parent&amp;gt;
        &amp;lt;groupId&amp;gt;org.springframework.boot&amp;lt;/groupId&amp;gt;
        &amp;lt;artifactId&amp;gt;spring-boot-starter-parent&amp;lt;/artifactId&amp;gt;
        &amp;lt;version&amp;gt;1.5.2.RELEASE&amp;lt;/version&amp;gt;
 &amp;lt;/parent&amp;gt;
  &amp;lt;dependencies&amp;gt;
    &amp;lt;dependency&amp;gt;
            &amp;lt;groupId&amp;gt;org.springframework.boot&amp;lt;/groupId&amp;gt;
            &amp;lt;artifactId&amp;gt;spring-boot-starter-web&amp;lt;/artifactId&amp;gt;
        &amp;lt;/dependency&amp;gt;
   &amp;lt;/dependencies&amp;gt;
 &amp;lt;/project&amp;gt;&lt;/pre&gt;

&lt;p&gt;&lt;strong&gt;Book.java&lt;/strong&gt;&lt;/p&gt;

&lt;pre&gt;package com.onlintutorials.tech;

public class Book {
    int bookId;
    String bookName;
    String bookPrice;
    public int getBookId() {
        return bookId;
    }
    public void setBookId(int bookId) {
        this.bookId = bookId;
    }
    public String getBookName() {
        return bookName;
    }
    public void setBookName(String bookName) {
        this.bookName = bookName;
    }
    public String getBookPrice() {
        return bookPrice;
    }
    public void setBookPrice(String bookPrice) {
        this.bookPrice = bookPrice;
    }
    
}&lt;/pre&gt;

&lt;p&gt;&lt;strong&gt;BookController.java&lt;/strong&gt;&lt;/p&gt;

&lt;pre&gt;package com.onlinetutotrials.tech;

import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import java.util.*;

@RestController
@RequestMapping(value = "/rest")
public class BookController {

    @RequestMapping(value = "/book/{bookId}",method = RequestMethod.GET)
    public Book getBookById(@PathVariable int bookId) {
        
        List&amp;lt;Book&amp;gt; listBook = createBook();
        for(Book book : listBook) {
            if(book.getBookId() == 1) {
                return book;
            }
        }
        
        return null;        
    }
    
    public List&amp;lt;Book&amp;gt; createBook() {
    Book book = new Book();
        book.setBookId(1);
        book.setBookName("book1");
        book.setBookPrice("100");
        
        Book book1 = new Book();
        book1.setBookId(2);
        book1.setBookName("book2");
        book1.setBookPrice("200");
        List&amp;lt;Book&amp;gt; bookList = new ArrayList&amp;lt;Book&amp;gt;();
        bookList.add(book);
        bookList.add(book1);
        
        return bookList;
        
    }
}&lt;/pre&gt;

&lt;p&gt;&lt;strong&gt;SpringMain.java&lt;/strong&gt;&lt;/p&gt;

&lt;pre&gt;package com.onlinetutorials.tech;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;

@SpringBootApplication
public class SpringMain {
    public static void main(final String[] args) {
        final ConfigurableApplicationContext configurableApplicationContext = SpringApplication
                .run(SpringMain.class, args);
    }
}&lt;/pre&gt;

&lt;p&gt;&lt;strong&gt;Now hit the URL:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="http://localhost:8080:/rest/book/1"&gt;&lt;/a&gt;&lt;a href="http://localhost:8080:/rest/book/1"&gt;http://localhost:8080:/rest/book/1&lt;/a&gt;&lt;/p&gt;

&lt;h2 id="RequestParam_example"&gt;&lt;strong&gt;@RequestParam example in Spring Boot.&lt;/strong&gt;&lt;/h2&gt;

&lt;p&gt;Let’s modify the controller class.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;BookController.java&lt;/strong&gt;&lt;/p&gt;

&lt;pre&gt;package com.onlinetutorials.tech;

import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.*;

@RestController
@RequestMapping(value = "/rest")
public class BookController {

    @RequestMapping(value = "/listofbooks/{bookId}/book",method = RequestMethod.GET)
    public Book getBookById(
            @PathVariable int bookId, 
            @RequestParam String bookName) {
        
        List&amp;lt;Book&amp;gt; listBook = createBook();
        for(Book book : listBook) {
            System.out.println("bookIs is ---"+bookId);
            System.out.println("bookName is ---"+bookName);
            if(book.getBookId() == 1 &amp;amp;&amp;amp; book.getBookName().equals(bookName)) {
                return book;
            }
        }
        return null;
    }
    
    public List&amp;lt;Book&amp;gt; createBook() {
        
        Book book = new Book();
        book.setBookId(1);
        book.setBookName("book1");
        book.setBookPrice("100");
        
        Book book1 = new Book();
        book1.setBookId(2);
        book1.setBookName("book2");
        book1.setBookPrice("200");
        List&amp;lt;Book&amp;gt; bookList = new ArrayList&amp;lt;Book&amp;gt;();
        bookList.add(book);
        bookList.add(book1);
        
        return bookList;
        
    }
}&lt;/pre&gt;

&lt;p&gt;Now hit the URL to see the OutPut:&lt;/p&gt;

&lt;p&gt;&lt;a href="http://localhost:8080:/rest/listpfbooks/1/book?bookName=book1"&gt;&lt;strong&gt;&lt;a href="http://localhost:8080:/rest/listpfbooks/1/book?bookName=book1"&gt;http://localhost:8080:/rest/listpfbooks/1/book?bookName=book1&lt;/a&gt;&lt;/strong&gt;&lt;/a&gt;&lt;/p&gt;


&lt;p&gt;Source: &lt;a href="https://howtostartprogramming.in/category/java/" rel="dofollow"&gt;Java Algos&lt;/a&gt;&lt;br&gt;&lt;br&gt;
&lt;/p&gt;

&lt;br&gt;&lt;br&gt;


</description>
      <category>springboot</category>
      <category>interviewquestions</category>
      <category>java</category>
      <category>annotations</category>
    </item>
    <item>
      <title>Exception Handling in Java</title>
      <dc:creator>Rajesh Mishra</dc:creator>
      <pubDate>Fri, 07 Aug 2020 16:44:34 +0000</pubDate>
      <link>https://dev.to/rajesh1761/exception-handling-in-java-2547</link>
      <guid>https://dev.to/rajesh1761/exception-handling-in-java-2547</guid>
      <description>

&lt;h3&gt;Table of Contents:&lt;/h3&gt;

&lt;ol&gt;
    &lt;li&gt;Exception&lt;/li&gt;
    &lt;li&gt;
Checked and Unchecked Exception in java
&lt;ol&gt;
    &lt;li&gt;Checked Exception&lt;/li&gt;
    &lt;li&gt;UnChecked Exception&lt;/li&gt;
&lt;/ol&gt;




&lt;/li&gt;

    &lt;li&gt;Difference between checked and unchecked exception in java&lt;/li&gt;

    &lt;li&gt;
Try Catch and finally block

&lt;ol&gt;
    &lt;li&gt;Try with finally block&lt;/li&gt;
&lt;/ol&gt;




&lt;/li&gt;

    &lt;li&gt;Custom exception in Java With Example&lt;/li&gt;

    &lt;li&gt;Try with multiple catch blocks in java with example&lt;/li&gt;

    &lt;li&gt;Return statement in exception handling in Java with Example&lt;/li&gt;

    &lt;li&gt;Exception in case of method overriding&lt;/li&gt;

&lt;/ol&gt;
&lt;br&gt;&lt;br&gt;
&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--ffE-G61b--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/http://onlinetutorials.tech/wp-content/uploads/2020/08/Exception_handling_in_java-300x188.png" alt="Exception Handling In Java" width="300" height="188"&gt;

&lt;h2 id="exceptions-in-java"&gt;Exception in java&lt;/h2&gt;

&lt;p&gt;An exception is an event, which occurs during the execution of a program, that disrupts the normal flow of the program’s executions.&lt;/p&gt;

&lt;h2 id="checked-and-unchecked-exception"&gt;Checked and Unchecked Exception&lt;/h2&gt;

&lt;p&gt;All the &lt;strong&gt;Exceptions&lt;/strong&gt; which are not a child class of &lt;strong&gt;RuntimeException&lt;/strong&gt; are called Checked Exception. We have to &lt;strong&gt;handle&lt;/strong&gt; all Checked exception at compile time by using &lt;strong&gt;throws&lt;/strong&gt; keywords or by using &lt;strong&gt;try-catch block&lt;/strong&gt;&lt;/p&gt;

&lt;h3 id="checked-exception"&gt;Examples of Checked Exception.&lt;/h3&gt;

&lt;ul&gt;
    &lt;li&gt;IOException&lt;/li&gt;
    &lt;li&gt;FileNotFoundException&lt;/li&gt;
    &lt;li&gt;ClassNotFoundException&lt;/li&gt;
    &lt;li&gt;CloneNotSupportedException&lt;/li&gt;
    &lt;li&gt;NoSuchMethodException&lt;/li&gt;
    &lt;li&gt;InterruptedException&lt;/li&gt;
    &lt;li&gt;NoSuchFieldException&lt;/li&gt;
    &lt;li&gt;ParseException&lt;/li&gt;
&lt;/ul&gt;

&lt;pre&gt;import java.io.*;
class Exception { 
public static void main(String args[]) {
    FileInputStream fis = new FileInputStream("C:/Onlinetutorials"); 
    fis.close();
  }
}&lt;/pre&gt;

&lt;p&gt;The above code will not compile because &lt;strong&gt;FileInputStream&lt;/strong&gt; constructor defined public &lt;em&gt;&lt;strong&gt;FileInputStream(String name) throws FileNotFoundException&lt;/strong&gt;&lt;/em&gt; which is &lt;strong&gt;checked exception&lt;/strong&gt; and also close() method throws &lt;strong&gt;IOException&lt;/strong&gt; which is also a checked &lt;strong&gt;exception&lt;/strong&gt;. We need to do following changes in order to resolve the compilation error.&lt;/p&gt;

&lt;p&gt;As we know checked exception &lt;strong&gt;must be handle at compile time&lt;/strong&gt; either by &lt;strong&gt;using throws keyword or by using a try-catch block&lt;/strong&gt;. So here firstly we are using throws keyword.&lt;/p&gt;

&lt;h4&gt;Using throws keyword:&lt;/h4&gt;

&lt;pre&gt;import java.io.*;
class Exception { 
public static void main(String args[]) throws Exception {
    FileInputStream fis = new FileInputStream("C:/Onlinetutorials"); 
    fis.close();
  }
}&lt;/pre&gt;

&lt;h4&gt;Using try catch block:&lt;/h4&gt;

&lt;pre&gt;import java.io.*;
class Exception { 
public static void main(String args[]) {
try {
    FileInputStream fis = new FileInputStream("C:/Onlinetutorials"); 
    fis.close();
}catch (Exception e) {
    e.printStackTrace();
    }
  }
}&lt;/pre&gt;

&lt;h2 id="unchecked-exception"&gt;Unchecked Exception&lt;/h2&gt;

&lt;p&gt;All Exceptions which are the subclass of &lt;strong&gt;RuntimeException&lt;/strong&gt; is called &lt;strong&gt;unchecked&lt;/strong&gt; exceptions. Like &lt;strong&gt;Checked&lt;/strong&gt; exceptions, it is not mandatory to handle &lt;strong&gt;RuntimeExcepton&lt;/strong&gt; by using throws keyword or try-catch block. If we do some programming mistakes then JVM will automatically throw a runtime exception. It will always happen at runtime.&lt;/p&gt;

&lt;h3&gt;Example of UnChecked Exception.&lt;/h3&gt;

&lt;ul&gt;
    &lt;li&gt;NullPointerException&lt;/li&gt;
    &lt;li&gt;ClassCastException&lt;/li&gt;
    &lt;li&gt;ArrayIndexOutOfBoundsException&lt;/li&gt;
    &lt;li&gt;IllegalStateException&lt;/li&gt;
    &lt;li&gt;NumberFormatException&lt;/li&gt;
    &lt;li&gt;IllegalArgumentException&lt;/li&gt;
    &lt;li&gt;IllegalMonitorStateException&lt;/li&gt;
    &lt;li&gt;IndexOutOfBoundsException&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Let’s see an example that throws &lt;strong&gt;NullPointerException&lt;/strong&gt;.&lt;/p&gt;

&lt;pre&gt;class NullPointerExample{ 
    public static void main(String args[]) {
    String s=null;
    System.out.println(s.hashCode());
  }
}&lt;/pre&gt;

&lt;h2 id="difference-between-checked-and-unchecked-exception"&gt;Difference between checked and unchecked exception in java&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Checked Exception&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;UnChecked Exception&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The Exception which are not subclass of RuntimeException are Checked Exception.&lt;br&gt;
This occuured only at runtime.&lt;/p&gt;

&lt;p&gt;The Checked Exception must need to be handled at compile time either by using try catch or throws keyword&lt;br&gt;
This is not mandatory to handle.&lt;/p&gt;

&lt;p&gt;Example is :FileNotFoundException/SQLException&lt;br&gt;
Example is :NullPointerException&lt;/p&gt;


&lt;br&gt;


&lt;h2 id="try-catch-finally-block"&gt;Try Catch and finally block&lt;/h2&gt;

&lt;p&gt;The catch block will execute only in case if we have some exception in the try block. The finally block will get always executed.&lt;/p&gt;

&lt;pre&gt;public class ExceptionHandling{
public static void main(String[] args) {
    try{
               //Custom logic
    }catch(Exception e){
        // if some exception will come inside try block 
        //then only catch block will execute    
    }finally {
        //it will execute always even exception comes inside try block or not
    }
}
}&lt;/pre&gt;

&lt;h3 id="try-with-finally"&gt;Try finally block&lt;/h3&gt;

&lt;pre&gt;public class TryWithFinally{
public static void main(String[] args) {
    try{        
        String stringVariable = "OnlineTutorials";
        System.out.println("value of stringVariable "+stringVariable);      
    }finally {
        System.out.println("finally block executed");       
    }
}
}&lt;/pre&gt;

&lt;p&gt;&lt;strong&gt;Note –&lt;/strong&gt; Finally block mainly used to perform release of resources clean up or session closing or database connection closing task.&lt;/p&gt;

&lt;h2 id="custom-exception-in-java"&gt;Creating a custom exception in Java With Example&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Custom Exception:&lt;/strong&gt; Class created by extending Exception class or &lt;strong&gt;RuntimeException&lt;/strong&gt; class is known as &lt;strong&gt;CustomException&lt;/strong&gt;.&lt;/p&gt;

&lt;h5&gt;Steps to Create custom exception.&lt;/h5&gt;

&lt;ul&gt;
    &lt;li&gt;Create &lt;strong&gt;CustomException&lt;/strong&gt; class(Mainly is application name) extending Exception or &lt;strong&gt;RuntimeException&lt;/strong&gt; class.&lt;/li&gt;
    &lt;li&gt;Define one or two or more parametrized constructor.&lt;/li&gt;
    &lt;li&gt;In each constructor, Define super.&lt;/li&gt;
    &lt;li&gt;Use throw keyword to show the proper exception message.&lt;/li&gt;
&lt;/ul&gt;

&lt;pre&gt;package com.onlinetutorials.tech;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

public class OnlineTutorialsException extends Exception{
        public OnlineTutorialsException (String message) {
            super(message);
        }
        public OnlineTutorialsException (String message, Throwable t) {
            super(message, t);
        }
           public static void main(String[] args) {
        try {
           String customerId= null;
                   if(customerId== null) {
                throw new OnlineTutorialsException("Customer Id Cannot be null ");
               }
        }catch(Exception e) {
            System.err.print(e.printStackTrace());
        }
            
       }
}&lt;/pre&gt;

&lt;blockquote&gt;
&lt;strong&gt;Output is –&lt;/strong&gt;

com.onlinetutorials.tech.OnlineTutorialsException: Customer Id Cannot be null
at com.onlinetutorials.tech.OnlineTutorialsException.main(OnlineTutorialsException .java:20)&lt;/blockquote&gt;

&lt;h3&gt;&lt;strong&gt;Creating a custom exception extending RuntimeException class&lt;/strong&gt;&lt;/h3&gt;

&lt;pre&gt;package com.onlinetutorials.tech;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.time.format.DateTimeFormatter;
import java.util.Date;

public class CustomException extends RuntimeException{
     public CustomException (String message) {
            super(message);
        }
     public CustomException (String message, Throwable t) {
            super(message, t);
     }
    public static void main(String[] args) {        
        String dateAsString = "11101988";       
        DateFormat dateFormat = new SimpleDateFormat("dd-MMM-yyyy");        
        try {
            Date date = dateFormat.parse(dateAsString);
        }catch(Exception e) {
            throw new CustomException("problem in parsing the date");
        }                           
    }
}&lt;/pre&gt;

&lt;blockquote&gt;
&lt;strong&gt;Output is –&lt;/strong&gt;

Exception in thread “main” com.onlinetutorials.tech.CustomException: problem in parsing the date
at com.onlinetutorials.tech.CustomException.main(CustomException.java:27)&lt;/blockquote&gt;

&lt;h2 id="try-with-multiple-catch-block"&gt;Try with multiple catch blocks in java with example&lt;/h2&gt;

&lt;ul&gt;
    &lt;li&gt;How to implement try with multiple catch blocks in &lt;a href="https://onlinetutorials.tech/what-is-java-programming-language/"&gt;java&lt;/a&gt;.&lt;/li&gt;
    &lt;li&gt;What is the benefit of multiple catch blocks? How to implement in the real-time scenario?&lt;/li&gt;
&lt;/ul&gt;

&lt;pre&gt;package com.onlinetutorials.tech;

public class TryWithMultipleCatch{
public static void main(String[] args) {
    try {
    //Custom logic
    }catch(NullPointerException n) {
        System.err.println(n);
    }catch(RuntimeException r) {
        System.err.println(r);
    }catch(Exception e) {
        System.err.println(e);
    }
}
}&lt;/pre&gt;

&lt;h2 id="return-statement-in-exception-handling"&gt;Return statement in exception handling in Java with Example&lt;/h2&gt;

&lt;pre&gt;package com.onlinetutorials.tech;

public class Example {
public static void main(String[] args) {
    try{        
        return;
        
    }catch(NullPointerException e) {
        System.err.println("catch block");
    }
    
}
}&lt;/pre&gt;

&lt;pre&gt;package com.onlinetutorials.tech;

public class Example2 {
public static void main(String[] args) {
    try{        
        return;
    }catch(NullPointerException e) {
        return;
    }finally {
        return;
    }       
}
}&lt;/pre&gt;

&lt;h2 id="exception-handling-in-method-overriding"&gt;Exception in case of method overriding&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;when the superclass method throws some exception.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Subclass overridden method can declare subclass exception or same exception(i.e declares in superclass) or no exception.&lt;/p&gt;

&lt;pre&gt;import java.io.FileNotFoundException;
import java.io.IOException;
class Vehicle{
    public void m1() throws IOException{
    System.out.println("super class method");
  }
}
class Car extends Vehicle{
    // subclass exception
    public void m1() throws FileNotFoundException{
    System.out.println("sub class method");
  }
}
public class Test4 {
    public static void main(String[] args) throws Exception{
    Vehicle vehicle=new Car();
    vehicle.m1();
  }
}&lt;/pre&gt;

&lt;p&gt; &lt;/p&gt;


&lt;p&gt;Source: &lt;a href="https://howtostartprogramming.in/category/exceptions/" rel="dofollow"&gt;Exception Handling in Java&lt;/a&gt;&lt;br&gt;&lt;br&gt;
&lt;/p&gt;


</description>
      <category>exceptionhandlinginjava</category>
      <category>trycatchinjava</category>
      <category>trycatchfinally</category>
      <category>tryfinally</category>
    </item>
    <item>
      <title>Sorting using Comparable in java with example</title>
      <dc:creator>Rajesh Mishra</dc:creator>
      <pubDate>Sat, 04 Jul 2020 16:30:37 +0000</pubDate>
      <link>https://dev.to/rajesh1761/sorting-using-comparable-in-java-with-example-4p7o</link>
      <guid>https://dev.to/rajesh1761/sorting-using-comparable-in-java-with-example-4p7o</guid>
      <description>&lt;p&gt;&lt;strong&gt;Table of Contents&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
    &lt;li&gt;Basic points about the comparable interface.&lt;/li&gt;
    &lt;li&gt;Program to sort on the basis of the name using Comparable.&lt;/li&gt;
    &lt;li&gt;Program to sort on the basis of the id using Comparable.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id="Basic_points_about_the_comparable_interface"&gt;Basic points about the comparable interface.&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://docs.oracle.com/javase/7/docs/api/java/lang/Comparable.html"&gt;Comparable&lt;/a&gt; is an interface available in java.lang package, which provides single sorting(means we can sort either the basis of id or name at a time. Consider we have Employee class which contains fields name and id).&lt;/p&gt;

&lt;p&gt;The Comparable interface &lt;strong&gt;contains compare()&lt;/strong&gt; method. If we want to sort any collection of user-defined objects using comparable, our java class must need to implement Comparable interface and override &lt;strong&gt;compareTo(Object o)&lt;/strong&gt; method.&lt;/p&gt;

&lt;h2 id="Program_to_sort_on_the_basis_of_the_name_using_Comparable"&gt;Program to sort on the basis of the name using Comparable.&lt;/h2&gt;

&lt;ul&gt;
    &lt;li&gt;Define an Employee class which should implement &lt;a href="https://docs.oracle.com/javase/7/docs/api/java/lang/Comparable.html"&gt;Comparable&lt;/a&gt; interface and create two variables name and id.  Provide corresponding getter/setter and parameterized constructor. Here we are going to sort employee object on basis of Id.&lt;/li&gt;
    &lt;li&gt;Override compareTo () method and provide logic.&lt;/li&gt;
    &lt;li&gt;
&lt;blockquote&gt;
&lt;pre&gt;@Override
public int compareTo(Object o) {
        Employee employee = (Employee) o;
        String name = employee.name;
        return this.name.compareTo(name);
}
&lt;/pre&gt;
&lt;/blockquote&gt;
&lt;/li&gt;
    &lt;li&gt;Override compareTo () method and provide logic.&lt;/li&gt;
    &lt;li&gt;Create a couple of employee object using parameterized constructor and add into list.&lt;/li&gt;
    &lt;li&gt;
&lt;blockquote&gt;
&lt;pre&gt;List emplist = new ArrayList();
emplist.add(new Employee("John", 101));
emplist.add(new Employee("Eric", 106));
emplist.add(new Employee("Arya", 103));
emplist.add(new Employee("Sansa", 105));
emplist.add(new Employee("Cersei", 102));
&lt;/pre&gt;
&lt;/blockquote&gt;
&lt;/li&gt;
    &lt;li&gt;Use Collections.sort(emplist) the method which will internally call the compareTo() method(Check here how String compareTo() method internally works in java). Here Collections is a class and sort() is a static method. Use Java 8 forEach() method and print the name and id.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Comparable example in java:&lt;/p&gt;

&lt;pre&gt;import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
class Employee implements Comparable {
 private String name;
 private int id;
 public Employee(String string, int i) {
  this.name = string;
  this.id = i;
 }
 public String getName() {
  return name;
 }
 public void setName(String name) {
  this.name = name;
 }
 public int getId() {
  return id;
 }
 public void setId(int id) {
  this.id = id;
 }
 @Override 
 public int compareTo(Object o) {
  Employee employee = (Employee) o;
  String name = employee.name;
  return this.name.compareTo(name);
 }
}
public class SortingByNameUsingComprable {
 public static void main(String[] args) {
  List emplist = new ArrayList();
  emplist.add(new Employee("John", 101));
  emplist.add(new Employee("Eric", 106));
  emplist.add(new Employee("Arya", 103));
  emplist.add(new Employee("Sansa", 105));
  emplist.add(new Employee("Cersei", 102));
  Collections.sort(emplist);
  emplist.forEach(empolyee -&amp;gt; 
          System.out.println(empolyee.getName() + " " + empolyee.getId()));
 }
}&lt;/pre&gt;
&lt;blockquote&gt;
&lt;pre&gt;Arya 103
Cersei 102
Eric 106
John 101
Sansa 105
&lt;/pre&gt;




&lt;/blockquote&gt;

&lt;h2 id="Program_to_sort_on_the_basis_of_the_id_using_Comparable"&gt;Program to sort on the basis of the id using Comparable.&lt;/h2&gt;

&lt;p&gt;For sorting on basis of id we need to change compareTo() logic as below. The rest of the steps would be the same.&lt;/p&gt;

&lt;pre&gt;@Override public int compareTo(Object o) {
 Employee employee = (Employee) o;
 return this.getId() - employee.getId();
}&lt;/pre&gt;
&lt;pre&gt;import java.lang.reflect.Field;
import java.util.*;
 
class Employee implements Comparable {
    String name;
    int id;
 
    public Employee(String string, int i) {
        this.name = string;
        this.id = i;
    }
 
    public String getName() {
        return name;
    }
 
    public void setName(String name) {
        this.name = name;
    }
 
    public int getId() {
        return id;
    }
 
    public void setId(int id) {
        this.id = id;
    }
 
    @Override
    public int compareTo(Object o) {
        Employee employee = (Employee) o;
        return this.getId() - employee.getId();
 
    }
}
 
public class SortingByIdUsingComprable {
    public static void main(String[] args) {
        List emplist = new ArrayList();
        emplist.add(new Employee("John", 101));
        emplist.add(new Employee("Eric", 106));
        emplist.add(new Employee("Arya", 103));
        emplist.add(new Employee("Sansa", 105));
        emplist.add(new Employee("Cersei", 102));
 
        Collections.sort(emplist);
        emplist.forEach(empolyee -&amp;gt; System.out.println(empolyee.getName() + "   " + empolyee.getId()));
    }
}&lt;/pre&gt;
&lt;blockquote&gt;
&lt;pre&gt;John 101
Cersei 102
Arya 103
Sansa 105
Eric 106
&lt;/pre&gt;




&lt;/blockquote&gt;
&lt;br&gt;&lt;br&gt;
Learn more about&lt;strong&gt; &lt;a href="https://onlinetutorials.tech/java-collection/"&gt;collection in java&lt;/a&gt;&lt;/strong&gt;

</description>
      <category>collection</category>
      <category>java</category>
      <category>collectionsinjava</category>
      <category>comparable</category>
    </item>
  </channel>
</rss>
