StudyLover
  • Home
  • Study Zone
  • Features
    • Workout Interval Timer
    • Typing Tutor
  • Profiles
  • Contact us
  • Sign in
StudyLover The Unique Identity Set
Download
  1. Java
  2. Programs
The ArrayList Manager : The Phonebook Map
Programs
Initiative: To ensure data integrity by preventing duplicates using the HashSet collection.
import java.util.*; // Import for Sets and Utilities

public class UniqueIdentitySet { // Define class to manage unique identifiers
    public static void main(String[] args) { // Entry point for set demonstration
        System.out.println("--- Unique User-ID Filter ---"); // Print system header
        
        HashSet userIds = new HashSet<>(); // Initialize a set of integers (no duplicates)
        
        userIds.add(1001); // Add a unique ID to the set
        userIds.add(1002); // Add another unique ID
        userIds.add(1003); // Add a third unique ID
        
        System.out.println("Original Set: " + userIds); // Print current IDs
        
        // ATTEMPTING DUPLICATE ADDITION
        System.out.print("Adding 1001 again... "); // Attempting to repeat an existing ID
        boolean result = userIds.add(1001); // HashSet will check if 1001 exists internally
        System.out.println(result ? "Success" : "REJECTED (Already exists)"); // Log rejection result
        
        System.out.println("Final Unique IDs: " + userIds); // Print final contents
        
        // SEARCHING (O(1) Efficiency)
        if (userIds.contains(1002)) { // Check if a specific ID exists in the set
            System.out.println("Verification: ID 1002 is active."); // Confirm existence
        } // End of search check
        
        userIds.remove(1003); // Remove a specific ID from the set
        System.out.println("After Deletion: " + userIds); // Show current state
        
        System.out.println("Iterating through set:"); // Section header
        for (int id : userIds) { // Use for-each loop to print all unique IDs
            System.out.println("ID -> " + id); // Print each ID
        } // End of for-each loop
        
        System.out.println("Set Identity Lab: Finished."); // Final log for program exit
    } // End of main method
} // End of UniqueIdentitySet class
The ArrayList Manager The Phonebook Map
Our Products & Services
  • Home
Connect with us
  • Contact us
  • info.studylover@gmail.com

StudyLover - About us

The Best knowledge for Best people.

Copyright © StudyLover
Powered by Odoo - Create a free website