Introduction
In this tutorial, we will learn how to create a simple Contact Management System using Java collections. This system will store and manage information about contacts such as name, phone number, email address, and more. Collections, a core part of Java, provide a powerful and efficient way to store and manipulate data, and we’ll take full advantage of them in our application.
Understanding the Requirements
The Contact Management System we’re going to build needs to allow users to:
- Add a new contact
- View existing contacts
- Update contact information
- Delete contacts
We’ll use the Java collections framework to store contacts. Specifically, we’ll make use of the HashMap and ArrayList collections for efficient management and retrieval of contacts.
Step 1: Define the Contact Class
To represent a contact, we’ll create a simple Contact class. This class will store basic information such as name, phone number, and email address.
public class Contact { private String name; private String phoneNumber; private String email; // Constructor public Contact(String name, String phoneNumber, String email) { this.name = name; this.phoneNumber = phoneNumber; this.email = email; } // Getters and Setters public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPhoneNumber() { return phoneNumber; } public void setPhoneNumber(String phoneNumber) { this.phoneNumber = phoneNumber; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } @Override public String toString() { return "Name: " + name + ", Phone: " + phoneNumber + ", Email: " + email; } }
This class will hold the necessary details of a contact and provide getter and setter methods for manipulating the data. Additionally, we have overridden the toString() method to display contact details in a readable format.
Step 2: Set Up the Contact Manager Class
Next, we’ll create the ContactManager class, which will handle adding, viewing, updating, and deleting contacts. We’ll use a HashMap to store the contacts, where the contact’s name is used as the key and the Contact object is the value.
import java.util.HashMap; import java.util.Map; public class ContactManager { private Mapcontacts; // Constructor public ContactManager() { contacts = new HashMap<>(); } // Add a contact public void addContact(Contact contact) { contacts.put(contact.getName(), contact); System.out.println("Contact added: " + contact); } // View a contact by name public void viewContact(String name) { Contact contact = contacts.get(name); if (contact != null) { System.out.println(contact); } else { System.out.println("Contact not found."); } } // Update a contact's information public void updateContact(String name, Contact updatedContact) { if (contacts.containsKey(name)) { contacts.put(name, updatedContact); System.out.println("Contact updated: " + updatedContact); } else { System.out.println("Contact not found."); } } // Delete a contact public void deleteContact(String name) { if (contacts.containsKey(name)) { contacts.remove(name); System.out.println("Contact deleted: " + name); } else { System.out.println("Contact not found."); } } }
The ContactManager class manages a HashMap where the key is the contact name and the value is the Contact object. It contains methods to add, view, update, and delete contacts. The methods interact with the HashMap to perform the required operations efficiently.
Step 3: Test the Application
Now that we have both the Contact and ContactManager classes set up, let’s create a main method to test the application.
public class Main { public static void main(String[] args) { // Create a ContactManager instance ContactManager contactManager = new ContactManager(); // Add contacts Contact contact1 = new Contact("Alice", "1234567890", "alice@example.com"); contactManager.addContact(contact1); Contact contact2 = new Contact("Bob", "9876543210", "bob@example.com"); contactManager.addContact(contact2); // View a contact contactManager.viewContact("Alice"); // Update a contact Contact updatedContact = new Contact("Alice", "1112223333", "alice_new@example.com"); contactManager.updateContact("Alice", updatedContact); // View the updated contact contactManager.viewContact("Alice"); // Delete a contact contactManager.deleteContact("Bob"); // Try to view the deleted contact contactManager.viewContact("Bob"); } }
In the main method, we create a ContactManager instance and perform several operations: adding contacts, viewing a contact, updating a contact, and deleting a contact. Each operation is displayed in the console for user feedback.
Output
Contact added: Name: Alice, Phone: 1234567890, Email: alice@example.com Contact added: Name: Bob, Phone: 9876543210, Email: bob@example.com Name: Alice, Phone: 1234567890, Email: alice@example.com Contact updated: Name: Alice, Phone: 1112223333, Email: alice_new@example.com Name: Alice, Phone: 1112223333, Email: alice_new@example.com Contact deleted: Bob Contact not found.
As shown, the program successfully adds, updates, and deletes contacts, with appropriate feedback at each step.
Conclusion
In this tutorial, we have successfully built a simple Contact Management System using Java collections. We used the HashMap to store contacts and implemented basic operations such as add, view, update, and delete. The use of collections makes it easy to manage and manipulate the contacts efficiently. You can further extend this application by adding more features like searching contacts by phone number or email, storing data in a database, or even creating a graphical user interface (GUI).
Hello,
https://Interview.company is now available for sale—a premium domain that’s perfect for recruitment platforms, HR tech startups, or career coaching services.
With its clear, descriptive name and the modern “.company” extension, this domain is a powerful asset for building authority, driving traffic, and establishing your brand as a leader in the interview and recruitment space.
As demand for innovative hiring solutions grows, so will the value of this domain. Secure it now before the price increases!
Domains like this don’t last long. If you’re interested, reply now for details before it’s gone!
Best regards,
Ihab Elsaeed
Domain Name Expert
egacs@egacs.com
Techinterviewguide
–