This project demonstrates how to use a HashMap
in Java to store and retrieve client information based on a unique key. In this case, the unique key is the client's Social Security Number (SSN).
A HashMap
is a part of Java’s Collections Framework and provides a way to store data in key-value pairs.
- Keys must be unique (in this project, the SSN is used as the key).
- Values are associated with keys (here, each SSN maps to a
Client
object). HashMap
allows fast lookup, insertion, and deletion operations, typically in constant time, O(1).- The
HashMap
library is a generic class that can be initialized using the generic interfaceMap
. - The generic parameters are
HashMap<K,V>
whereK
is a class representing the key andV
is a class representing the value. - This example will relate
String
to aClient
(a record class).
Map<String, Client> map = new HashMap<>();
map.put("123-45-6789", someClient);
Client result = map.get("123-45-6789");
Represents a hospital client with the following fields:
- SSN
- First name and last name
- Birth date (month, day, year)
- List of comments
Includes:
addComment(String comment)
: Adds a new comment to the client.toString(int indentLevel)
: Formats and returns the client's details with the specified indentation.toString()
: A no-indentation version for convenience.
A helper class that:
- Stores clients in a
HashMap<String, Client>
using SSN as the key. - Allows clients to be added (
addClient
) and retrieved (lookUpClient
).
Demonstrates:
- Creating multiple
Client
objects. - Adding comments to each client.
- Storing them in the
ClientLookup
system. - Looking up clients by SSN and printing their details.
A Few Recent Clients:
Client Information:
SSN: 123-45-6789
Name: Jane Smith
Birth Date: 3/14/1985
Comments:
- Regular checkup.
- Blood pressure is high but not a significantly.
- Advised client to exercise more.
Client Information:
SSN: 987-65-4321
Name: Mark Johnson
Birth Date: 7/22/1978
Comments:
- Annual DOT physical completed.
Client Information:
SSN: 555-55-5555
Name: Emily Davis
Birth Date: 12/5/1992
Comments:
- Client fell asleep in lobby and missed appointment.
- Follow-up visit next week.
- Java
record
classes are immutable by default, but mutable fields likeList
can still be changed unless made unmodifiable. - Using SSN as a key demonstrates how unique identifiers can be used for fast and precise lookups in a system.
org.example
├── Main.java
└── hospital
├── Client.java
└── ClientLookup.java
-
Compile all
.java
files:javac org/example/*.java org/example/hospital/*.java
-
Run the main program:
java org.example.Main