Posts

Showing posts with the label HashMap

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...

Facts about HashMap in Java 6

Image
  Few points regarding HashMap DS in Java 6 as per the source code. HashMap extends AbstractMap implements Map Backed by array of Map.Entry where each element points to a list of Map.Entry  Default Initial Capacity – 16 i.e. Entry[16] Default load factor  - 0.75 i.e. when size reaches 75% of capacity – resizing of internal data structure is triggered. Capacity is always in power of 2 - if 5 is passed as initialCapacity in constructor, capacity will be set to 8 – it just does bitwise shift of 1 until it gets a number just greater than the passed capacity value. If 8 is passed – actual capacity will be 16 . (for detail check source code) provides a hook method init() which subclasses can use for addition functionality- it is invoked from constructor. modCount member variable to track the modifications in hashmap. same is used for Fail-fast detection modifications means adding new Entry and removing Entry  from hashmap provide supplemen...

Iterate Through a HashMap

The  HashMap  is one of the most useful data structures in the Java programming language. Once you have a HashMap filled with data, you may want to iterate through its keys and values. Below are three different ways to iterate through a Java HashMap. Sample HashMap Here is our sample HashMap. The key is an Integer and the value is a String: HashMap <Integer, String> hm = new HashMap<Integer, String>(); hm.put(0, "zero"); hm.put(1, "one"); hm.put(2, "two"); In the above Java code, we first declare the HashMap. Then we add the values "zero", "one" and "two" with the keys 0, 1 and 2 respectively. Now that the HashMap has data, we can try to iterate over the keys and values. Iteration Example 1 for (int i=0; i < hm.size(); i++) { Integer key = hm.keySet().toArray()[i]; String val = hm.values().toArray()[i]; System.out.println("key,val: " + key + "," + val); } In this example, we use a  for  loop ...

hashcode and equals methods

Equals and hashcode methods are two primary but yet one of most important methods for java developers to be aware of. Java intends to provide equals and hashcode for every class to test the equality and to provide a hash or digest based on content of class. Importance of hashcode increases when we use the object in different collection classes which works on hashing principle e.g. hashtable and hashmap. A well written hashcode method can improve performance drastically by distributing objects uniformly and avoiding collision. General Contracts for hashCode() in Java  1) If two objects are  equal  by equals() method then there  hashcode  returned by hashCode() method must be same. 2) Whenever  hashCode() mehtod  is invoked on the same object more than once within single execution of application, hashCode() must return same integer provided no information or fields used in equals and hashcode is modified. This integer is not required to be same during multiple execution of application th...

How HashMap works in Java

How HashMap works in Java or sometime how get method work in HashMap is common questions on Java interviews now days. Almost everybody who worked in Java knows about HashMap, where to use HashMap or difference between Hashtable and HashMap then why this interview question becomes so special? Because of the depth it offers. It has become very popular java interview question in almost any senior or mid-senior level Java interviews. Investment banks mostly prefer to ask this question and some time even ask to implement your own HashMap based upon your coding aptitude. Introduction of ConcurrentHashMap and other concurrent collections has also made this questions as starting point to delve into more advanced feature. let's start the journey. Questions start with simple statement "Have you used HashMap before" or "What is HashMap? Why do we use it “ Almost everybody answers this with yes and then interviewee keep talking about common facts about HashMap like HashMap accep...