Thursday, May 22, 2025

GENERICS - USE CASES

 

1. Basic ArrayList Manipulation

Problem:
Create a program to manage a bookstore inventory using ArrayList<Book>. Each Book has a title (String) and price (double).

  • Add 5 books

  • Remove the cheapest book

  • Print the remaining books sorted by price


2. HashMap for Employee Database

Problem:
Build an employee ID lookup system using HashMap<Integer, Employee>.

  • Add employees with ID as key

  • Allow searching by ID

  • Print all employees in alphabetical order


3. HashSet for Social Media Tags

Problem:
Implement a unique hashtag tracker for posts using HashSet<String>.

  • Add tags like "#Java", "#Programming"

  • Prevent duplicates

  • Show trending tags in alphabetical order


4. PriorityQueue for Airport Security

Problem:
Simulate an airport security line where:

  • First-class passengers (priority=1) go first

  • Economy passengers (priority=3) go last

  • Use PriorityQueue<Passenger>


5. LinkedList for Browser History

Problem:
Create a browser history with LinkedList<String> that:

  • Stores visited URLs

  • Allows "back" and "forward" navigation

  • Limits history to 10 entries


6. TreeMap for Product Catalog

Problem:
Build a sorted product catalog using TreeMap<String, Double> where:

  • Key = Product name

  • Value = Price

  • Display products alphabetically

  • Find the most expensive product


7. Stack for Undo/Redo System

Problem:
Implement text editor undo/redo using two Stack<String>:

  • undoStack stores document states

  • redoStack stores undone states


8. ArrayDeque for Ticket Booking

Problem:
Simulate a concert ticket booking system with ArrayDeque<String>:

  • First-come-first-served queue

  • Allow VIPs to cut in line (add to front)


9. Custom Comparator for Employee Sorting

Problem:
Sort a List<Employee>:

  • First by department (ascending)

  • Then by salary (descending)



10. WeakHashMap for Cache System

Problem:
Create a memory-sensitive cache using WeakHashMap<String, Image>:

  • Automatically removes entries when memory is low

  • Ideal for storing temporary image thumbnails



Key Concepts Tested

QuestionCollections UsedCore Concept
1ArrayListSorting, removal
2HashMapKey-value lookup
3HashSetUniqueness
4PriorityQueueCustom ordering
5LinkedListSequential access
6TreeMapAuto-sorting
7StackLIFO operations
8ArrayDequeFIFO + priority
9ListCustom Comparator
10WeakHashMapMemory management

No comments:

Post a Comment