Skip to the content.

Java - Collections - Hands-on

Learning Objectives


Exercise 1: Managing a Library (Using List)

Problem Statement

A library needs a system to store a list of available books. Implement a List to store book titles and perform the following operations:

  1. Add books to the library.
  2. Display all books.
  3. Remove a specific book.
  4. Search for a book by title.

Library

Input & Output

Input:

Expected Output:


Exercise 2: Unique Student Roll Numbers (Using Set)

Problem Statement

A university stores student roll numbers and wants to ensure that duplicate entries are not added. Implement a Set to store roll numbers and perform the following:

  1. Add new roll numbers.
  2. Display all roll numbers.
  3. Attempt to add a duplicate roll number and observe the output.

StudentRecords

Input & Output

Input:

Expected Output:


Exercise 3: Phonebook Directory (Using Map)

Problem Statement

A phonebook application stores contacts with names and phone numbers. Implement a Map to store contact details and perform the following:

  1. Add contacts to the phonebook.
  2. Retrieve a contact’s phone number using the name.
  3. Display all contacts.

PhoneBook

Input & Output

Input:

Expected Output:


Java Collections - Sorting Exercises

Exercise 4: Sorting Books by Title (Using Comparable)

Problem

A library needs to sort books by title in alphabetical order. Implement the Comparable interface to allow sorting of book objects.

  1. Define a Book class with title and author.
  2. Implement the Comparable interface to sort books by title.
  3. Add books to a List and sort them.
  4. Display the sorted list.

Input & Output

Input:

Expected Output:


Exercise 5: Sorting Students by Age (Using Comparator)

Problem

A university maintains student records and needs to sort them by age. Implement the Comparator interface for custom sorting.

  1. Define a Student class with name and age.
  2. Create a Comparator implementation to sort students by age.
  3. Add students to a List and sort them.
  4. Display the sorted list.

Input & Output

Input:

Expected Output:


Exercise 6: Sorting Products by Price (Using Comparator with Lambda)

Problem

An e-commerce store needs to sort products by price. Use Java 8 lambdas to define the sorting logic.

  1. Define a Product class with name and price.
  2. Use a lambda expression to sort products by price.
  3. Add products to a List and sort them.
  4. Display the sorted list.

Input & Output

Input:

Expected Output:


← Excercise 11 Next Topic TBD →

🔗 Related Topics: