10 Java Collections And Advanced Features

Advanced Java Programming ยท Unit 10

Java Collections and Advanced Features

Exam-focused notes for Java Collections and Advanced Features (Advanced Java Programming, BIT401): what the TU syllabus asks and how it has actually been tested, with 1 solved past questions from this unit.

Java Collections and Advanced Features in short

This last chapter covers Java's ready-made data structures, the scope of JSP objects, and a short review of Java's main language features.

Java Collections and Advanced Features: notes

Container classes and collections

The Collections Framework (java.util) gives ready data structures called container classes. They store groups of objects and grow as needed. The root interface is Collection. The main kinds are:

TypeOrder and duplicatesCommon classes
ListOrdered, duplicates allowedArrayList, LinkedList, Vector
SetNo duplicatesHashSet, LinkedHashSet, TreeSet
QueueFirst in, first outPriorityQueue, ArrayDeque
MapKey-value pairs, unique keysHashMap, LinkedHashMap, TreeMap
  • ArrayList is fast for reading by index. LinkedList is fast for inserts in the middle.
  • HashSet has no fixed order. TreeSet keeps items sorted.
  • HashMap allows one null key. TreeMap keeps keys sorted and does not allow a null key.
  • Vector is synchronized. ArrayList is not.
  • Map is not a child of Collection.
import java.util.*;

public class CollDemo {
    public static void main(String[] args) {
        List<String> names = new ArrayList<>();
        names.add("Ram"); names.add("Sita"); names.add("Ram");
        Set<String> unique = new TreeSet<>(names);
        Map<String, Integer> marks = new HashMap<>();
        marks.put("Ram", 80); marks.put("Sita", 90);
        System.out.println(names);
        System.out.println(unique);
        for (Map.Entry<String, Integer> e : marks.entrySet())
            System.out.println(e.getKey() + " = " + e.getValue());
        Iterator<String> it = names.iterator();
        while (it.hasNext()) System.out.println(it.next());
    }
}

What this unit covers

  • Container classes and collections
  • Scope of JSP objects
  • Java language features overview

Container classes and collections

0 ยท 5 marks

Write short notes on a. Container Classes b. Scope of JSP Objects [5]

Show solved answer

Model Answer: Container Classes and Scope of JSP Objects

a. Container Classes

Container classes are classes that hold or manage collections of other objects. They provide a mechanism to store, organize, and manipulate groups of objects as a single unit.

Key characteristics:

  • Store multiple objects of the same or different types
  • Provide methods to add, remove, retrieve, and iterate through elements
  • Examples: Vector, ArrayList, HashMap, HashSet, LinkedList
  • Implement the Collection interface (in Java)
  • Allow dynamic resizing of data structures
  • Provide convenient access to stored elements through various methods

Common operations:

  • add() - insert elements
  • remove() - delete elements
  • get() - retrieve elements
  • size() - get number of elements
  • iterator() - traverse elements

Container classes are fundamental to managing data collections efficiently in object-oriented programming.


b. Scope of JSP Objects

JSP objects have different scopes that determine their lifetime and accessibility within the application:

1. Page Scope

  • Object exists only within a single JSP page
  • Destroyed when page processing completes
  • Not accessible to other pages
  • Default scope for most JSP objects

2. Request Scope

  • Object exists for the duration of a single client request
  • Accessible to all pages involved in handling that request (including forwarded pages)
  • Destroyed after response is sent to client

3. Session Scope

  • Object persists across multiple requests from the same user
  • Maintained for the entire user session
  • Destroyed when session expires or user logs out
  • Useful for storing user-specific data

4. Application Scope

  • Object exists for the entire application lifetime
  • Shared across all users and sessions
  • Destroyed only when application stops
  • Used for global application data

Scope declaration syntax: <jsp:useBean id="name" class="ClassName" scope="page|request|session|application" />

Open this answer on its own page โ†’