Introduction: In a well-designed Java application, the classes should be as independent as possible. Such a design promotes reusability of components. It also makes it easier to unit test the various components. The concept of dependency injection promotes loose coupling among Java objects. In this tutorial, we’ll talk about the dependency injection in Spring framework. […]
Introduction: ArrayDeque in Java is a class that implements a Deque interface. It’s an array-based implementation of a double-ended queue. As the name suggests, a double-ended queue is a queue that allows us to add or remove items from both front and rear ends. Before we dive in, let’s quickly look at a few noteworthy […]
Introduction: A PriorityBlockingQueue in Java implements the BlockingQueue interface and supports the features of a PriorityQueue. So, what’s a BlockingQueue? Following holds true for any implementation of a BlockingQueue: While attempting to retrieve an element, a thread waits if the queue is empty In case of a bounded BlockingQueue implementation, the thread waits till it […]
Introduction: LinkedHashMap is a Map implementation which maintains the insertion order of its elements. It extends the HashMap class: public class LinkedHashMap<K,V> extends HashMap<K,V> implements Map<K,V> Just like a HashMap, it allows storing one null key and many null values. Instantiating LinkedHashMap: We can use one of the following constructors to create a LinkedHashMap: LinkedHashMap() //default […]
Introduction: Spring @Value annotation is used to inject values into variables and method arguments. We can either read spring environment variables or system variables. It also supports SpEL. In this quick tutorial, we’ll explore how to work with Spring @Value annotation. Setup: Let’s start by first defining a few properties in our app.properties file: user.first.name=Sam […]
Introduction: Java Object class provides basic implementation of methods – hashCode() and equals(). These methods are extremely useful especially when working with the Collection framework. The hash table implementations rely on these methods for storing and retrieving data. In this tutorial, we’ll learn about the contract between hashCode() and equals(), their default implementations. We’ll also […]
Introduction: A WeakHashMap in Java is a pretty popular data-structure among mid to senior-level Java developers. The WeakHashMap class is present in the java.util package. It’s a Map implementation which stores weak references to its keys. An entry in a WeakHashMap gets automatically removed when the associated key loses all of its active strong and soft […]
Introduction: HashSet in Java implements Set interface i.e. it doesn’t allow duplicates. It is internally backed by a HashMap which works on the principle of hashing. We can store a null value in a HashSet. Its default capacity is 16 with a load factor of 0.75, where: Load factor = Number of Stored Elements / capacity A […]
Introduction: In this tutorial, we’ll learn how to sort a Java HashMap. We can sort a HashMap either by keys or by values. We’ll discuss both of these strategies. Sorting a Java HashMap: To follow along with the rest of the article, let’s first construct a HashMap: HashMap<Integer, Student> map = new HashMap<>(); map.put(1003, new Student(1003, […]
Introduction: In this article, we’ll quickly learn how to convert a Java List (say an ArrayList) to an array and vice-versa. If you wish to learn more about an ArrayList in general, feel free to read our article on Java ArrayLists. Meanwhile, let’s get going! Convert Java List to Array: Converting a Java List to an array is […]