Java Collections Framework

CSCI 1913 – Introduction to Algorithms, Data Structures, and Program Development
Adriana Picoral

Collections Framework

The collections framework is a unified architecture for representing and manipulating collections, enabling them to be manipulated independently of the details of their representation. It reduces programming effort while increasing performance.

The framework is based on more than a dozen collection interfaces. It includes implementations of these interfaces and algorithms to manipulate them.

List

  • ArrayList: Resizable-array implementation of the List interface.
  • LinkedList: Doubly-linked list implementation of the List and Deque interfaces.

Map

  • HashMap: Hash table based implementation of the Map interface. This implementation provides constant-time performance for the basic operations (get and put), assuming the hash function disperses the elements properly among the buckets.
  • TreeMap: A Red-Black tree. This implementation provides guaranteed log(n) time cost for the containsKey, get, put and remove operations.

Set

  • HashSet: This class implements the Set interface, backed by a hash table (actually a HashMap instance). This class offers constant time performance for the basic operations (add, remove, contains and size), assuming the hash function disperses the elements properly among the buckets.
  • TreeSet: Implementation based on a TreeMap. This implementation provides guaranteed log(n) time cost for the basic operations (add, remove and contains).
TreeSet<String> reverseStrings = new TreeSet<>();
TreeSet<String> reverseStrings = new TreeSet<>(Comparator.reverseOrder());

Queue

  • PriorityQueue: An unbounded priority queue based on a priority heap.
import java.util.Comparator;

PriorityQueue<Integer> minHeap = new PriorityQueue<>();
PriorityQueue<Integer> maxHeap = new PriorityQueue<>(Comparator.reverseOrder());

Stack

  • Stack: The usual push and pop operations are provided, as well as a method to peek at the top item on the stack, a method to test for whether the stack is empty, and a method to search the stack for an item and discover how far it is from the top. When a stack is first created, it contains no items.

Challenge 1 – gradebook

Data (this code should be in GradeBook’s main):

Student alice = new Student("Alice", new double[]{100, 80, 95, 85, 92});
Student peter = new Student("Peter", new double[]{98, 88, 92, 83, 87});
Student joan = new Student("Joan", new double[]{75, 80, 78, 82, 72});
Student alex = new Student("Alex", new double[]{100, 98, 95, 96, 93});
Student julienne = new Student("Julienne", new double[]{100, 100, 98, 95, 92});
Student philip = new Student("Philip", new double[]{88, 82, 85, 85, 82});

Output:

1. Julienne: 97.0
2. Alex: 96.4
3. Alice: 90.4
4. Peter: 89.6
5. Philip: 84.4
6. Joan: 77.4

Submit your Student.java and GradeBook.java (GradeBook has main with the print statements) to gradescope

Challenge 2 – phone book

Create a PhoneBook class that stores contact information (Name + Phone Number). Phone number should be exactly 10 digits.

  • You can use String.valueOf(phoneNumber).length() == 10 for validation of the Long variable
  • .add(String, Long) and .get(String) should be constant time for runtime
  • Submit your PhoneBook.java to gradescope

Challenge 2 – phone book

Test code:

public static void main(String[] args) {
  PhoneBook contacts = new PhoneBook();
  
  // each of these add calls should be constant time
  contacts.add("Wali", 5553428631L);
  contacts.add("Emilia", 573147373L); // Invalid phone number
  contacts.add("Seo-yun", 5552574665L);
  contacts.add("Flor", 5559083456L);
  System.out.println(contacts.length()); // 3
  
  // each of these get calls should be constant time
  System.out.println(contacts.get("Flor")); // 5559083456
  System.out.println(contacts.get("Emilia")); // Contact not in PhoneBook
}

Submit your PhoneBook.java to gradescope