0Pricing
Java Academy · Lesson

List & Set Overview

Understand List vs Set: ordering, duplicates, and everyday operations. We will add, remove, search, and iterate with simple examples.

List & Set Overview is a free Java Academy lesson on CoddyKit — lesson 1 of 3. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Java Academy learning path, one of 3 lessons in the course, and your progress syncs across the web and the CoddyKit app.

What are collections?

Collections hold groups of objects. List is ordered and index-based; Set focuses on membership and uniqueness. Pick based on the question you ask: order or uniqueness?

List vs Set — keys

  • List: keeps insertion order, duplicates allowed, access by index.
  • Set: no duplicates; order not guaranteed in HashSet.
  • Choose List for sequences; choose Set to avoid duplicates and for fast membership checks.

Code: ArrayList basics

ArrayList keeps order and allows duplicates. You can access by index and remove by value or index.

public class Main {
  public static void main(String[] args) {
    java.util.List<String> items = new java.util.ArrayList<>();
    // add elements (duplicates allowed)
    items.add("apple");
    items.add("banana");
    items.add("apple"); // duplicate ok

    // access by index
    System.out.println(items.get(0)); // apple

    // remove by value and by index
    items.remove("banana");
    items.remove(0); // remove first

    // print remaining
    System.out.println(items); // [apple]
  }
}

Everyday operations

Common operations:

  • add(e), remove(e), contains(e)
  • size() to get the count
  • Iterate with index or enhanced for

Code: HashSet uniqueness

HashSet removes duplicates and makes membership checks easy. Printing may show elements in any order.

public class Main {
  public static void main(String[] args) {
    java.util.Set<String> tags = new java.util.HashSet<>();
    tags.add("java");
    tags.add("oop");
    tags.add("java"); // duplicate ignored

    System.out.println("size=" + tags.size()); // 2

    // membership check
    System.out.println("has oop? " + tags.contains("oop"));
    System.out.println(tags); // order not guaranteed
  }
}

Code: iteration patterns

Iterate by index when you need positions; use enhanced for for readability when you only need elements.

public class Main {
  public static void main(String[] args) {
    java.util.List<String> list = new java.util.ArrayList<>();
    list.add("A"); list.add("B"); list.add("C");

    // classic for with index
    for (int i = 0; i < list.size(); i = i + 1) {
      System.out.print(list.get(i) + (i < list.size()-1 ? " " : "\n"));
    }

    // enhanced for (for-each)
    for (String s : list) {
      System.out.print("[" + s + "]");
    }
    System.out.println();
  }
}

List vs Set check

Quick check: Which statement is true about List and Set in Java?

Recap

Recap: Use List for ordered sequences and Set for unique items and membership checks. Practice: convert a List to a Set to remove duplicates, then back if you need order.

Frequently asked questions

Is the “List & Set Overview” lesson free?

Yes — the full text of “List & Set Overview” is free to read here on the web, and the Java Academy course includes 3 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Java Academy course, upgrade to CoddyKit PRO.

What will I learn in “List & Set Overview”?

Understand List vs Set: ordering, duplicates, and everyday operations. We will add, remove, search, and iterate with simple examples. You practise Java Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Java Academy?

No prior experience is required. Java Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 3, so you can start here or from the beginning and move at your own pace.

How long does the “List & Set Overview” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Java Academy lesson?

Yes. Every Java Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. List & Set Overview
  2. Iterators & Enhanced for
  3. Collections Utilities
← Back to Java Academy