Java Data Structures Flashcards

1
Q

List vs ArrayList

A

List is preferred because can translate into a LinkedList without affecting rest of codebase. List myList = new ArrayList(); is programming to the List interface

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Array vs ArrayList

A

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);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Array vs ArrayList (in terms of data)

A

Array can contain both primitive data types as well as objects of a class. ArrayList only supports object entries
Primitive = int, char,

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

HashMap vs Hashtable

A

HashMap is unsynchronized and allows at max one null key and multiple null values

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

HashMap properties

A

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%

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

HashMap in depth

A

HashMap uses its static inner class Node for storing map entries, so each entry in the HashMap is a Node.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

HashMap Node structure

A

final int hash;
final K key;
V value;
Node next;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

“Final” keyword

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

“Abstract” keyword

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Interface

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Interface vs Abstract class

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

When to use abstract classes?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

When to use interfaces?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Default/Defender method in interface

A

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)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

HashMap vs HashSet

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly