Java Data Structures Flashcards
List vs ArrayList
List is preferred because can translate into a LinkedList without affecting rest of codebase. List myList = new ArrayList(); is programming to the List interface
Array vs ArrayList
Array is a fixed-size data structure, ArrayList is not. int[arr] = new int[3]; (fixed size of 3)
ArrayList arrL = new ArrayList();
arrL.add(1);
arrL.add(2);
Array vs ArrayList (in terms of data)
Array can contain both primitive data types as well as objects of a class. ArrayList only supports object entries
Primitive = int, char,
HashMap vs Hashtable
HashMap is unsynchronized and allows at max one null key and multiple null values
HashMap properties
1) Does not preserve the order of insertion of entries into the map
2) HashMap has multiple buckets/bins which contain a head reference to a singly liked list. There are as many linked lists as there are buckets. initial bucket size is 16 which grows to 32 when the number of entries in the map crosses 75%
HashMap in depth
HashMap uses its static inner class Node for storing map entries, so each entry in the HashMap is a Node.
HashMap Node structure
final int hash;
final K key;
V value;
Node next;
“Final” keyword
final variable -> value can’t be modified. must be initialized.
final class -> cannot be extended (inherited). Class is also immutable (e.g. String)
final method -> cannot be overriden.
“Abstract” keyword
abstract method -> used for just method declaration in super-classes. They have no implementation so they must be overriden in subclasses
abstract class -> cannot be instantiated. can’t create object of any abstract class. can define static methods.
Interface
contains abstract methods and static constants. can’t have method bodies. If a class implements an interface and does not provide method bodies for all functions specified in the interface, then the class must be declared abstract.
Interface vs Abstract class
interface can have only abstract methods. abstract class can have abstract and non-abstract methods.
variables declared in a Java interface are by default final. abstract class can contain non-final variables.
Abstract class can have final, non-final, static and non-static variables. The interface has only static and final variables.
An interface can extend another Java interface only, an abstract class can extend another Java class and implement multiple Java interfaces.
Members of a Java interface are public by default. A Java abstract class can have class members like private, protected, etc.
When to use abstract classes?
If there are some related classes that need to share some lines of code then you can put these lines of code within the abstract class and this abstract class should be extended by all these related classes.
You can expect that the classes that extend an abstract class have many common methods or fields, or require access modifiers other than public (such as protected and private).
When to use interfaces?
A class can implement more than one interface. It is called multiple inheritances.
You want to specify the behavior of a particular data type but are not concerned about who implements its behavior.
Default/Defender method in interface
Allows new methods to be added to an interface without affecting the classes that implement the interface (but still allowing them to call the new method)
HashMap vs HashSet
In HashSet, we store objects(elements or values) e.g. If we have a HashSet of string elements then it could depict a set of HashSet elements: {“Hello”, “Hi”, “Bye”, “Run”}
HashSet does not allow duplicate elements that mean you can not store duplicate values in HashSet.
HashSet permits to have a single null value.
HashSet is not synchronized which means they are not suitable for thread-safe operations until unless synchronized explicitly.