Hashtable in Java

We will find out answers for the following question What is Hashtable Common methods available Hashtable HashTable is a member of the Java Collection Framework as it implements Map Interface. HashTable implements a hash table, which maps keys to values. Any non-null object can be used as a key or as a value. To successfully store and retrieve objects from a hashtable, the objects used as keys must implement the hashCode method and the equals Read more…

HashMap in Java

We will find out answers for the following question What is HashMap in Java Common methods available HashMap HashMap is a Map-based collection class that is used to store Key & value pairs. It is denoted as HashMap<Key, Value> or HashMap<K, V>. This class makes no guarantees as to the order of the map. It is similar to the Hashtable class except that it is unsynchronized and permits nulls(null values and null key). Example The Read more…

Hashing Technique

We will find out what are Hash functions, Hashcode, Collision and Rehashing. Since HashMap and HashSet both use Hashing Technique we will also be able to answer Internal working of HashMap Internal working of HashSet Hash functions Hash functions are used in hash tables, to quickly locate a data record (e.g., a dictionary definition) given its search key. A hash function has the following properties it always returns a number for an object. two equal objects Read more…

Apache POI – Read and Write

In this Blog, we will find out How to read xls, xlsx format Excel sheet using Apache POI API How to write into an Excel sheet using Apache POI API Java doesn’t provide built-in support for working with excel files so we need to rely on  Open Source APIs. I came across Apache POI. Reason I choose Apache POI provides stream-based processing. So memory usage is less. It supports xlsx format as well. I implemented Read more…

Overriding, Hiding and Overloading in Java

Overview In this blog we will find answer following questions What is Overriding What is Hiding What is Overloading Overriding From javaDoc An instance method m1, declared in class C, overrides another instance method m2, declared in class A if: C is a subclass of A. The signature of m1 is a subsignature of the signature of m2. m2 is public, protected, or declared with default access in the same package as C, or m1 Read more…

Primitive vs Wrapper Class in Java

Overview In this Blog we will find answer for following questions What is Primitive Type. What is Wrapper Class. Convert one into another. Ideal scenario to use Primitive vs Wrapper. Primitive Type Primitive types directly contain values. Java defines many primitive type variables. We will see mainly int vs Integer. int is a Primitive Type. Variables of type int store the actual binary value for the integer. int variables are mutable. Wrapper class A class whose object Read more…

String is immutable

Overview We will find answers for the following questions What is String pool? How does memory allocation work for String? Why is String immutable. String pool String pool is a special storage area in Java heap. When a string is created and if the string already exists in the pool, the reference of the existing string will be returned, instead of creating a new object and returning its reference. Does that make sense ??? If Read more…

Collection Framework in Java – Overview

What Is a Collections Framework? A collections framework is a unified architecture for representing and manipulating collections. All collections frameworks contain the following: Interfaces Implementations Algorithms Benefits of the Java Collections Framework Reduces programming effort: By providing useful data structures and algorithms, the Collections Framework frees you to concentrate on the important parts of your program rather than on the low-level “plumbing” required to make it work. Increases program speed and quality: This Collections Framework Read more…

Difference between Comparable and Comparator in Java

Based on my previous posts related to Comparator and Comparable let’s see the difference. Comparable Comparator Should be used when sorting with one element only Should be used when sorting with multiple elements Need to implement compareTo(Obj ob) Need to implement compare(Obj e1, Obj e2) Found in java.lang package Found in java.util package Actual class is not modified. Actual class is modified. Compares “this” reference with the object specified Compares two objects provided to compare

Sorting in Java (Comparator)

In my previous post, we saw Comparable and it’s implementation. Java provides another way to compare objects. Instead of implementing the comparable interface, we can implement the Comparator interface. The main difference between these two interfaces is that the comparable interface defines one method compareTo(), which takes one parameter. The Comparator interface defines one method compare() that takes two parameters (of the same type) and returns the comparison of these two objects. Let’s see implementation of comparator User.java UserComparatorExample.java Read more…