<?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: Abhinav Pandey</title>
    <description>The latest articles on DEV Community by Abhinav Pandey (@abh1navv).</description>
    <link>https://dev.to/abh1navv</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%2F440730%2Ff0fc3149-bd2f-4450-99bb-39bdc8c95824.jpg</url>
      <title>DEV Community: Abhinav Pandey</title>
      <link>https://dev.to/abh1navv</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/abh1navv"/>
    <language>en</language>
    <item>
      <title>5 productivity hacks that nobody talks about</title>
      <dc:creator>Abhinav Pandey</dc:creator>
      <pubDate>Sun, 21 Aug 2022 07:22:20 +0000</pubDate>
      <link>https://dev.to/abh1navv/5-productivity-hacks-that-nobody-talks-about-1l6f</link>
      <guid>https://dev.to/abh1navv/5-productivity-hacks-that-nobody-talks-about-1l6f</guid>
      <description>&lt;p&gt;In this article, we will look at a few hacks which can enhance the productivity of an individual or a team. &lt;/p&gt;

&lt;p&gt;All these ways are sustainable to follow and work well when incorporated as habits. &lt;/p&gt;

&lt;h2&gt;
  
  
  1.  Reusing tools and knowledge
&lt;/h2&gt;

&lt;p&gt;One of the biggest mistakes people make is to start from scratch and re-invent the wheel.&lt;/p&gt;

&lt;p&gt;When faced with a problem, the first step should be to look for existing solutions. &lt;/p&gt;

&lt;p&gt;If an exact solution is not found, the target should be to compare the requirements to similar solved problems. &lt;br&gt;
Find similarities and differences. &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Reuse what you can and more importantly, document the new findings on every iteration.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;This is effective on a personal level but works even better when done on a team or organization level. &lt;/p&gt;

&lt;p&gt;Successful organizations spend a lot of effort in enforcing this re-usability which in turn helps their developers to be more productive.&lt;/p&gt;

&lt;h2&gt;
  
  
  2.  Speed over Perfection
&lt;/h2&gt;

&lt;p&gt;While perfection can be a target in some cases, speed is usually mandatory. Perfection on a high level design is not a bad idea, but it starts hurting the speed when it's desired on every small detail.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Delivering quality depends a lot on doing a lot of repetitive and trivial tasks correct and quickly.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Here are a few ideas to achieve this.&lt;/p&gt;

&lt;h3&gt;
  
  
  Delegate without micro-managing
&lt;/h3&gt;

&lt;p&gt;As developers, out state of mind is always to provide solutions. This becomes tough when you're dividing work with other people. &lt;/p&gt;

&lt;p&gt;While it's good to care about the big picture, it's a waste of time trying to ensure others are performing their tasks according to your thought process.  &lt;/p&gt;

&lt;p&gt;Trust the other person to do their job without you being involved at every step. Their output may not  match your expectations for the first few times, but it gets better after a few iterations. &lt;/p&gt;

&lt;p&gt;It helps them figure things out on their own and they build a better working relationship with you if they feel you trust them.&lt;/p&gt;

&lt;h3&gt;
  
  
  Have an Acceptance Criteria
&lt;/h3&gt;

&lt;p&gt;Establish an acceptance criteria early to give an idea of when to stop. The maintainability and adaptability of your code is also a part of this acceptance criteria. &lt;/p&gt;

&lt;p&gt;When you write down your target, it makes it difficult to overthink and over-engineer beyond it. &lt;/p&gt;

&lt;p&gt;Meeting this criteria is the first target of the developer. It takes priority over refactoring and making code look pretty.&lt;/p&gt;

&lt;h3&gt;
  
  
  Reduce Review Cycles
&lt;/h3&gt;

&lt;p&gt;Build guidelines that a team follows rather than relying on long reviews. This should be done for both code and processes.&lt;/p&gt;

&lt;p&gt;Use automated code quality and testing tools to validate PRs rather than a reviewer having to do it manually.&lt;/p&gt;

&lt;h2&gt;
  
  
  3.  Prototyping
&lt;/h2&gt;

&lt;p&gt;The best way to check the feasibility of a solution is to build a prototype and test it.&lt;br&gt;
Focus of the prototype should be to test the risky and unfamiliar parts of the solution. &lt;/p&gt;

&lt;p&gt;Once its successful(or unsuccessful), it is important to document the new findings.&lt;/p&gt;

&lt;p&gt;It is not required to do extensive research, long courses of study or multiple rounds of planning before getting your hands dirty with the code.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Working code brings more confidence than any notes, documentation or minutes of the meetings. &lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  4.  Power of the people
&lt;/h2&gt;

&lt;p&gt;Whether you work in a team or build a product on your own, it is important to leverage the power of the people.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Doing it all alone is not a good idea. It makes your chances of success limited to your own efforts.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Here are a few things when you should consider involving other people:&lt;/p&gt;

&lt;h3&gt;
  
  
  Asking for advice
&lt;/h3&gt;

&lt;p&gt;If you know someone who is good at the skills or problems you are trying to conquer, ask for their advice. Learn how they do things. Build upon their knowledge.&lt;/p&gt;

&lt;h3&gt;
  
  
  Validation and feedback
&lt;/h3&gt;

&lt;p&gt;Let your team or friends validate your ideas and give feedback. &lt;/p&gt;

&lt;p&gt;The earlier you get someone else's perspective on what you're about to do, the more time you have to make it better. Feedback at a late stage is not always helpful and harder to incorporate.&lt;/p&gt;

&lt;h3&gt;
  
  
  Ask for contributions
&lt;/h3&gt;

&lt;p&gt;Ask people to contribute in whatever way they can. They could share the workload with you, fix some bugs, review your code, or even help you with the design. &lt;/p&gt;

&lt;h3&gt;
  
  
  Brainstorm together
&lt;/h3&gt;

&lt;p&gt;Working on ideas together is a great way to get ideas flowing. It is a way to learn new approaches to solving a problem. It can also bring constant validation and feedback.&lt;/p&gt;

&lt;h3&gt;
  
  
  Share knowledge
&lt;/h3&gt;

&lt;p&gt;Sharing knowledge can build a community of like-minded people. It helps you network and builds trust. &lt;/p&gt;

&lt;p&gt;It can unintentionally lead to people pointing out flaws in your implementation or documentation and getting you to fix them.&lt;/p&gt;

&lt;h2&gt;
  
  
  5.  Self-reflection
&lt;/h2&gt;

&lt;p&gt;Everyone should take planned periodic breaks and reflect on their progress. This is similar to a sprint retrospective and let's use the same principles for our personal reflection.&lt;/p&gt;

&lt;p&gt;Here are a few things to consider in your reflection:&lt;/p&gt;

&lt;h3&gt;
  
  
  What can you do better?
&lt;/h3&gt;

&lt;p&gt;Focus on saving time and effort by finding opportunities to optimize the little things you do every day.&lt;/p&gt;

&lt;p&gt;Find opportunities to automate processes. Find ways to clear out the junk. &lt;strong&gt;Improve your work environment (physical and software) to support the plans you have for the near future&lt;/strong&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  What's hurting you?
&lt;/h3&gt;

&lt;p&gt;Reflect on the habits that are keeping you from getting things done. Find a sustainable way to &lt;strong&gt;reduce the time you spend on those habits&lt;/strong&gt;. &lt;/p&gt;

&lt;p&gt;It's also important to figure out your mental blockages. Pending tasks and annoying people weigh a lot on your subconscious mind and you are better off without them.&lt;/p&gt;

&lt;h3&gt;
  
  
  What's working well?
&lt;/h3&gt;

&lt;p&gt;Reflect on the things that are going well for you. &lt;strong&gt;It could be beneficial to invest more time and effort into it.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;If you work in a team, it's important to share this with people who's productivity affects yours. &lt;/p&gt;




&lt;p&gt;Thanks for reading. If you found this article useful, consider sharing it with your friends. I'd also love to know your own advice in the comments.&lt;/p&gt;

&lt;p&gt;And if you want to connect with me, you can find me on &lt;a href="https://www.twitter.com/abh1navv"&gt;Twitter&lt;/a&gt;&lt;/p&gt;

</description>
      <category>productivity</category>
      <category>career</category>
      <category>programming</category>
    </item>
    <item>
      <title>Why Cloud is the Future?</title>
      <dc:creator>Abhinav Pandey</dc:creator>
      <pubDate>Fri, 05 Aug 2022 04:46:38 +0000</pubDate>
      <link>https://dev.to/abh1navv/why-cloud-is-the-future-49b3</link>
      <guid>https://dev.to/abh1navv/why-cloud-is-the-future-49b3</guid>
      <description>&lt;p&gt;Let's look at a few reasons why organizations will keep investing in cloud computing and why learning about it will provide you a competitive edge over others.&lt;/p&gt;

&lt;h2&gt;
  
  
  Lowers Infrastructure Cost
&lt;/h2&gt;

&lt;p&gt;Instead of buying hardware, you are renting it. This means &lt;strong&gt;you only pay for what you use and for the time you use it.&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;The cloud provider takes care of maintaining and upgrading the hardware for you. This reduces the need for IT specialists.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;It also reduces the time required to procure hardware, software and licenses. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Temporarily required instances can be spun up and down for the duration that they are required. This is a great way to save cost of infrastructure required for testing.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Supports Fast-paced Innovation
&lt;/h2&gt;

&lt;p&gt;By abstracting the infrastructure setup, cloud enables you to focus on the application development.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;It provides PaaS and SaaS services which you can build on top of. This further reduces the need to reinvent the wheel for repetitive tasks.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Cost-effective concepts like Serverless make it easier to experiment with new products without requiring a significant initial investment.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;All these factors reduce the time to market and help organizations stay ahead of their competition.&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Battle-tested Security
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;You would rather use a tried and tested security solution than build you own.&lt;/strong&gt; Moreover, with cloud its mostly a matter of enabling a few configurations. &lt;/p&gt;

&lt;p&gt;Cloud providers provide a wide range of security features like:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Firewalls,&lt;/li&gt;
&lt;li&gt;DDoS protection,&lt;/li&gt;
&lt;li&gt;Fine-grained access control,&lt;/li&gt;
&lt;li&gt;Encryption,&lt;/li&gt;
&lt;li&gt;Compliance with data privacy and security regulations,&lt;/li&gt;
&lt;li&gt;and more.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Geographical Distribution
&lt;/h2&gt;

&lt;p&gt;It makes it easier to geographically distribute data and services.&lt;/p&gt;

&lt;p&gt;Database redundancy can be set up to replicate data across the world. Similarly, services can be deployed to several regions.&lt;/p&gt;

&lt;p&gt;This can have benefits like:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Low latency service to people in multiple countries,&lt;/li&gt;
&lt;li&gt;Safeguard against downtimes and data loss due to natural disasters.&lt;/li&gt;
&lt;li&gt;Eliminates single point of failures at a regional level. &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Your services can keep running even if a data center burns down.&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Elastic Scalability
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;You can scale your cloud services to meet spikes in load.&lt;/li&gt;
&lt;li&gt;More importantly, you can scale down to save cost when your users are less active.
-This makes it suitable for small to medium-sized businesses where it's important to save money in every way.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  High Availability
&lt;/h2&gt;

&lt;p&gt;Most cloud services come with an availability guarantee.This means that &lt;strong&gt;if a service fails, it's the provider's responsibility to ensure that the service is available again.&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;If it fails to meet the availability SLA, you get compensated for it.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Please note that you don't get compensated for downtimes due to your own fault - e.g. bad code, bad deployment strategy, under-provisioning, etc.&lt;/p&gt;




&lt;p&gt;These benefits motivate big organizations to more towards the cloud. These savings in cost, reliability and speed convert to millions of dollars for them.&lt;/p&gt;

&lt;p&gt;On the other hand, &lt;strong&gt;it enables startups to get started quickly and avoid huge initial investments.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;This translates into demand for skilled professionals with knowledge and experience in cloud technologies. &lt;/p&gt;

&lt;p&gt;Cloud skills have become a part of most usual software roles like DevOps, Backend development, Data, ML and AI. Additionally, new cloud specific roles have emerged.&lt;/p&gt;

</description>
      <category>cloud</category>
      <category>cloudskills</category>
      <category>programming</category>
      <category>career</category>
    </item>
    <item>
      <title>Building an Effective Code Review Process</title>
      <dc:creator>Abhinav Pandey</dc:creator>
      <pubDate>Wed, 03 Aug 2022 16:38:00 +0000</pubDate>
      <link>https://dev.to/abh1navv/building-an-effective-code-review-process-22d2</link>
      <guid>https://dev.to/abh1navv/building-an-effective-code-review-process-22d2</guid>
      <description>&lt;p&gt;Code reviews are not just a great way to ensure code quality, they're also a way to build standards, mentor people and learn from mistakes.&lt;/p&gt;

&lt;p&gt;Here are a few tips that can help a team in building an effective review process.&lt;/p&gt;

&lt;h2&gt;
  
  
  1. Create Small PRs
&lt;/h2&gt;

&lt;p&gt;The longer the PR, greater the chance that parts of it will be overlooked. Small and frequent PR reviews create a better feedback loop.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Break down big stories into smaller chunks which can be developed and reviewed independent of the rest of the code.&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  2. Document Requirements
&lt;/h2&gt;

&lt;p&gt;Referring requirements and acceptance criteria makes it easier for the reviewer to understand the intent of the code. It reduces the need for synchronous communication during the review.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Include links to document or tickets in the PR description.&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  3. Make Atomic Changes
&lt;/h2&gt;

&lt;p&gt;A PR should not have inconsistent code. &lt;/p&gt;

&lt;p&gt;There should be no TODOs, unfinished methods, commented code or failing test cases.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Whatever purpose the PR solves, it solves it completely. Otherwise, it's not ready for review.&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  4. Build Team Checklists
&lt;/h2&gt;

&lt;p&gt;A team should maintain a list of guidelines that developers must follow while coding and raising PRs. It helps establish expectations early.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Before raising a PR, developers should refer to the guidelines and ensure they are followed.&lt;/strong&gt; For e.g., these are some guidelines that should be followed while raising PRs:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Code formatting&lt;/li&gt;
&lt;li&gt;Comment guidelines&lt;/li&gt;
&lt;li&gt;Variable naming conventions&lt;/li&gt;
&lt;li&gt;Unit test coverage&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  5. Use Automated Code Quality Tools
&lt;/h2&gt;

&lt;p&gt;Use static code analyzers to validate PRs rather than having the reviewer decide if the code stinks or not.&lt;/p&gt;

&lt;p&gt;Reviewers should focus more on business requirements and rules that are specific to the project. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Use code quality checks in the build pipeline to break builds if quality or unit test metrics are not met.&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  6. It's a Learning Opportunity
&lt;/h2&gt;

&lt;p&gt;It's important to make code reviews a learning process and not one person pointing out mistakes of the other person. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Reviewing and being reviewed is a great way to share knowledge and perspectives.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Write useful review comments which help the developer understand the 'why' behind the suggestions. &lt;/p&gt;

&lt;p&gt;As a developer, take time to understand why the reviewer made a particular suggestion rather than just blindly accepting it.&lt;/p&gt;

&lt;h2&gt;
  
  
  7. Use Round-robin Reviews
&lt;/h2&gt;

&lt;p&gt;Give everyone a chance to review code and give more developers a chance to look at each other's code.&lt;/p&gt;

&lt;p&gt;It makes them comfortable in communicating with each other, distributes the review work efficiently and breaks knowledge silos within the team.&lt;/p&gt;

&lt;h2&gt;
  
  
  8. Use Positive Language
&lt;/h2&gt;

&lt;p&gt;The language used in a code review should be positive and constructive.&lt;/p&gt;

&lt;p&gt;As a reviewer, include positive comments if the code is well-written and well-commented.&lt;/p&gt;

&lt;p&gt;Suggestions should be soft like, "can we try this?" or "I think this is a good idea".&lt;/p&gt;

&lt;p&gt;There should be no rigid and commanding language like "do this instead". &lt;strong&gt;Any review comment should be open to discussion.&lt;/strong&gt;&lt;/p&gt;




&lt;p&gt;Thanks for reading. I hope it gives you a few ideas on what to improve in your current review process. If you have some code review tips, feel free to add them in the replies.&lt;/p&gt;

&lt;p&gt;If you want to connect with me, you can find me on &lt;a href="https://www.twitter.com/abh1navv"&gt;Twitter&lt;/a&gt;&lt;/p&gt;

</description>
      <category>codereview</category>
      <category>discuss</category>
      <category>codequality</category>
      <category>programming</category>
    </item>
    <item>
      <title>An Approach to Continuous Learning</title>
      <dc:creator>Abhinav Pandey</dc:creator>
      <pubDate>Sun, 17 Jul 2022 05:27:18 +0000</pubDate>
      <link>https://dev.to/abh1navv/an-approach-to-continuous-learning-30pk</link>
      <guid>https://dev.to/abh1navv/an-approach-to-continuous-learning-30pk</guid>
      <description>&lt;p&gt;Being a successful developer requires continuous learning and skill improvement. But there are simply too many things to learn and too little time.&lt;/p&gt;

&lt;p&gt;Let's see a few principles with which we can invest our learning efforts effectively and receive maximum gains.&lt;/p&gt;

&lt;h2&gt;
  
  
  Small improvements everyday
&lt;/h2&gt;

&lt;p&gt;First and foremost it's important to invest a little time and energy every (normal) day.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Build a habit with a set time and place to try something new - learn, build, practice. &lt;/li&gt;
&lt;li&gt;1% better everyday makes you twice as good in 70 days. (probably has been said too many times)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Building this habit requires a little effort but everything seems effortless once we get there. &lt;/p&gt;

&lt;p&gt;A few things that help in the process:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Making long-term and short-term to-do lists&lt;/li&gt;
&lt;li&gt;Setting learning reminders(on a calendar rather than in the mind)&lt;/li&gt;
&lt;li&gt;An environment that enables learning and avoids distraction&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Get Good Enough at Your Niche
&lt;/h2&gt;

&lt;p&gt;Everyone needs to be good at the particular technologies they are working with. &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;If you work, the first target is to get good at your job. &lt;/li&gt;
&lt;li&gt;If you're looking for work, focus on the role you want to land. &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Invest your learning time and efforts on the most essential skills needed for your job. This includes both technical skills and soft skills. &lt;/p&gt;

&lt;h2&gt;
  
  
  Then Diversify
&lt;/h2&gt;

&lt;p&gt;Once you're good at the necessary skill, it's time to look into more technologies.&lt;/p&gt;

&lt;p&gt;There are two directions to explore:&lt;/p&gt;

&lt;h3&gt;
  
  
  1. Tools that go well with your niche.
&lt;/h3&gt;

&lt;p&gt;My personal trick is to look at job descriptions corresponding to my current role and look for gaps in my knowledge. &lt;/p&gt;

&lt;p&gt;The execution strategy is to first master the essentials, then build and then go deeper. &lt;/p&gt;

&lt;p&gt;Getting good at what works well with your niche gets you those hikes, promotions, job switches. It makes the most sense to spend time on this.  &lt;/p&gt;

&lt;h3&gt;
  
  
  2. Tools that compete with your niche.
&lt;/h3&gt;

&lt;p&gt;If something exists as a competition to your current tools, its worth knowing why it exists, what are the differences and how to choose between the two. &lt;/p&gt;

&lt;p&gt;A different database, framework, library and sometimes programming language comes into this category.&lt;/p&gt;

&lt;p&gt;This makes you capable of taking informed decisions at work. It also makes you future-ready if you ever need to switch your tech stack or parts of it. &lt;/p&gt;

&lt;h2&gt;
  
  
  Keep an eye on shiny objects
&lt;/h2&gt;

&lt;p&gt;Too much time spent into shiny objects or switching from one context to other can waste time and energy. However, it doesn't mean you ignore upcoming technologies. &lt;/p&gt;

&lt;p&gt;It is worth keeping an eye at promising things happening around you. Learning something that is still in the process of getting popular can be a head-start for you. If the thing has a good future, it can be highly rewarding for you.&lt;/p&gt;

&lt;p&gt;Have a little FOMO when you see people hyping something. But at the same time, invest your time responsibly.&lt;/p&gt;

&lt;p&gt;The first questions to ask are,&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;What are the chances of this new thing being used at scale in the industry? &lt;/li&gt;
&lt;li&gt;What are the chances of you using it as part of your job?&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Continuous retrospection and re-planning
&lt;/h2&gt;

&lt;p&gt;To not get overwhelmed by the number of technologies to learn and also to stay on the right course, it's important to keep yourself in check.&lt;/p&gt;

&lt;p&gt;Make time for reviewing your path once in a while. To check where you're investing time and if there are any changes you can make to make it more effective.&lt;/p&gt;

&lt;p&gt;Questions to ask yourself during the review:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;what's urgent and needs to be prioritised, &lt;/li&gt;
&lt;li&gt;what's outdated and needs to be cut down and &lt;/li&gt;
&lt;li&gt;what needs to be revisited. &lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;Thanks for reading. Hopefully it gives you a few helpful points. If you want to connect with me, you can find me on &lt;a href="https://www.twitter.com/abh1navv"&gt;Twitter&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>productivity</category>
      <category>discuss</category>
      <category>beginners</category>
      <category>career</category>
    </item>
    <item>
      <title>Tips to Optimize Backend Performance</title>
      <dc:creator>Abhinav Pandey</dc:creator>
      <pubDate>Wed, 22 Jun 2022 15:53:50 +0000</pubDate>
      <link>https://dev.to/abh1navv/tips-to-optimize-backend-performance-3nmd</link>
      <guid>https://dev.to/abh1navv/tips-to-optimize-backend-performance-3nmd</guid>
      <description>&lt;p&gt;As the number of users/requests of an application increases, it becomes important that it maintains its performance.&lt;/p&gt;

&lt;p&gt;Let's look at a few improvements that we can do to enhance application performance.&lt;/p&gt;

&lt;h2&gt;
  
  
  Query Optimization
&lt;/h2&gt;

&lt;p&gt;Make database queries more efficient using one of these ways:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Use only indexed columns in a query&lt;/li&gt;
&lt;li&gt;Make fewer database calls by using joins when advantageous.&lt;/li&gt;
&lt;li&gt;Use a query cache to avoid making duplicate queries.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Caching
&lt;/h2&gt;

&lt;p&gt;Use a cache(distributed or local) to store data that is frequently accessed. This may include:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;results of database queries&lt;/li&gt;
&lt;li&gt;results of expensive computations&lt;/li&gt;
&lt;li&gt;responses to frequent idempotent requests&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Database optimization
&lt;/h2&gt;

&lt;p&gt;Don't just optimize the queries, also optimize the way data is being stored.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Use indexes to make queries faster&lt;/li&gt;
&lt;li&gt;Use normalization to clean up data, separate data into meaningful chunks, or make it easier to query&lt;/li&gt;
&lt;li&gt;Alternatively, use denormalization when possible to reduce the need to use joins for tables always queried together.&lt;/li&gt;
&lt;li&gt;If using NoSQL, make use of shards and partitions for faster lookup.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  CDN
&lt;/h2&gt;

&lt;p&gt;CDNs are content delivery networks that provide content to the user.&lt;/p&gt;

&lt;p&gt;The main use of CDNs is to serve static content such as images, CSS, and JavaScript from locations that are geographically close to the user.&lt;/p&gt;

&lt;p&gt;They can also serve dynamically generated content including:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;JSON data in GET requests which does not change frequently with respect to its URL&lt;/li&gt;
&lt;li&gt;HTML content that is generated on the server-side&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Fail fast
&lt;/h2&gt;

&lt;p&gt;Make sure your application does not spend time processing bad requests.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Validate requests as soon as they are received&lt;/li&gt;
&lt;li&gt;Use rate limiting and deduplication to avoid DDoS attacks. &lt;/li&gt;
&lt;li&gt;If calling other services, use a circuit breaker to prevent cascading failures&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Keep the network light
&lt;/h2&gt;

&lt;p&gt;Use lighter request and response formats to reduce the amount of data that is sent over the network.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Use PATCH requests instead of PUT if updating small parts of a resource&lt;/li&gt;
&lt;li&gt;Prefer JSON over XML because of the compactness of the format.&lt;/li&gt;
&lt;li&gt;Use the GraphQL approach if querying large amounts of data but need only specific values back.&lt;/li&gt;
&lt;li&gt;Use compression for APIs which return large sizes (MBs) of data.&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;Thanks for reading. This should give you some ideas which you can implement in your low-level design to optimize performance. &lt;/p&gt;

&lt;p&gt;These are only some of the popular techniques. Feel free to add any of your advice in the comments. If you want to connect with me, you can find me on &lt;a href="https://twitter.com/abh1navv"&gt;Twitter&lt;/a&gt;&lt;/p&gt;

</description>
      <category>api</category>
      <category>programming</category>
      <category>architecture</category>
      <category>performance</category>
    </item>
    <item>
      <title>A Guide to Java Records</title>
      <dc:creator>Abhinav Pandey</dc:creator>
      <pubDate>Sun, 15 May 2022 06:44:06 +0000</pubDate>
      <link>https://dev.to/abh1navv/a-guide-to-java-records-5gic</link>
      <guid>https://dev.to/abh1navv/a-guide-to-java-records-5gic</guid>
      <description>&lt;p&gt;In this tutorial, we will cover the basics of how to use records in Java.&lt;br&gt;
Records were introduced in Java 14 as a way to remove boilerplate code around the creation of value objects while incorporating the benefits of immutable objects.&lt;/p&gt;
&lt;h2&gt;
  
  
  1. Basic Concepts
&lt;/h2&gt;

&lt;p&gt;Before moving on to Records, let's look at the problem Records solve. To understand this, let's examine how value objects were created before Java 14.&lt;/p&gt;
&lt;h3&gt;
  
  
  1.1. Value Objects
&lt;/h3&gt;

&lt;p&gt;Value objects are an integral part of Java applications. They store data that needs to be transferred between layers of the application.&lt;/p&gt;

&lt;p&gt;A value object contains fields, constructors and methods to access those fields.&lt;br&gt;
Below is an example of a value object:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Contact&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;email&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nf"&gt;Contact&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;email&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
        &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;email&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;email&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="nf"&gt;getName&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="nf"&gt;getEmail&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;email&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  1.2. Equality between Value Objects
&lt;/h3&gt;

&lt;p&gt;Additionally, the value objects may provide a way to compare them for equality.&lt;/p&gt;

&lt;p&gt;By default, Java compares the equality of objects by comparing their memory address. However, in some cases, objects containing the same data may be considered equal.&lt;br&gt;
To implement this, we can override the &lt;code&gt;equals&lt;/code&gt; and &lt;code&gt;hashCode&lt;/code&gt; methods.&lt;/p&gt;

&lt;p&gt;Let's implement them for the &lt;em&gt;Contact&lt;/em&gt; class:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Contact&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;

    &lt;span class="c1"&gt;// ...&lt;/span&gt;

    &lt;span class="nd"&gt;@Override&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;boolean&lt;/span&gt; &lt;span class="nf"&gt;equals&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Object&lt;/span&gt; &lt;span class="n"&gt;o&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;o&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;o&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="n"&gt;getClass&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="n"&gt;o&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getClass&lt;/span&gt;&lt;span class="o"&gt;())&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
        &lt;span class="nc"&gt;Contact&lt;/span&gt; &lt;span class="n"&gt;contact&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Contact&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="n"&gt;o&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nc"&gt;Object&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;equals&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;email&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;contact&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;email&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt;
                &lt;span class="nc"&gt;Objects&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;equals&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;contact&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="nd"&gt;@Override&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;hashCode&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nc"&gt;Objects&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;hash&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;email&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  1.3. Immutability of Value Objects
&lt;/h3&gt;

&lt;p&gt;Value objects should be immutable. This means that we should restrict ways to change the fields of the object.&lt;/p&gt;

&lt;p&gt;This is advisable for the below reasons:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;To avoid the risk of accidentally changing the value of a field.&lt;/li&gt;
&lt;li&gt;To make sure equal objects remain equal throughout their lifetime.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The Contact class is already immutable. We have:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Made the fields &lt;em&gt;private&lt;/em&gt; and &lt;em&gt;final&lt;/em&gt;.&lt;/li&gt;
&lt;li&gt;Provided only a &lt;em&gt;getter&lt;/em&gt; for each field and no &lt;em&gt;setters&lt;/em&gt;.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  1.4. Logging Value Objects
&lt;/h3&gt;

&lt;p&gt;We will often need to log the values that the objects contain. This is done by providing a &lt;code&gt;toString&lt;/code&gt; method.&lt;br&gt;
Whenever an object is logged or printed, the &lt;code&gt;toString&lt;/code&gt; method is called.&lt;/p&gt;

&lt;p&gt;The easiest way is to print each field's value. Here is an example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Contact&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// ...&lt;/span&gt;
    &lt;span class="nd"&gt;@Override&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="nf"&gt;toString&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s"&gt;"Contact["&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt;
                &lt;span class="s"&gt;"name='"&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="sc"&gt;'\''&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt;
                &lt;span class="s"&gt;", email="&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;email&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt;
                &lt;span class="sc"&gt;']'&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  2. Reducing Boilerplate with Records
&lt;/h2&gt;

&lt;p&gt;Since most value objects have the same needs and functionality, it was a good idea to make the process of creating them easier.&lt;br&gt;
Let's look at how Records achieve this.&lt;/p&gt;
&lt;h3&gt;
  
  
  2.1. Converting Person Class to a Record
&lt;/h3&gt;

&lt;p&gt;Let's create a record of the Contact class which has the same functionality as the Contact class defined above.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="n"&gt;record&lt;/span&gt; &lt;span class="nf"&gt;Contact&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;email&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The 'record' keyword is used to create a record class. Records can be treated exactly like a class by a caller.&lt;br&gt;
For e.g, to create a new instance of the record, we can use the &lt;code&gt;new&lt;/code&gt; keyword.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nc"&gt;Contact&lt;/span&gt; &lt;span class="n"&gt;contact&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Contact&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"John Doe"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"johnrocks@gmail.com"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  2.2. Default Behaviour
&lt;/h3&gt;

&lt;p&gt;We have reduced the code to a single line. Let's list down what this includes:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;The &lt;em&gt;name&lt;/em&gt; and &lt;em&gt;email&lt;/em&gt; fields are private and final by default.&lt;/li&gt;
&lt;li&gt;It defines a constructor which takes the fields as parameters.&lt;/li&gt;
&lt;li&gt;The fields are accessible via getter-like methods - &lt;code&gt;name()&lt;/code&gt; and &lt;code&gt;email()&lt;/code&gt;. There is no setter for the fields so the data in the object becomes immutable.&lt;/li&gt;
&lt;li&gt;A &lt;code&gt;toString&lt;/code&gt; method is implemented to print the fields the same as we did for the Contact class.&lt;/li&gt;
&lt;li&gt;The &lt;code&gt;equals&lt;/code&gt; and &lt;code&gt;hashCode&lt;/code&gt; methods are implemented. They include all the fields just like the Contact class.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  3. Working with Records
&lt;/h2&gt;

&lt;p&gt;We may want to change the behavior of the record in multiple ways. Let's look at some use cases and how to achieve them.&lt;/p&gt;

&lt;h3&gt;
  
  
  3.1. Overriding default implementations
&lt;/h3&gt;

&lt;p&gt;Any default implementation can be changed by overriding it. E.g. if we want to change the behavior of the &lt;code&gt;toString&lt;/code&gt; method, we can override it between the braces {}.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="n"&gt;record&lt;/span&gt; &lt;span class="nf"&gt;Contact&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;email&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="nd"&gt;@Override&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="nf"&gt;toString&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s"&gt;"Contact["&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt;
                &lt;span class="s"&gt;"name is '"&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="sc"&gt;'\''&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt;
                &lt;span class="s"&gt;", email is"&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;email&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt;
                &lt;span class="sc"&gt;']'&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Similarly, we can override the &lt;code&gt;equals&lt;/code&gt; and &lt;code&gt;hashCode&lt;/code&gt; methods as well.&lt;/p&gt;

&lt;h3&gt;
  
  
  3.2. Compact Constructors
&lt;/h3&gt;

&lt;p&gt;Sometimes, we want constructors to do more than just initialize the fields.&lt;br&gt;
We can add these operations to our record in a compact constructor. It's called compact because it does not need to define the initialization of fields or the parameter list.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="n"&gt;record&lt;/span&gt; &lt;span class="nf"&gt;Contact&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;email&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nc"&gt;Contact&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="o"&gt;(!&lt;/span&gt;&lt;span class="n"&gt;email&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;contains&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"@"&lt;/span&gt;&lt;span class="o"&gt;))&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;IllegalArgumentException&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Invalid email"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="o"&gt;}&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Note that there is no parameter list and initialization of name and email takes place in the background before the validation is performed.&lt;/strong&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  3.3. Adding Constructors
&lt;/h3&gt;

&lt;p&gt;We can add more constructors to our record. Let's see a few examples and a couple of restrictions.&lt;/p&gt;

&lt;p&gt;First, let's add new valid constructors:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="n"&gt;record&lt;/span&gt; &lt;span class="nf"&gt;Contact&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;email&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nf"&gt;Contact&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;email&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"John Doe"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;email&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="c1"&gt;// replaces the default constructor&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nf"&gt;Contact&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;email&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
        &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;email&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;email&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the first constructor, the default constructor is accessed using the &lt;em&gt;this&lt;/em&gt; keyword.&lt;br&gt;
The second constructor overrides the default constructor because it has the same parameter list. In this case, the record will not create a default constructor on its own.&lt;/p&gt;

&lt;p&gt;There are a few restrictions on the constructors.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1. The default constructor should always be called from any other constructor.&lt;/strong&gt;&lt;br&gt;
E.g., the below code will not compile:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="n"&gt;record&lt;/span&gt; &lt;span class="nf"&gt;Contact&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;email&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nf"&gt;Contact&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"John Doe"&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
        &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;email&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This rule ensures that fields are always initialized. It also ensures that the operations defined in the compact constructor are always executed.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2. Cannot override the default constructor if a compact constructor is defined.&lt;/strong&gt;&lt;br&gt;
When a compact constructor is defined, a default constructor is automatically constructed with the initialization and compact constructor logic.&lt;br&gt;
In this case, the compiler won't allow us to define a constructor with the same arguments as the default constructor.&lt;/p&gt;

&lt;p&gt;E.g., this won't compile:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="n"&gt;record&lt;/span&gt; &lt;span class="nf"&gt;Contact&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;email&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nc"&gt;Contact&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="o"&gt;(!&lt;/span&gt;&lt;span class="n"&gt;email&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;contains&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"@"&lt;/span&gt;&lt;span class="o"&gt;))&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;IllegalArgumentException&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Invalid email"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="o"&gt;}&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nf"&gt;Contact&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;email&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
        &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;email&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;email&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  3.4. Implementing Interfaces
&lt;/h3&gt;

&lt;p&gt;Just like any class, we can implement interfaces in our record.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="n"&gt;record&lt;/span&gt; &lt;span class="nf"&gt;Contact&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;email&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="kd"&gt;implements&lt;/span&gt; &lt;span class="nc"&gt;Comparable&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Contact&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="nd"&gt;@Override&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;compareTo&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Contact&lt;/span&gt; &lt;span class="n"&gt;o&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;compareTo&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;o&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Important Note:&lt;/strong&gt; To ensure complete immutability, records are not allowed to participate in inheritance. &lt;strong&gt;Records are final and cannot be extended. Nor can they extend other classes.&lt;/strong&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  3.5. Adding Methods
&lt;/h3&gt;

&lt;p&gt;In addition to constructors, overriding methods and implementing interfaces, we can also add any methods we want.&lt;/p&gt;

&lt;p&gt;For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="n"&gt;record&lt;/span&gt; &lt;span class="nf"&gt;Contact&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;email&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="nf"&gt;printName&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s"&gt;"My name is:"&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We can also add static methods. For example, if we wanted to have a static method that returns the regex against which emails can be validated, we can define it as below:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="n"&gt;record&lt;/span&gt; &lt;span class="nf"&gt;Contact&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;email&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="nc"&gt;Pattern&lt;/span&gt; &lt;span class="nf"&gt;emailRegex&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nc"&gt;Pattern&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;compile&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"^[A-Z0-9._%+-]+@[A-Z0-9.-]+\\.[A-Z]{2,6}$"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;Pattern&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;CASE_INSENSITIVE&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  3.6. Adding Fields
&lt;/h3&gt;

&lt;p&gt;We cannot add instance fields to our record. However, we can add static fields.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="n"&gt;record&lt;/span&gt; &lt;span class="nf"&gt;Contact&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;email&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="nc"&gt;Pattern&lt;/span&gt; &lt;span class="no"&gt;EMAIL_REGEX_PATTERN&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Pattern&lt;/span&gt;
            &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;compile&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"^[A-Z0-9._%+-]+@[A-Z0-9.-]+\\.[A-Z]{2,6}$"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;Pattern&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;CASE_INSENSITIVE&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;

    &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="nc"&gt;Pattern&lt;/span&gt; &lt;span class="nf"&gt;emailRegex&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="no"&gt;EMAIL_REGEX_PATTERN&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Note that there are no implicit restrictions on the visibility of static fields. They can be public if needed and may not be final.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;Records are a great way to define data classes. They are a lot more powerful than the JavaBeans/POJO approach.&lt;br&gt;
Because of their ease of implementation, they should be preferred over other approaches for creating value objects.&lt;/p&gt;




&lt;p&gt;Thanks for reading. If you have any questions/suggestions, please feel free to mention them in the comments.&lt;/p&gt;

&lt;p&gt;If you want to connect with me, you can find me on &lt;a href="https://twitter.com/abh1navv"&gt;Twitter&lt;/a&gt;&lt;/p&gt;

</description>
      <category>java</category>
      <category>programming</category>
      <category>tutorial</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Tips for Faster Debugging in IntelliJ</title>
      <dc:creator>Abhinav Pandey</dc:creator>
      <pubDate>Fri, 22 Apr 2022 03:34:00 +0000</pubDate>
      <link>https://dev.to/abh1navv/tips-for-faster-debugging-in-intellij-28d9</link>
      <guid>https://dev.to/abh1navv/tips-for-faster-debugging-in-intellij-28d9</guid>
      <description>&lt;p&gt;Top IntelliJ tricks to debug issues faster. (excluding the debugger)&lt;/p&gt;

&lt;h2&gt;
  
  
  Fold Logs
&lt;/h2&gt;

&lt;p&gt;Running an application can produce a lot of console output. Most of it is not useful and comes from third party libraries. You can fold console output lines coming from these libraries. This will help you focus on the important information.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Select the class name on the log line and right click &lt;/li&gt;
&lt;li&gt;Select "Fold lines like this".&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;It will open a window and add this class to the list of classes to fold. You can further edit this class name to a package name to fold all classes in that package.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fpbs.twimg.com%2Fmedia%2FFQ4Q-3kVkAAW6D2.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fpbs.twimg.com%2Fmedia%2FFQ4Q-3kVkAAW6D2.png" alt="Fold Lines Pop-up in IntelliJ"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Search With Google
&lt;/h2&gt;

&lt;p&gt;You can trigger a Google search from anywhere in the IDE - code, console, log files, text files, etc.&lt;/p&gt;

&lt;p&gt;For e.g., when you see an error in the logs, &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Select the piece of text you want to search &lt;/li&gt;
&lt;li&gt;Right click &lt;/li&gt;
&lt;li&gt;Search with Google.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fpbs.twimg.com%2Fmedia%2FFQ4Q_R5VkAAbZLo.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fpbs.twimg.com%2Fmedia%2FFQ4Q_R5VkAAbZLo.png" alt="Search with Google Option"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;While this is a trivial thing, it gets you to StackOverflow faster. &lt;/p&gt;

&lt;p&gt;If you want to take this one step further and stop the monopoly, use the Smart Search plugin which lets you use custom search engines. &lt;/p&gt;

&lt;p&gt;You can even search on StackOverflow directly and cut out the middleman.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fpbs.twimg.com%2Fmedia%2FFQ4Q_sgVsAEIEiJ.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fpbs.twimg.com%2Fmedia%2FFQ4Q_sgVsAEIEiJ.png" alt="Custom search engine settings in Smart Search"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Compare files with Clipboard
&lt;/h2&gt;

&lt;p&gt;You can compare contents of a file with the content in the clipboard. This is useful in comparing files which are not part of version control.&lt;/p&gt;

&lt;p&gt;This can also serve as a quick way to compare and merge two files.&lt;/p&gt;

&lt;p&gt;A usual usage scenario is to compare configuration files.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fpbs.twimg.com%2Fmedia%2FFQ4RAFQVgAEhCN9.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fpbs.twimg.com%2Fmedia%2FFQ4RAFQVgAEhCN9.png" alt="Compare with clipboard"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Show Git History for a Piece of Code
&lt;/h2&gt;

&lt;p&gt;You see a code, but you don't know what it does (or why it does what it does). &lt;/p&gt;

&lt;p&gt;To get an idea, you need to know why the code was written and/or who wrote it. &lt;/p&gt;

&lt;p&gt;Find it out using the Git history. &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Select the piece of code &lt;/li&gt;
&lt;li&gt;Right click &lt;/li&gt;
&lt;li&gt;Go to the Git section and select "Show history for selection".&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This will list down all the old commits which included changes in the code. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fpbs.twimg.com%2Fmedia%2FFQ4RAnwUYAEpNxr.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fpbs.twimg.com%2Fmedia%2FFQ4RAnwUYAEpNxr.png" alt="Show Git History"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Compare code with a working copy
&lt;/h2&gt;

&lt;p&gt;Something worked in the past, but now it doesn't. A great way to debug it is to check the changes that have been made to it since then.&lt;/p&gt;

&lt;p&gt;There are multiple ways to do this.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Use the Git option "Compare with branch" and compare the current code to the code in a branch that worked fine.&lt;/li&gt;
&lt;li&gt;Use the Git option "Compare with Revision" and compare it to the code in a commit that worked fine.&lt;/li&gt;
&lt;li&gt;Compare entire current branch with another branch. This is useful to understand multiple changes together instead of looking at a small piece of code.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Local History
&lt;/h2&gt;

&lt;p&gt;You can see the history of changes you made to a file in the local history. IntelliJ maintains a local history for each file (yes, even uncommitted code is safe).&lt;/p&gt;

&lt;p&gt;This can be helpful when recovering from accidental deletions or to recover an intermediate state of a file.&lt;/p&gt;

&lt;p&gt;For e.g. You made the code work, tested it and then went on to refactor it before committing. If the code doesn't work, you can recover the previous working state of the file from the local history or just compare it to understand what you did wrong.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fpbs.twimg.com%2Fmedia%2FFQ4RB0iVIAAu7Pt.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fpbs.twimg.com%2Fmedia%2FFQ4RB0iVIAAu7Pt.jpg" alt="John Cena in Suicide Squad Meme - used to portray IntelliJ does everything Eclipse does, but better"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;p&gt;Thank you for reading. &lt;a href="https://dev.tourl"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;More importantly, if you have any tips of your own, feel free to share them in the comments.&lt;/p&gt;

</description>
      <category>java</category>
      <category>discuss</category>
      <category>programming</category>
      <category>productivity</category>
    </item>
    <item>
      <title>The Ultimate Guide to Backtracking</title>
      <dc:creator>Abhinav Pandey</dc:creator>
      <pubDate>Tue, 19 Apr 2022 17:13:07 +0000</pubDate>
      <link>https://dev.to/abh1navv/the-ultimate-guide-to-backtracking-g5j</link>
      <guid>https://dev.to/abh1navv/the-ultimate-guide-to-backtracking-g5j</guid>
      <description>&lt;p&gt;Backtracking is a technique for solving problems by exploring all possible solutions. &lt;/p&gt;

&lt;p&gt;Backtracking problems ask us to find combinations or permutations. These Ps &amp;amp; Cs are then matched against certain conditions or smallest/greatest logic.  Here are a couple of examples:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Given an array of numbers, find all the possible combinations of numbers that add up to a given number.&lt;/li&gt;
&lt;li&gt;Given a collection of distinct integers, return all possible permutations of them.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In this article, we will use backtracking to solve these two problems.  In doing so, we will go through each step of the process in detail.&lt;/p&gt;

&lt;p&gt;We will also look at a few important variations of these problems and how to change our solution to solve them.&lt;/p&gt;

&lt;p&gt;In short, we can drill down backtracking to the below steps:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Start from a state.&lt;/li&gt;
&lt;li&gt;If the current state is a solution, add the current state to the result.&lt;/li&gt;
&lt;li&gt;If not, try each of the possible moves from the current state.&lt;/li&gt;
&lt;li&gt;Once we have tried all the possible moves, backtrack to the previous state.&lt;/li&gt;
&lt;li&gt;Repeat the above steps until a solution is found or all possibilities have been exhausted.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Problem 1: Combinations
&lt;/h2&gt;

&lt;p&gt;Let's solve the below problem:&lt;/p&gt;

&lt;p&gt;_Given a list of distinct positive numbers, find all combinations that add up to a given number.&lt;/p&gt;

&lt;p&gt;Input: [1, 2, 3, 4], target = 5&lt;br&gt;
Output: [[2, 3], [1, 4]]_&lt;/p&gt;

&lt;p&gt;Let's use the backtracking technique to solve this problem.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Disclaimer&lt;/strong&gt;: There are many ways to solve this problem. I have chosen a way that will be easy to explain. I have left out any clever tricks to keep the code simple.&lt;br&gt;
The programming examples are in Java but do not use any special data structures.&lt;/p&gt;
&lt;h3&gt;
  
  
  Initial State
&lt;/h3&gt;

&lt;p&gt;The initial state is an empty list. We are going to start from this state and try to add numbers to it.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;We will keep track of&lt;/strong&gt;:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;The current state or list of numbers used. &lt;/li&gt;
&lt;li&gt;The sum of the numbers in the list.
&lt;/li&gt;
&lt;/ol&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nc"&gt;List&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;List&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Integer&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;combinationSum&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;target&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// will store the final result&lt;/span&gt;
    &lt;span class="nc"&gt;List&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;List&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Integer&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;res&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;ArrayList&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&amp;gt;();&lt;/span&gt; 

    &lt;span class="c1"&gt;// keeps track of the current state&lt;/span&gt;
    &lt;span class="nc"&gt;List&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Integer&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;current&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;ArrayList&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&amp;gt;();&lt;/span&gt;

    &lt;span class="c1"&gt;// call the helper function which does the processing &lt;/span&gt;
    &lt;span class="c1"&gt;// first 0 is the sum of the numbers in the current state&lt;/span&gt;
    &lt;span class="c1"&gt;// second 0 is the index to start from&lt;/span&gt;
    &lt;span class="n"&gt;backtrack&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;target&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;current&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;res&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;res&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;h3&gt;
  
  
  Check if the current state is a solution
&lt;/h3&gt;

&lt;p&gt;We need to check if the current state is a solution.&lt;br&gt;
To do so, we need to check if the sum of the numbers in the current state is equal to the target.&lt;/p&gt;

&lt;p&gt;If yes, we will add the current state to the final result.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;backtrack&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;target&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;sum&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;index&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;List&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Integer&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;current&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;List&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;List&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Integer&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;res&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// if the sum of the numbers in the current state is equal to the target,&lt;/span&gt;
    &lt;span class="c1"&gt;// then we have found a solution&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;target&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;sum&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="c1"&gt;// copy the content of the current state to a new list and save to result&lt;/span&gt;
        &lt;span class="n"&gt;res&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;add&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;ArrayList&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&amp;gt;(&lt;/span&gt;&lt;span class="n"&gt;current&lt;/span&gt;&lt;span class="o"&gt;));&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;sum&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;target&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="c1"&gt;// short circuit, if the sum of the numbers in the current state is greater than the target,&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="c1"&gt;// to be continued...&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Try each of the possible moves and backtrack
&lt;/h3&gt;

&lt;p&gt;If the current state is not a solution, we need to try each of the possible moves.&lt;/p&gt;

&lt;p&gt;The possible moves are all the numbers in the nums array that comes on or after the index.&lt;/p&gt;

&lt;p&gt;We can divide this into 3 steps:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Add a number to the current list to get to the next state.&lt;/li&gt;
&lt;li&gt;Call the backtrack function to check if the next state is a solution. Update the index and sum being sent to the backtrack function.&lt;/li&gt;
&lt;li&gt;Remove the number from the current list to get back to the previous state. The next iteration will try the next number.
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;backtrack&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;target&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;sum&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;index&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;List&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Integer&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;current&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;List&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;List&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Integer&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;res&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// checking for solution - omitted&lt;/span&gt;

    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;index&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;length&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="c1"&gt;// add the current number to the current state&lt;/span&gt;
        &lt;span class="n"&gt;current&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;add&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;]);&lt;/span&gt;

        &lt;span class="c1"&gt;// call the helper function to try the next state - Current sum is updated and index is increased to the next number&lt;/span&gt;
        &lt;span class="n"&gt;backtrack&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;target&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;sum&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;],&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;current&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;res&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;

        &lt;span class="c1"&gt;// remove the current number from the current state&lt;/span&gt;
        &lt;span class="n"&gt;current&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;remove&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;current&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;size&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Variation 1: Numbers are not distinct
&lt;/h3&gt;

&lt;p&gt;If we ran the same solution, but with the numbers not being distinct, we would get duplicate results.&lt;br&gt;
For e.g. [1, 2, 2, 3, 4] will return [[1, 2, 2], [1,4], [2, 3], [2, 3]]&lt;/p&gt;
&lt;h4&gt;
  
  
  Solution 1: Set to Store Result
&lt;/h4&gt;

&lt;p&gt;The simplest way to solve this problem is to use a Set to keep track of the final result and convert the set to a list at the end.&lt;br&gt;
Although this will lead to a correct solution, this does lead to some unnecessary work being done. Duplicate results are still being calculated even if not included in the result.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nc"&gt;List&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;List&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Integer&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;combinationSum&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;target&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// will store the temporary result&lt;/span&gt;
    &lt;span class="nc"&gt;Set&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;List&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Integer&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;res&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;HashSet&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&amp;gt;();&lt;/span&gt; 

    &lt;span class="nc"&gt;List&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Integer&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;current&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;ArrayList&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&amp;gt;();&lt;/span&gt;
    &lt;span class="n"&gt;backtrack&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;target&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;current&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;res&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;

    &lt;span class="c1"&gt;// convert the set to a list&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;ArrayList&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&amp;gt;(&lt;/span&gt;&lt;span class="n"&gt;res&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Solution 2: Avoid Duplicate Moves
&lt;/h4&gt;

&lt;p&gt;A better way is to exclude duplicate moves while forming the next state.&lt;/p&gt;

&lt;p&gt;Suppose our current state is [1]&lt;br&gt;
When we loop through the remaining numbers, we can get the below possible states:&lt;br&gt;
[1, 2], [1, 2], [1, 3], [1, 4]&lt;/p&gt;

&lt;p&gt;To avoid the duplicate move, all we need to do is to check if a number is being processed twice.&lt;/p&gt;

&lt;p&gt;We will put the processed numbers in a Set and if a number is already in the set, we can skip it.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;backtrack&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;target&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;sum&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;index&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;List&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Integer&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;current&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;Set&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;List&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Integer&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;res&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// checking for solution - omitted&lt;/span&gt;

    &lt;span class="nc"&gt;Set&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Integer&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;set&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;HashSet&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&amp;gt;();&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;index&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;length&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="c1"&gt;// if the current number is already in the set, skip it&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;set&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;contains&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;]))&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;continue&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
        &lt;span class="o"&gt;}&lt;/span&gt;

        &lt;span class="c1"&gt;//processing - call and backtrack - omitted&lt;/span&gt;

        &lt;span class="c1"&gt;// add the current number to the set&lt;/span&gt;
        &lt;span class="n"&gt;set&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;add&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;]);&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We can further remove the set &lt;strong&gt;if the order of the result is not important&lt;/strong&gt;. &lt;/p&gt;

&lt;p&gt;The steps for this are:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Sort the initial array so that all duplicates come together.&lt;/li&gt;
&lt;li&gt;Instead of using a set, compare numbers to the previous number and skip if it's the same.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The below code can replace the Set logic once the array &lt;em&gt;nums&lt;/em&gt; is sorted.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;backtrack&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;target&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;sum&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;index&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;List&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Integer&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;current&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;List&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;List&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Integer&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;res&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// checking for solution - omitted&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;index&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;length&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="c1"&gt;// if the current number is already in the set, skip it&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;index&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;])&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;continue&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
        &lt;span class="o"&gt;}&lt;/span&gt;
        &lt;span class="c1"&gt;//processing - call and backtrack - omitted&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Variation 2: Numbers Can Be Used any Number of Times
&lt;/h3&gt;

&lt;p&gt;If we could use each number any number of times, the result changes.&lt;br&gt;
For e.g. For a target sum of 5, [1,2,3,4] will return [1,1,1,1,1], [1,1,1,2], [1,1,3], [1,2,2], [1,4], [2,3]&lt;/p&gt;

&lt;p&gt;This leads to a very small change in our recursive call.&lt;br&gt;
Since the same number can be used again in the solution, we will not increment the index while calling the recursive function.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;backtrack&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;target&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;sum&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;index&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;List&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Integer&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;current&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;List&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;List&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Integer&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;res&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// checking for solution - omitted&lt;/span&gt;

    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;index&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;length&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;current&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;add&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;]);&lt;/span&gt;

        &lt;span class="c1"&gt;// sum is updated but index is not incremented&lt;/span&gt;
        &lt;span class="n"&gt;backtrack&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;target&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;sum&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;],&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;current&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;res&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;

        &lt;span class="n"&gt;current&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;remove&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;current&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;size&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Problem 2: Permutations
&lt;/h2&gt;

&lt;p&gt;Let's solve the below problem:&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Given a collection of distinct integers, return all possible permutations of them.&lt;br&gt;
Input: [1, 2, 3]&lt;br&gt;
Output:&lt;br&gt;
[[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]]&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;This becomes simple once our intuition is clear. To form a permutation, there are 2 important principles:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Include all the elements&lt;/strong&gt; - This forms the stopping condition.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;No element can be included more than once&lt;/strong&gt;. This means that at any state, our possible moves include only the elements we have not included before.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;We will use a similar approach we used for the combination problem and will incorporate the above two points.&lt;/p&gt;
&lt;h3&gt;
  
  
  The Stopping Condition
&lt;/h3&gt;

&lt;p&gt;The stopping condition is that we have included all the elements.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;backtrack&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt;  &lt;span class="nc"&gt;Set&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Integer&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;current&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;List&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;List&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Integer&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;res&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;current&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;size&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;length&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;res&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;add&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;ArrayList&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&amp;gt;(&lt;/span&gt;&lt;span class="n"&gt;current&lt;/span&gt;&lt;span class="o"&gt;));&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="c1"&gt;// otherwise process&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  The Possible Moves
&lt;/h3&gt;

&lt;p&gt;The possible moves are the elements that we have not included in the current state before.&lt;/p&gt;

&lt;p&gt;We can break this into two steps:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Store the numbers that have been used in the current state. In simple words, maintain a Set for the current state.&lt;/li&gt;
&lt;li&gt;Check for all numbers after each move. We do not need to maintain the index variable as before since we want to check the possibility of all numbers after the current number.
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;backtrack&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt;  &lt;span class="nc"&gt;Set&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Integer&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;current&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;List&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;List&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Integer&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;res&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// stopping condition - omitted&lt;/span&gt;

    &lt;span class="c1"&gt;// starting from the first element always&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;length&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;

        &lt;span class="c1"&gt;// if the current number is already in the set, skip it&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;current&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;contains&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;]))&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;continue&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
        &lt;span class="o"&gt;}&lt;/span&gt;
        &lt;span class="n"&gt;current&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;add&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;]);&lt;/span&gt;
        &lt;span class="n"&gt;backtrack&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;current&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;res&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="n"&gt;current&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;remove&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;current&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;size&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;        
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Please note that the Set needs to be a LinkedHashSet. This is because we need to maintain the order of the elements.&lt;/strong&gt; If it's not, everything falls apart (At least in Java). &lt;strong&gt;If you do not have a Set alternative in your language that provides ordered iteration, simply use a list.&lt;/strong&gt; The contains operation does not make a difference to the order of time complexity (discussed later).&lt;/p&gt;

&lt;h3&gt;
  
  
  Variation: Numbers Are Not Distinct
&lt;/h3&gt;

&lt;p&gt;If some numbers are not distinct, the above solution cannot find possible permutations.&lt;/p&gt;

&lt;p&gt;This is because once a number has been used, its contains check will return true and we cannot use it again.&lt;/p&gt;

&lt;p&gt;The current set will never have the length of the original array, and we will be stuck in the backtrack function FOREVER.&lt;/p&gt;

&lt;h4&gt;
  
  
  Solution: Set of Indices
&lt;/h4&gt;

&lt;p&gt;The solution is simple. Instead of maintaining a set of values, we can maintain a set of indices. Indices are always unique.&lt;/p&gt;

&lt;p&gt;To maintain a set of indices that have been included in the current state, we can use a boolean array.&lt;/p&gt;

&lt;p&gt;The boolean array updates in the same way as the current set/list.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;backtrack&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt;  &lt;span class="nc"&gt;Set&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Integer&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;current&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;List&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;List&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Integer&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;res&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;boolean&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;used&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="c1"&gt;// stopping condition - omitted&lt;/span&gt;

        &lt;span class="k"&gt;for&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;length&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
            &lt;span class="c1"&gt;// if the current number is already in the set, skip it&lt;/span&gt;
            &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;used&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;])&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
                &lt;span class="k"&gt;continue&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
            &lt;span class="o"&gt;}&lt;/span&gt;
            &lt;span class="c1"&gt;// add to boolean array as well as current set&lt;/span&gt;
            &lt;span class="n"&gt;used&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
            &lt;span class="n"&gt;current&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;add&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;]);&lt;/span&gt;

            &lt;span class="n"&gt;backtrack&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;current&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;res&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;used&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;

            &lt;span class="c1"&gt;// remove from boolean array as well as current set&lt;/span&gt;
            &lt;span class="n"&gt;current&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;remove&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;current&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;size&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
            &lt;span class="n"&gt;used&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
        &lt;span class="o"&gt;}&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Time Complexity
&lt;/h2&gt;

&lt;p&gt;The theoretical Time complexity of each of these backtracking algorithms is O(N!) where N is the number of elements in the input array.&lt;/p&gt;

&lt;p&gt;This can be calculated by the following intuition:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;At every step, we have N choices to choose from.&lt;/li&gt;
&lt;li&gt;After making all choices, we have to make N-1 choices for each of them.&lt;/li&gt;
&lt;li&gt;And so on.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Number of steps = Nx(N-1)x(N-2)x...x1 = N!&lt;/p&gt;

&lt;p&gt;This does not mean all backtracking algorithms are O(N!). It all depends on the number of choices we have at each step.&lt;/p&gt;




&lt;p&gt;Thanks for reading. The problems and variations in this post cover the most popular variations of the backtracking technique.&lt;/p&gt;

&lt;p&gt;If you found this article helpful, please don't forget to share it with people in need - in whatever way possible.&lt;/p&gt;

&lt;p&gt;If you want to connect with me, you can find me on &lt;a href="https://twitter.com/abh1navv"&gt;Twitter&lt;/a&gt; or &lt;a href="https://www.linkedin.com/in/abh1navv"&gt;LinkedIn&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>programming</category>
      <category>beginners</category>
      <category>algorithms</category>
      <category>java</category>
    </item>
    <item>
      <title>10 SEO tips to increase the reach of your blogs.</title>
      <dc:creator>Abhinav Pandey</dc:creator>
      <pubDate>Tue, 05 Apr 2022 07:11:43 +0000</pubDate>
      <link>https://dev.to/abh1navv/10-seo-tips-to-increase-the-reach-of-your-blogs-54kk</link>
      <guid>https://dev.to/abh1navv/10-seo-tips-to-increase-the-reach-of-your-blogs-54kk</guid>
      <description>&lt;p&gt;Whether you're a blogger, startup, or a business, blogging is a long-term strategy to build a strong presence in front of humans as well as search engines.&lt;br&gt;
With these small, easy-to-implement SEO tips, you can increase your blog's reach and traffic.&lt;/p&gt;

&lt;h2&gt;
  
  
  1. Seed Keywords
&lt;/h2&gt;

&lt;p&gt;Ranking well for a large number of keywords is a difficult goal and may not be the best strategy.&lt;br&gt;
We should rather &lt;strong&gt;focus on a small set of keywords&lt;/strong&gt; that form a theme for your blogs. These words should be:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;relevant&lt;/strong&gt; to your niche or product,&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;frequently searched&lt;/strong&gt; for by your target audience,&lt;/li&gt;
&lt;li&gt;but has &lt;strong&gt;less competition in the market&lt;/strong&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;An example of this is:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Java is a vague keyword which is used by a lot of people. I will have a hard time beating other sites to rank for the best Java website.&lt;/li&gt;
&lt;li&gt;I can rather try with something more specific and smaller. E.g., Spring Cloud. If all my blogs are about Spring Cloud, my blog can build authority and rank for Spring Cloud.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  2. Long-tail keywords
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Another strategy is to use long-tail keywords - e.g. "implement service discovery in Java". These are &lt;strong&gt;something people will put in a search query&lt;/strong&gt; and at the same time are &lt;strong&gt;less competitive to rank for&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;Individually, long-tail keywords will not bring a lot of traffic but the traffic will add up when you start ranking for a lot of them.&lt;/li&gt;
&lt;li&gt;If blogs combine long-tail keywords with seed keywords, the effect multiplies. The next two tips talk more about this.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  3. URL and Title
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;The URL and Title of the page contain the keywords. &lt;/li&gt;
&lt;li&gt;A good strategy is to use the &lt;strong&gt;long-tail keywords in the URL&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;The &lt;strong&gt;title&lt;/strong&gt; can be similar and &lt;strong&gt;can additionally contain your seed keywords and/or your brand name&lt;/strong&gt;. &lt;/li&gt;
&lt;li&gt;Another thing to keep in mind is that the URL and Title should not be too long.&lt;/li&gt;
&lt;li&gt;On a search results page, only 60 characters of the title are shown and there is no reason why the title should be longer than that.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;E.g., &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;the URL can be &lt;a href="https://www.example.com/blog/implement-service-discovery-spring-cloud.html"&gt;https://www.example.com/blog/implement-service-discovery-spring-cloud.html&lt;/a&gt; &lt;/li&gt;
&lt;li&gt;the title can be &lt;strong&gt;Implement Service Discovery with Spring Cloud | Abhinav's Blog&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  4. H1 Heading
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;The heading is similar to the title. However, if your title is shorter on purpose, you can elaborate on it with the heading. &lt;/li&gt;
&lt;li&gt;Headings are more relevant to human readers and should always make sense. &lt;/li&gt;
&lt;li&gt;Another trick you can include is to use &lt;strong&gt;alternate versions of your long-tail keywords in the heading or secondary keywords&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;For E.g. if my article uses Netflix's Eureka, I could also include "Netflix Eureka" in the heading.&lt;/li&gt;
&lt;li&gt;So my heading becomes - &lt;strong&gt;How to Implement Service Discovery with Netflix Eureka and Spring Cloud&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; Some SEO tools also recommend that other heading levels should contain keywords. This is optional in my opinion and should only be done if it doesn't affect the readability of the sub-headings.&lt;/p&gt;

&lt;h2&gt;
  
  
  5. Meta Description
&lt;/h2&gt;

&lt;p&gt;A meta description is a short description of the page that is shown in search results. This needs to &lt;strong&gt;focus more on human readers than on search engines&lt;/strong&gt;.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;It will add very little to SEO but helps a lot with the click-through rate.&lt;/li&gt;
&lt;li&gt;The meta-description should be &lt;strong&gt;short, clear, and should fit in the space&lt;/strong&gt;. &lt;/li&gt;
&lt;li&gt;The number of characters in the meta description should be &lt;strong&gt;around 150 characters&lt;/strong&gt;. (This is not something you can control and may change in the future) &lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  6. Images, Videos, and Audio
&lt;/h2&gt;

&lt;p&gt;For search engines, images are a black box. &lt;br&gt;
However, as we know, now we can even search for images. So how does that work?&lt;/p&gt;

&lt;p&gt;Images are understood by search engines using the following ways: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The most basic connection they can make is between the image and &lt;strong&gt;the page it is on&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;Better information about the image is provided by the &lt;em&gt;alt&lt;/em&gt; text. But be careful with the alt text. Its purpose is to provide a correct description of the image for humans. It is not just an SEO strategy. Anyways, &lt;strong&gt;always include alt text in your images&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;The image URL&lt;/strong&gt; or &lt;em&gt;src&lt;/em&gt; attribute can also be used to provide information about the image. This is one of the most overlooked points when it comes to image SEO.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;The content surrounding the image&lt;/strong&gt; like a leading or trailing text can also be used to provide information about the image.&lt;/li&gt;
&lt;li&gt;An advanced strategy is to use an &lt;strong&gt;enhanced markup&lt;/strong&gt; for images. Refer to &lt;a href="https://schema.org/"&gt;Schema.org&lt;/a&gt; for more information.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  7. Page speed
&lt;/h2&gt;

&lt;p&gt;There are a few things technical things to take care of that support a better page ranking. Some of these will not be completely in your hands if you write on someone else's website but they are good to know.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Slow pages are not ranked high&lt;/strong&gt; by search engines and are not liked by users.&lt;/li&gt;
&lt;li&gt;Another important factor is your blog being &lt;strong&gt;mobile-friendly&lt;/strong&gt;. Page rankings are maintained separately for mobile and desktop so that the user can get the best experience. This means that if your website is not mobile-friendly, there is a chance your blog will be ranked lower on mobile than on desktop.&lt;/li&gt;
&lt;li&gt;Do not use images hosted on external websites. &lt;strong&gt;Dependency on external images is not good&lt;/strong&gt; because they could be deleted at any time or may load slower if not CDN-cached by the owner.&lt;/li&gt;
&lt;li&gt;It's best to download the images and upload them again on your blog. Needless to say, &lt;strong&gt;the images should not be protected by copyright&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;Use &lt;strong&gt;optimized images&lt;/strong&gt;. Do not use high-resolution images which will take a lot of time to load. &lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  8. Cross-posting
&lt;/h2&gt;

&lt;p&gt;Cross-posting or not cross-posting is a separate discussion. But if you do cross-post, keep a few things in mind.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The original post should appear on your personal/company blog.&lt;/li&gt;
&lt;li&gt;All other posts should have &lt;strong&gt;canonical links&lt;/strong&gt; to the original post.&lt;/li&gt;
&lt;li&gt;Do not bother posting on other websites which do not allow canonical links.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  9. Internal Linking
&lt;/h2&gt;

&lt;p&gt;Link building is an important part of SEO. &lt;/p&gt;

&lt;p&gt;Internal links are links to pages on your website. They are important to tell search engines how pages on your website are related to each other. &lt;br&gt;
This means a new blog can be used to bring traffic to older blogs. However, this only works if the users also find this linking useful. &lt;/p&gt;

&lt;p&gt;Let's look at a few small ways to improve internal linking.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Links to an &lt;strong&gt;old blog should be relevant to your current blog&lt;/strong&gt;. A user should find it useful to click the link.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Anchor text&lt;/strong&gt; describes the link for the user as well as the search engine. Link texts like "Read More" or "Continue Reading" are not good. Rather, use a version of the page title. This is true for any kind of link on a blog.&lt;/li&gt;
&lt;li&gt;The &lt;strong&gt;linked page should be of good quality&lt;/strong&gt; too. If the user closes the page right after clicking the link, search engines will not consider the linking relevant.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  10. Inbound Linking
&lt;/h2&gt;

&lt;p&gt;Inbound links are links from other websites to your blogs. They are important to tell search engines how pages on your website are related to other websites. They act as a way to review the quality of your website. &lt;/p&gt;

&lt;p&gt;This is not an easy part but there are some important things to understand:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Inbound links from only the relevant sites count&lt;/strong&gt;. So there is no need to go to the whole internet and try to place a link to your blog on every social media page.&lt;/li&gt;
&lt;li&gt;Find communities that are relevant to your blog and place links to your blog on them. For e.g., &lt;strong&gt;Hacker News, Reddit&lt;/strong&gt; (relevant subreddits), etc.&lt;/li&gt;
&lt;li&gt;Encourage people to share your blog on social media. &lt;strong&gt;Social share links&lt;/strong&gt; will be helpful to people who are interested in your blog. It brings traffic and is likely to reach a relevant audience. A sentence asking people to share your blog on social media goes a long way.&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;Thank you for reading. Hopefully, these tips can form a checklist for us when publishing blogs. If you liked the article, please do not forget to comment, react and/or share the blog with others. If you want to connect with me, you can find me on &lt;a href="https://twitter.com/abh1navv"&gt;Twitter&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>seo</category>
      <category>writing</category>
    </item>
    <item>
      <title>12 Common uses of Java Streams</title>
      <dc:creator>Abhinav Pandey</dc:creator>
      <pubDate>Wed, 09 Mar 2022 07:29:38 +0000</pubDate>
      <link>https://dev.to/abh1navv/12-common-uses-of-java-streams-1pgk</link>
      <guid>https://dev.to/abh1navv/12-common-uses-of-java-streams-1pgk</guid>
      <description>&lt;p&gt;Java Streams API was introduced in Java 8. Its aim is to provide a less verbose and concise way to carry out common operations on collections of objects.&lt;/p&gt;

&lt;p&gt;Although it can be hard to get used to, the Java Streams API is very powerful and can be used to implement complex algorithms.&lt;/p&gt;

&lt;p&gt;In this article, we will talk about some common use cases of the Java Streams API. &lt;/p&gt;

&lt;p&gt;For those of us not proficient with the Streams API, the goal should be to keep these use cases in mind and whenever we come across one of them, we should try to implement them using Streams rather than the traditional way.&lt;/p&gt;

&lt;p&gt;Let's establish some basics first.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;stream()&lt;/code&gt; - creates a stream from a collection&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;collect()&lt;/code&gt; - collects the stream into an object. The object can be a collection, primitive, or a custom class.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Collectors&lt;/code&gt; - a class which provides (a lot of) static methods for collecting streams. We will use some of these below. Refer to the documentation for full reference.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;These are the most popular stream operations and will be used in the rest of this tutorial.&lt;/p&gt;

&lt;h2&gt;
  
  
  Use cases
&lt;/h2&gt;

&lt;p&gt;Let's look at some use cases of streams:&lt;/p&gt;

&lt;h3&gt;
  
  
  1. Filtering
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Used for removing values from a Collection based on a condition.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;filter()&lt;/code&gt; method is used to filter the elements of a Collection based on a condition. Only matching elements are retained.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;E.g. - Remove all odd numbers from a list.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nc"&gt;List&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Integer&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;evenNumbers&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;originalList&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;stream&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt;
        &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;filter&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;n&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
        &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;collect&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Collectors&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;toList&lt;/span&gt;&lt;span class="o"&gt;());&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  2. Preprocessing
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Useful when each value in the collection needs to be changed in place.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;map()&lt;/code&gt; method is used to apply a function to each element of a Collection and return a new Collection of the computed values.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;E.g. Convert each value to its square.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nc"&gt;List&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Integer&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;squares&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;originalList&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;stream&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt;
        &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;map&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;n&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
        &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;collect&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Collectors&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;toList&lt;/span&gt;&lt;span class="o"&gt;());&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  3. Conversion
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Useful when we want to convert a Collection to another Collection.&lt;/li&gt;
&lt;li&gt;There are multiple ways to achieve this and let's see them.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In general, as mentioned above, we can use map() and collect() methods to convert a Collection to another Collection.&lt;/p&gt;

&lt;p&gt;Example 1. Create a Map from a List.&lt;/p&gt;

&lt;p&gt;Convert a list of strings to a map of string and length.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nc"&gt;Map&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;Integer&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;wordLengths&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;words&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;stream&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt;
        &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;collect&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Collectors&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;toMap&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;
                &lt;span class="n"&gt;word&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;word&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt;
                &lt;span class="n"&gt;word&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;word&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;length&lt;/span&gt;&lt;span class="o"&gt;()));&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Example 2. Convert list to sets. &lt;/p&gt;

&lt;p&gt;This is a common use case for removing duplicates. &lt;br&gt;
Further, if we want to put the elements back to a list, we can use the stream() and collect() methods twice.&lt;/p&gt;

&lt;p&gt;Convert a list of strings to a list of unique strings.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="c1"&gt;// if we want to collect to a set&lt;/span&gt;
&lt;span class="nc"&gt;Set&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;uniqueWords&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;words&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;stream&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt;
        &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;collect&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Collectors&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;toSet&lt;/span&gt;&lt;span class="o"&gt;());&lt;/span&gt;

&lt;span class="c1"&gt;// OR&lt;/span&gt;

&lt;span class="c1"&gt;// if we want to start and end as a list&lt;/span&gt;
&lt;span class="nc"&gt;List&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;uniqueWords&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;words&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;stream&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt;
        &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;collect&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Collectors&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;toSet&lt;/span&gt;&lt;span class="o"&gt;()).&lt;/span&gt;&lt;span class="na"&gt;stream&lt;/span&gt;&lt;span class="o"&gt;().&lt;/span&gt;&lt;span class="na"&gt;collect&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Collectors&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;toList&lt;/span&gt;&lt;span class="o"&gt;());&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Example 3. Convert a list of Products to a list of their names. (Flattening)&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nc"&gt;List&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;productNames&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;products&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;stream&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt;
        &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;map&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;product&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;product&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getName&lt;/span&gt;&lt;span class="o"&gt;())&lt;/span&gt;
        &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;collect&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Collectors&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;toList&lt;/span&gt;&lt;span class="o"&gt;());&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  4. Reduction
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Reduce a Collection to a single value.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;reduce()&lt;/code&gt; method is used to apply a function to each element of a Collection and return a single value.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Note that since the &lt;code&gt;reduce()&lt;/code&gt; method returns a single value, it is not possible to use it to return a Collection.&lt;/p&gt;

&lt;p&gt;E.g. Sum all the values in a list.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;sum&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;numbers&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;stream&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt;
        &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;reduce&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  5. Grouping
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Group elements of a Collection based on a condition.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Collectors.groupingBy()&lt;/code&gt; method is used to group elements of a Collection based on a condition.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;E.g. Group all products into lists of Products by their category.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nc"&gt;Map&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;List&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Product&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;productsByCategory&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;products&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;stream&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt;
        &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;collect&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Collectors&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;groupingBy&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;product&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;product&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getCategory&lt;/span&gt;&lt;span class="o"&gt;()));&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  6. Finding
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Finding the first or any element of a Collection which matches a condition.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;findFirst()&lt;/code&gt; and &lt;code&gt;findAny()&lt;/code&gt; methods are used to find the first or any element of a Collection which matches a condition.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This is typically similar to linear search.&lt;/p&gt;

&lt;p&gt;E.g. Find the first word in a list which is longer than 5 characters.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nc"&gt;Optional&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;firstLongWord&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;words&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;stream&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt;
        &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;filter&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;word&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;word&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;length&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
        &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;findFirst&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
&lt;span class="c1"&gt;// Note that findFirst() and findAny() methods return Optional&amp;lt;T&amp;gt; objects.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  7. Sorting
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Sort elements of a Collection.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;sorted()&lt;/code&gt; method is used to sort elements of a Collection.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In general, Collections.sort() is enough to sort a Collection. We can use sorted() specially if we want to follow it with another operation.&lt;/p&gt;

&lt;p&gt;E.g. Sort a list of numbers in ascending order and then return the first k elements.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nc"&gt;List&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Integer&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;topK&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;numbers&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;stream&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt;
        &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;sorted&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt;
        &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;limit&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;k&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
        &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;collect&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Collectors&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;toList&lt;/span&gt;&lt;span class="o"&gt;());&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  8. Partitioning
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Partition elements of a Collection based on a condition.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Collectors.partitioningBy()&lt;/code&gt; method is used to partition elements of a Collection based on a condition.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Partitioning is similar to grouping except it returns two Collections - one for elements matching the condition and one for elements not matching the condition.&lt;/p&gt;

&lt;p&gt;E.g. Partition students into passing and failing.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt; &lt;span class="nc"&gt;Map&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Boolean&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;List&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Student&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;passingFailing&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;students&lt;/span&gt;
        &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;stream&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt;
        &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;collect&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Collectors&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;partitioningBy&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;s&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getGrade&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="no"&gt;PASS_THRESHOLD&lt;/span&gt;&lt;span class="o"&gt;));&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  9. Counting
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Count the number of elements matching a condition.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;count()&lt;/code&gt; method is used to count the number of elements matching a condition.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;E.g. Count the number of words in a list which are longer than 5 characters.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kt"&gt;long&lt;/span&gt; &lt;span class="n"&gt;count&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;words&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;stream&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt;
        &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;filter&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;word&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;word&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;length&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
        &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;count&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  10. Range
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Create a range of values.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;range()&lt;/code&gt; method is used to create a range of values.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;There are special classes for creating streams of particular types - &lt;code&gt;IntStream, LongStream, DoubleStream, and Stream&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;These classes are useful when dealing with primitive numeric types. Internally, they use &lt;code&gt;Arrays.stream()&lt;/code&gt; to create the stream.&lt;/p&gt;

&lt;p&gt;E.g. Create an array of numbers from 0 to 10.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;numbers&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;IntStream&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;range&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="o"&gt;).&lt;/span&gt;&lt;span class="na"&gt;toArray&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  11. Matching
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Match elements of a Collection against a predicate(condition).&lt;/li&gt;
&lt;li&gt;Methods such as &lt;code&gt;anyMatch()&lt;/code&gt;, &lt;code&gt;allMatch()&lt;/code&gt;, and &lt;code&gt;noneMatch()&lt;/code&gt; are used to match elements of a Collection against a predicate and return a boolean value.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;E.g. Check for products with a price greater than 10.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="c1"&gt;// true when all elements match the predicate&lt;/span&gt;
&lt;span class="kt"&gt;boolean&lt;/span&gt; &lt;span class="n"&gt;allMatch&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;products&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;stream&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt;
        &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;allMatch&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;product&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;product&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getPrice&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;// true when any element matches the predicate&lt;/span&gt;
&lt;span class="kt"&gt;boolean&lt;/span&gt; &lt;span class="n"&gt;anyMatch&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;products&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;stream&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt;
        &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;anyMatch&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;product&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;product&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getPrice&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;// true when no elements match the predicate&lt;/span&gt;
&lt;span class="kt"&gt;boolean&lt;/span&gt; &lt;span class="n"&gt;noneMatch&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;products&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;stream&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt;
        &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;noneMatch&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;product&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;product&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getPrice&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  12. Joining
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Join elements of a Collection into a String.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Collectors.joining()&lt;/code&gt; method is used to join elements of a Collection into a String.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;E.g. Join all the words in a list into a single string.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;joinedWords&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;words&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;stream&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt;
        &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;collect&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Collectors&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;joining&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;" "&lt;/span&gt;&lt;span class="o"&gt;));&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Thats it for the common scenarios. There are other less common scenarios you can explore on your own:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Parallel Streams&lt;/li&gt;
&lt;li&gt;Statistics &lt;/li&gt;
&lt;li&gt;Custom Collectors.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Advantages of Streams
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Less Verbose Code&lt;/strong&gt; - Reduces the amount of code needed to process a Collection.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Lesser intermediate variables&lt;/strong&gt; - Intermediate variables can be an opportunity to make mistakes. Having fewer of them can help us avoid unexpected bugs.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Intuitive code&lt;/strong&gt; - Some developers will disagree that streams are more intuitive than other methods. However, they are much more intuitive than other methods once we get used to them. Reading 5 lines to understand that we are filtering and removing products from a list isn't intuitive compared to reading just a &lt;code&gt;filter()&lt;/code&gt; method.&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;Thanks for reading. I hope you enjoyed this article. &lt;br&gt;
There are many more cases where streams can be used which are not covered in this thread. Feel free to add any common scenario I missed.&lt;/p&gt;

&lt;p&gt;If you want to connect with me, you can find me on &lt;a href="https://twitter.com/abh1navv"&gt;Twitter&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>java</category>
      <category>programming</category>
      <category>codenewbie</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Retry Pattern in Microservices</title>
      <dc:creator>Abhinav Pandey</dc:creator>
      <pubDate>Fri, 25 Feb 2022 07:16:30 +0000</pubDate>
      <link>https://dev.to/abh1navv/retry-pattern-in-microservices-4m39</link>
      <guid>https://dev.to/abh1navv/retry-pattern-in-microservices-4m39</guid>
      <description>&lt;p&gt;In a microservices architecture, the retry pattern is a common pattern for recovering from transient errors.&lt;/p&gt;

&lt;p&gt;In a distributed system, some examples of these errors are:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Network failure&lt;/strong&gt; &lt;/p&gt;

&lt;p&gt;An application lost connectivity for a short period of time.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Component failure&lt;/strong&gt; &lt;/p&gt;

&lt;p&gt;A component is unavailable for a short period of time. This usually happens during maintenance or automatic recovery from a crash.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Component overload&lt;/strong&gt; &lt;/p&gt;

&lt;p&gt;A component is overloaded and cannot accept new requests for a short period of time. This could also be due to a throttling or rate-limiting implementation.&lt;/p&gt;

&lt;p&gt;As you can see, &lt;strong&gt;the above errors are self-healing&lt;/strong&gt;. In this case, it makes sense for the client to retry the request (immediately or after a delay) rather than logging the error and aborting the request.&lt;/p&gt;

&lt;h2&gt;
  
  
  Retry Pattern
&lt;/h2&gt;

&lt;p&gt;Retry patterns can be implemented in a number of ways. However, each implementation needs to take care of the below considerations:&lt;/p&gt;

&lt;h3&gt;
  
  
  Identifying Transient Errors
&lt;/h3&gt;

&lt;p&gt;The first important step is to identify if the error is transient or not. This will let us decide if the request needs to be repeated or the error should be logged.&lt;/p&gt;

&lt;p&gt;For example, a connection timeout indicates a network error and should be retried. &lt;br&gt;
On the other hand, an authentication failure isn't going to go away after a few seconds and should not be retried.&lt;/p&gt;

&lt;h3&gt;
  
  
  Retry Delay
&lt;/h3&gt;

&lt;p&gt;The second important step is to decide how long to wait before retrying the request.&lt;/p&gt;

&lt;p&gt;For real-time tasks like web page request, the delay should be as short as possible, retries should be less, and  errors may provide better experience than delayed retries. However, for long-running or background tasks like sending an email notification, the delay should be long enough to allow the system to recover.&lt;/p&gt;

&lt;p&gt;Let's discuss more about delay timings.&lt;/p&gt;

&lt;h3&gt;
  
  
  Backoff Time
&lt;/h3&gt;

&lt;p&gt;Backoff time is the time to wait before retrying the request. The backoff time is calculated based on the number of retries and the delay between retries.&lt;br&gt;
This can be used to avoid flooding the system with retries. &lt;/p&gt;

&lt;p&gt;It can be broadly classified into these categories:&lt;/p&gt;

&lt;h4&gt;
  
  
  Constant/0 backoff time
&lt;/h4&gt;

&lt;p&gt;The backoff time is the same for all retries. This is &lt;strong&gt;good for rare network failures or low load services&lt;/strong&gt;. &lt;/p&gt;

&lt;h4&gt;
  
  
  Incremental backoff time
&lt;/h4&gt;

&lt;p&gt;The backoff time is &lt;strong&gt;calculated based on the number of retries&lt;/strong&gt;. Some examples of incremental backoff time are linear, exponential, and fibonacci. Exponential is the most common where the time doubles every time the retried request fails.&lt;/p&gt;

&lt;h4&gt;
  
  
  Random backoff time
&lt;/h4&gt;

&lt;p&gt;The backoff time is calculated randomly between the minimum and maximum backoff time or through a more complex function which is guaranteed to produce random delays. This is &lt;strong&gt;useful for avoiding thundering herd problems&lt;/strong&gt; - in short, if a large number of requests hit at the same time and this is the reason for the failure, it won't help if their retries also hit at the same time. Randomness in retry delays will avoid this.&lt;/p&gt;

&lt;h2&gt;
  
  
  Implementing a Retry Pattern
&lt;/h2&gt;

&lt;p&gt;A simple retry implementation will consist of the below steps:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Identify transient conditions - E.g. which response codes, errors or network exceptions indicate a transient error.&lt;/li&gt;
&lt;li&gt;Decide backoff time and algorithm - as described above.&lt;/li&gt;
&lt;li&gt;Decide maximum number of retries.&lt;/li&gt;
&lt;li&gt;While retry count is less than the maximum count, retry the request until the request succeeds.&lt;/li&gt;
&lt;li&gt;If retries are exhausted, log the error and abort the request.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Existing implementations
&lt;/h3&gt;

&lt;p&gt;Before implementing our own retry pattern, we can leverage existing implementations. Let's see where we can look for existing implementations:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Clients and SDKs&lt;/strong&gt;. There are many existing solutions for retrying requests. Depending on the service you are calling, the retry functionality may already be implemented. For example, the Apache Kafka client provides retry functionality using the &lt;code&gt;retry&lt;/code&gt; configuration option. Similarly, a large number of popular cloud SDKs already provide retry functionality.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Language and tool support&lt;/strong&gt;. The retry pattern is a common pattern in many languages and tools. For example, the Python &lt;code&gt;requests&lt;/code&gt; library provides retry functionality.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Framework-specific libraries&lt;/strong&gt;. The retry pattern is a common pattern in many libraries. For example, in Spring applications, Spring Retry and Resilience4J are popular retry libraries.&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;Thanks for reading! This should give you an idea of Retry Patterns. If you want to connect with me, you can find me on &lt;a href="https://twitter.com/abh1navv"&gt;Twitter&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>microservices</category>
      <category>architecture</category>
      <category>programming</category>
      <category>cloud</category>
    </item>
    <item>
      <title>Template Method Design Pattern</title>
      <dc:creator>Abhinav Pandey</dc:creator>
      <pubDate>Tue, 22 Feb 2022 05:40:08 +0000</pubDate>
      <link>https://dev.to/abh1navv/template-method-design-pattern-4hp8</link>
      <guid>https://dev.to/abh1navv/template-method-design-pattern-4hp8</guid>
      <description>&lt;p&gt;Template method pattern is a design pattern that allows you to define a skeleton of an algorithm in an operation, deferring some steps to subclasses.&lt;/p&gt;

&lt;p&gt;In simpler terms, this is like making a to-do list which multiple implementations follow in their own way.&lt;/p&gt;

&lt;p&gt;Let's start with some terminology:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Template Method&lt;/strong&gt; - A template method is a method that defines the skeleton of an algorithm in an operation.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Concrete Template&lt;/strong&gt; - A concrete template is a class that implements the template method.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Client&lt;/strong&gt; - A client is a class that tells the template method which concrete template to use.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Abstract Steps&lt;/strong&gt; - An abstract step is a method that is defined in the template method but is left to be implemented by the subclasses.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  A real world example
&lt;/h2&gt;

&lt;p&gt;Let's think about an example of this in the real world. We will compare this to a digital payment system.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The payment system has a payment gateway which is a template method.&lt;/li&gt;
&lt;li&gt;The customer can use the payment gateway to make a payment.&lt;/li&gt;
&lt;li&gt;The payment gateway has a few steps that are defined in the template method - validating the payment, transferring the money and updating the database.&lt;/li&gt;
&lt;li&gt;However, the implementation of these steps is decided by the exact payment option that the customer chooses - Credit Card, Net Banking, PayPal, etc. &lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Let's code this up
&lt;/h2&gt;

&lt;h3&gt;
  
  
  The Template Method
&lt;/h3&gt;

&lt;p&gt;Let's create an abstract class called PaymentGateway:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;abstract&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;PaymentGateway&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;makePayment&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;validatePayment&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
        &lt;span class="n"&gt;transferMoney&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
        &lt;span class="n"&gt;updateDatabase&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
    &lt;span class="kd"&gt;protected&lt;/span&gt; &lt;span class="kd"&gt;abstract&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;validatePayment&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
   &lt;span class="kd"&gt;protected&lt;/span&gt; &lt;span class="kd"&gt;abstract&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;transferMoney&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
   &lt;span class="kd"&gt;protected&lt;/span&gt; &lt;span class="kd"&gt;abstract&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;updateDatabase&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here we have a template method &lt;code&gt;makePayment()&lt;/code&gt; which calls the three steps of the payment process. None of the steps are implemented in the abstract class.&lt;br&gt;
This method defines a template that its subclasses must follow to make a payment.&lt;/p&gt;

&lt;p&gt;We could also have provided a default implementation of the steps in the abstract class for reusability but this is an implementation decision.&lt;/p&gt;

&lt;p&gt;To make it even more strict, we have made the template method &lt;code&gt;final&lt;/code&gt; so that it cannot be overridden. It is a good practice to make the template method final, but it is not a requirement.&lt;/p&gt;
&lt;h3&gt;
  
  
  The Concrete Templates
&lt;/h3&gt;

&lt;p&gt;Let's create concrete templates for the payment gateway. We will create two concrete templates:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;CreditCardPaymentGateway - This template will implement the steps of the payment process for credit card payments.&lt;/li&gt;
&lt;li&gt;PayPalPaymentGateway - This template will implement the steps of the payment process for PayPal payments.
&lt;/li&gt;
&lt;/ol&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;CreditCardPaymentGateway&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;PaymentGateway&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;validatePayment&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="c1"&gt;// check if the credit card is valid&lt;/span&gt;
        &lt;span class="c1"&gt;// check if the credit card has sufficient balance&lt;/span&gt;
        &lt;span class="c1"&gt;// check if the credit card is expired&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;transferMoney&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="c1"&gt;// transfer the money from the credit card to the bank account&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;updateDatabase&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="c1"&gt;// update the database with the transaction details&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;PayPalPaymentGateway&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;PaymentGateway&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;validatePayment&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="c1"&gt;// check if the PayPal account is valid&lt;/span&gt;
        &lt;span class="c1"&gt;// check if the PayPal account has sufficient balance&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;transferMoney&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="c1"&gt;// transfer the money from the PayPal account to the bank account&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;updateDatabase&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="c1"&gt;// update the database with the transaction details&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;h3&gt;
  
  
  The Client
&lt;/h3&gt;

&lt;p&gt;To choose the payment gateway, we could use a factory. In our case, the factory will return the appropriate payment gateway based on the customer's payment option.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;PaymentGatewayFactory&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nc"&gt;PaymentGateway&lt;/span&gt; &lt;span class="nf"&gt;getPaymentGateway&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;paymentOption&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;paymentOption&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;equals&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"credit card"&lt;/span&gt;&lt;span class="o"&gt;))&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;CreditCardPaymentGateway&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
        &lt;span class="o"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;paymentOption&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;equals&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"paypal"&lt;/span&gt;&lt;span class="o"&gt;))&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;PayPalPaymentGateway&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
        &lt;span class="o"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;IllegalArgumentException&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Invalid payment option"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="o"&gt;}&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The client will use the factory to get the appropriate payment gateway and make the payment.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;PaymentGatewayClient&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="nc"&gt;PaymentGatewayFactory&lt;/span&gt; &lt;span class="n"&gt;factory&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;PaymentGatewayFactory&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
        &lt;span class="nc"&gt;PaymentGateway&lt;/span&gt; &lt;span class="n"&gt;gateway&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;factory&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getPaymentGateway&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"credit card"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="n"&gt;gateway&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;makePayment&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When the &lt;code&gt;makePayment&lt;/code&gt; method is called on the payment gateway, the template method defined in the abstract class is called. &lt;br&gt;
However, the concrete methods of CreditCardPaymentGateway are called to execute the internal steps.&lt;/p&gt;

&lt;h2&gt;
  
  
  Advantages of the Template Method
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;It provides a flexible way to change the implementation of internal steps without missing any of the steps.&lt;/li&gt;
&lt;li&gt;It provides a flexible way to add more implementations in the future.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Template method pattern is used in Java for many out-of-the-box implementations. For example, InputStreamReader is a template method pattern implementation.&lt;/p&gt;




&lt;p&gt;Thanks for reading! This should help you understand the concept of the template method pattern. Stay tuned for more design patterns.&lt;br&gt;
If you want to connect with me, you can find me on Twitter &lt;a href="https://twitter.com/abh1navv"&gt;@abh1navv&lt;/a&gt;&lt;/p&gt;

</description>
      <category>java</category>
      <category>programming</category>
      <category>codequality</category>
      <category>architecture</category>
    </item>
  </channel>
</rss>
