Array and Collections FAQ's

It stores a collection of similar data types.
The values are stored based on an index, starting from 0 to n-1.

Syntax: int num[] = new int[5]; 

Advantages of array:
- In a single variable we can store multiple values.

Disadvantages of array:
- Supports only similar data types.
- Fixed size.
- High memory wastage.
To overcome these, we go for collections.

  • Collection – An interface in java.util package (parent of List, Set, Map).
  • Collections – A utility class in java.util with static methods (like sort(), reverse(), etc.).
  • Supports dissimilar (heterogeneous) data types.
  • Dynamic memory allocation.
  • No memory wastage like arrays.
  • Overcomes disadvantages of arrays.
  • Array are homogeneous; Collections are heterogeneous.
  • Array are fixed in size; Collections are dynamic (no memory wastage).
  • Array support only similar data types; Collections support dissimilar data types.

It has 3 major types:

  • List
  • Set
  • Map
  • List: An interface that allows duplicate values, maintains insertion order, and stores data as index-based.
  • Set: An interface that does not allow duplicates, is value-based, and does not maintain any specific order.
  • Map: An interface that stores key-value pairs (entries). Keys do not allow duplicates, but values can have duplicates.
  • Array size is fixed.
  • ArrayList size is dynamic.
  • Array can store primitive types (e.g., int, char).
  • ArrayList stores only objects (e.g., Integer, String).

ArrayList

  • It is a class.
  • Best case: Retrieving or searching any value is faster (direct index access).
  • Worst case: Inserting or deleting is slower because all subsequent elements need to be shifted, causing performance issues.

LinkedList

  • It is a class.
  • Best case: Insertion and deletion are faster because data is stored in separate nodes connected via references.
  • Worst case: Searching or retrieving is slower because you must traverse node by node until the target is found.
  • ArrayList → It is non-synchronized and not thread-safe.
  • Vector → It is synchronized and thread-safe.
  • add(index, element)
  • get(index)
  • set(index, element)
  • indexOf(), lastIndexOf()

In Java, we can convert an array to a list in multiple ways:

  • Using Arrays.asList(array) → Gives a fixed-size list (cannot add/remove elements).
  • Using new ArrayList<>(Arrays.asList(array)) → Gives a modifiable list.
  • Using Collections.addAll() → Another flexible way to add array elements into a list.
  • Using Streams (Java 8+) → Modern and clean approach.

Example:

 Integer[] arr = {1, 2, 4, 5, 6}; 
List<Integer> list1 = Arrays.asList(arr); // Fixed-size
List<Integer> list2 = new ArrayList<>(Arrays.asList(arr)); // Modifiable
  • For loop → We can iterate in both forward and backward directions.
  • For loop → We can access values at a specific index position.
  • For-each loop (Enhanced for loop, introduced in Java 1.5) → We can iterate only in forward direction.
  • For-each loop → We cannot directly get values at a specific index.
  • HashSet → Prints in random order. Accepts only one null value.
  • LinkedHashSet → Prints in insertion order. Accepts only one null value.
  • TreeSet → Prints in ascending order (based on ASCII value). Does not accept null values.
  • HashMap → Prints in random order. Keys accept 1 null, values accept multiple nulls.
  • LinkedHashMap → Prints in insertion order. Keys accept 1 null, values accept multiple nulls.
  • TreeMap → Prints in ascending order. Keys do not accept null, values accept multiple nulls.
  • Hashtable → Prints in random order. Both keys and values do not accept nulls.
  • Hashtable: Random order, no null keys or values.
  • HashMap: Asynchronized, not thread-safe.
  • ConcurrentHashMap: Synchronized, thread-safe.
  • retainAll() → Keeps only common elements.
  • removeAll() → Removes common elements.
  • add() → Inserts a new element at the index.
  • set() → Replaces existing element at the index.

Use for-each with entrySet():

 for (Map.Entry<K, V> entry : map.entrySet()) {     System.out.println(entry.getKey() + " = " + entry.getValue()); } 
  • entrySet() → Set of Map entries
  • keySet() → Set of keys
  • values() → Collection of values
  • getKey() → Key of the current entry
  • getValue() → Value of the current entry
  • keySet() returns only the keys.
  • entrySet() returns key-value pairs.

Generics provide type safety and allow only similar data types.
Avoids typecasting at runtime.

Example: List<String> list = new ArrayList<>();

Referring a child class object with a parent class reference.

Example: List list = new ArrayList();

Converting parent reference back to child type.

Example: ArrayList list = (ArrayList) parentRef;

Type Casting is the process of converting one data type into another.