Posts

Showing posts with the label Core Java

Observability Done Right: Best Practices and Anti-Patterns for Effective System Monitoring

Image
  WHAT Observability is a concept that refers to the ability to gain insights into the behavior and performance of complex systems. In the context of software engineering, observability involves the collection, analysis, and visualization of data from software applications, infrastructure, and other components of a system. In the animal kingdom, observability plays a critical role in survival, allowing animals to monitor their surroundings, detect threats, and find food. Dolphins use echolocation to observe their surroundings. They emit high-frequency sounds that bounce off objects, allowing them to create a 3D map of their environment. Thanks for reading Knowledge Cafe! Subscribe for free to receive new posts and support my work. Subscribed WHY In today's era, architectures are becoming increasingly large, complex, and fast-paced due to the faster development and deployment of software by distributed teams with the help of DevOps, continuous delivery, and agile development methodo...

Is Java Dynamically Typed Language?

Image
  Lets understand difference between Dynamically Typed and Statically Typed Language. Dynamically Typed Language Statically Typed Language Perform variable type checking at runtime perform variable type checking at compile time Not Mandatory Need to declare the data types of your variables before you use them // Java example int roleNumber;  roleNumber = 5; // Groovy example roleNumber = 5 Dynamically typed language if not used properly it may lead to run to errors or exception due to type mismatch or typo.  Java 7 has introduced diamond operator and Lambda (Java 8) expression can infer variable types, see below example.   //Diamond Operator List<String> nameList = new ArrayList<>(); //Lambda Expr Function<String, String> convertLower= (s) -> s. toLowerCase (); Prior to Java 10, we used to declare variable like below String ...

Semaphores - Java Concurrency

Image
Semaphores are an often misunderstood and under used tool for restricting access to resources. They are ignored for other ways of controlling access to resources. But semaphores give us a tool set that goes beyond what normal synchronization and other tools can give us. The simplest way to describe a semaphore is a mechanism to allows n units to be acquired to access particular resource. A semaphore is a synchronization object that controls access by multiple processes to a common resource in a parallel programming environment. Semaphores are widely used to control access to files and shared memory. Semaphore is a technique used to control access to common resource (like database connection,  file operation,  ports etc) for competing multiple processes. Semaphore maintains a counter which keeps track of the number of resources available. When a process requests access to resource, semaphore checks the variable count and if it is less than total count then grants access an...

How to check if String Starts with one of the several prefixes

Image
In my current project I had requirement to find if given string starts with any of provided prefixes or not. If yes then do special handling otherwise normal processing.  There are few ways to achieve this, mentioned below. Please comment if you know any better way or problem in below approaches.   (1) Iteration and use startsWith() method This has limitation if search string varies over period of time, we might need to recompile code. This will take longer if search string list is too large static void javaStdWay (String testString, List<String> searchStringList) { boolean flag = false ; for (String searchSt : searchStringList) { if (testString. toUpperCase (). startsWith (searchSt. toUpperCase ())) { System. out . println ( "TestString {" + testString + "} starts with " + searchSt); flag = true ; } } if (!flag) { ...

Printing Object Properties - RecursiveToStringStyle

Image
It would be sometime challenging to print/log object properties when toString method is not overriden in  class. We can use reflection to retrieve and print values but it would become tricky when object has other object refrences and so on. Apache provides very good implementation (http://commons.apache.org/proper/commons-lang/download_lang.cgi) to solve the problem. I will try to explain this with simple example. I have Employee Object having two properties employeeId(Numer) and department (Type Department), While Department object has two properties departmentName (String) and address (Type Address), while Address has three properties houseNumber, StreetName, and Pincode. public class Employee { private Number employeeId; private Department department; public Department getDepartment () { return department; } public void setDepartment (Department department) { this . department = department; } public Number getEmployId () {...

CaseInsensitiveMap

Image
How to ignore case sensitivity when searching a key in the java.util.Map? There are multiple ways to achieve this (1) We can  use a  TreeMap  instead of  HashMap , then specify a Comparator with a case insensitive order ( String.CASE_INSENSITIVE_ORDER ) package in. softcaretech ; import java.util.Map; import java.util.TreeMap; public class TreeMapDemo { public static void main (String args[]){ Map<String, String> studentMap = new TreeMap<String, String>(String. CASE_INSENSITIVE_ORDER ); studentMap. put ( "Amit" , "Audi" ); studentMap. put ( "Nilesh" , "BMW" ); System. out . println ( "Searchnig AMIT Key from map:" + studentMap. get ( "AMIT" )); System. out . println ( "Searchnig amit Key from map:" + studentMap. get ( "amit" )); } } (2)  You can write a wrapper map class with custom put and get methods (converting keys to lower case or...