Java Tutorial

Java Collections

The Collections Framework gives you List, Set, and Map — shared interfaces with several implementations. Learn the interface first, then pick the class.

Interfaces first

List is ordered and allows duplicates. Set rejects duplicates.Map stores key → value pairs. You program to the interface and construct a concrete class (ArrayList, HashSet, HashMap, …).

Example

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

public class Main {
  public static void main(String[] args) {
    List names = new ArrayList<>();
    names.add("Ada");
    names.add("Ada");
    System.out.println(names.size());   // 2 — List keeps duplicates
  }
}

Common List operations

add, get, set, remove, contains, andsize cover most day-to-day work. Iteration order is insertion order forArrayList.

Example

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

public class Main {
  public static void main(String[] args) {
    List nums = new ArrayList<>();
    nums.add(10);
    nums.add(20);
    nums.add(30);
    nums.set(1, 25);
    System.out.println(nums.contains(25));
    System.out.println(nums);
  }
}

Utility helpers

java.util.Collections offers static helpers: sort, reverse, shuffle, frequency. They work on any compatible List.

Example

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class Main {
  public static void main(String[] args) {
    List nums = new ArrayList<>();
    nums.add(9);
    nums.add(2);
    nums.add(5);
    Collections.sort(nums);
    System.out.println(nums);   // [2, 5, 9]
  }
}

Pick the structure by need: List for sequence, Set for uniqueness, Map for lookup by key. The next lessons dig into Map and Set in detail.

Iterate safely

for-each is the default. When you must remove while iterating, use removeIf or anIterator — not list.remove inside a plain for-each.

Example

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

public class Main {
  public static void main(String[] args) {
    List words = new ArrayList<>();
    words.add("a");
    words.add("bb");
    words.add("ccc");
    words.removeIf(w -> w.length() < 2);
    System.out.println(words);   // [bb, ccc]
  }
}

List.of / Set.of / Map.of create immutable collections. Mutating them throws UnsupportedOperationException.

Try It Yourself

Exercise: Create an ArrayList<Integer> with 4, 1, 3, sort it with Collections.sort, and print the result.

Show solution
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

List nums = new ArrayList<>();
nums.add(4);
nums.add(1);
nums.add(3);
Collections.sort(nums);
System.out.println(nums);

Collections.sort sorts the list in place using natural order for Integer.

Key Takeaways

  • Program to List / Set / Map; construct a concrete implementation.
  • List keeps order and duplicates; Set and Map serve uniqueness and keyed lookup.
  • Collections helpers cover sort, reverse, and more.
  • Prefer removeIf over removing inside a for-each loop.

Worked examples

The short listings above are there so you can see the grammar. The programs here use the same statements on quantities that already have units: a speed, a pH, a count of bases. They are classroom numbers. Air resistance is ignored. g is 9.81 m/s² unless a line says otherwise.

Open them in the Java editor at /java/try. Change one measurement and check whether the result still has the right unit.

Statistics

Sort then summarise

Collections.sort puts marks in order. After that, first and last are min and max without a second scan.

Example

import java.util.*;

public class Main {
  public static void main(String[] args) {
    List<Integer> marks = new ArrayList<>(List.of(72, 91, 88));
    Collections.sort(marks);
    System.out.println(marks.get(0) + " " + marks.get(marks.size() - 1));
  }
}

FAQ: Java Collections

Common questions about this page.

What is the StudyGrid Java tutorial?

The StudyGrid Java tutorial is a full beginner-to-advanced track: syntax, types, input, loops, methods, classes, collections, generics, maps, and lambdas. Each chapter has copy-and-run examples.

Should I run java collections examples locally for better learning?

Yes. Use the browser editor on StudyGrid for a quick check, then Download the example and run it on your computer. Local runs show real errors and the real toolchain, which is one of the fastest ways to learn java collections in this Java Java lesson (Java Collections).

Is the Java editor the same as Try Python or Try C++?

No. Try Java compiles with javac at /java/try and shows stdout plus compiler messages. Try Python stays at /try. Try C++ stays at /cpp/try. Java lessons never open those editors.

Do I need to install a JDK to learn Java?

No. Open a chapter, click Try it in Java, and compile in the browser. You can also download a .java file and compile locally with javac.

Where should I start the Java tutorial?

Start at Java Intro, then Get Started and Syntax. After the first program, continue to output, variables, and if-else. After classes, open Java Examples, then generics, map, and lambdas. Use Next at the bottom of each chapter.

Is the Java tutorial free?

Yes. The Java workshop on StudyGrid (studygrid.in) is free: dashboard, chapters, and the compile-and-run editor.