FirstHack Learn
Log in Sign up free
Lessons in this course 0/6 All courses Java Fundamentals

CSE

Progress0 / 6 lessons
  1. 1. JVM, JDK and JRE explained properly
  2. 2. Classes, objects and the main method
  3. 3. Primitives, objects and autoboxing
  4. 4. String immutability and why == fails
  5. 5. Collections: ArrayList and HashMap
  6. 6. Exceptions and try-with-resources

Courses › Java Fundamentals

Collections: ArrayList and HashMap

Generics, the List and Map interfaces, and the complexity behind each operation.

12 min read · Lesson 5 of 6 · Pro

Arrays are fixed, collections are not

A Java array has a length fixed at creation. int[] a = new int[5]; is five ints forever. Collections grow.

Java
import java.util.ArrayList;
import java.util.List;

public class Basics {
    public static void main(String[] args) {
        List<String> names = new ArrayList<>();

        names.add("Adnan");
        names.add("Priya");
        names.add("Rahul");

        System.out.println(names.size());          // 3
        System.out.println(names.get(1));          // Priya
        System.out.println(names.contains("Rahul"));  // true

        names.remove("Priya");
        names.add(0, "Meera");                     // insert at front
The rest of this lesson is Pro

The free lessons of Java Fundamentals finish what they start — read those first if you have not. This one goes further, and it is part of the paid half.

A pass opens the paid lessons of every course, the mock test papers and the company-wise series. It ends on its own date; nothing renews by itself.

Get a pass — from ₹29 for 7 days

Already bought one? Log in.