Java Collections Framework

CSCI 1933 – Introduction to Algorithms and Data Structures
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

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

Contact wali = new Contact("Wali", 5553428631L);
Contact emilia = new Contact("Emilia", 573147373L);
Contact seoYun = new Contact("Seo-yun", 5552574665L);
Contact flor = new Contact("Flor", 5559083456L);
Contact aali = new Contact("Aali", 5654505366L);
Contact sumayya = new Contact("Sumayya", 4591129758L);

Output:

Aali        5654505366
Emilia      573147373
Flor        5559083456
Seo-yun     5552574665
Sumayya     4591129758
Wali        5553428631

Submit your Contact.java and PhoneBook.java (PhoneBook has main with the print statements) to gradescope

Challenge 3 – balanced string

Write a Balanced class with the follow static method signature:

public static boolean isBalanced(String input)

  • the String input argument always contains a sequence of the following characters: (, {, [, ), }, and ) (no need to validate the argument string)
  • Your isBalanced method should return true if the brackets are balanced

Submit your Balanced.java file to gradescope

Challenge 4 – anagrams

Submit your Anagrams.java (include a main with the print) to gradescope

Enter a valid English word (Q to quit): tea
Is "aet" a valid word [Y]es or [N]o? n
Is "eta" a valid word [Y]es or [N]o? y
Is "ate" a valid word [Y]es or [N]o? y
Is "tae" a valid word [Y]es or [N]o? n
Is "eat" a valid word [Y]es or [N]o? y
Enter your valid English word (Q to quit): cat
Is "cta" a valid word [Y]es or [N]o? n
Is "atc" a valid word [Y]es or [N]o? n
Is "act" a valid word [Y]es or [N]o? y
Is "tca" a valid word [Y]es or [N]o? n
Is "tac" a valid word [Y]es or [N]o? n
Enter your valid English word (Q to quit): pot
Is "opt" a valid word [Y]es or [N]o? y
Is "tpo" a valid word [Y]es or [N]o? n
Is "top" a valid word [Y]es or [N]o? y
Is "pto" a valid word [Y]es or [N]o? y
Is "otp" a valid word [Y]es or [N]o? n
Enter your valid English word (Q to quit): q
[tea, eta, ate, eat]
[act, cat]
[opt, pot, top, pto]