Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Overview In this challenge, we revisit the concept of hashing with the Java Hash

ID: 3710942 • Letter: O

Question

Overview In this challenge, we revisit the concept of hashing with the Java HashSet. You will create a SetTest application that demonstrates creating and processing a generic Set limited to String objects, with the Set implemented as a HashSet. The application will include an inputSetNames method, a displaySetNames method, and a searchSetNames method Specifications Create the skeleton for a program called SetTest. Declare a private static Scanner object. In method main, declare a generic Set called names that only accepts String objects. Use the Set implementation java.util.HashSet. . In method main, display a message explaining that a Set of names will be entered, displayed and searched Call the method to input the names o Prompt and read the first name o Add the name to the names Set o Continue prompting and reading names until the user types "end". o Add each name to the names Set after user entry Call the method to display the names Set. Call the method to search for a name in the names Set o Prompt and read a search name o Display the proper message indicating whether the search name was found Hint: use the Set contains method The user can continue to search for another name or enter "end" when finished searching o Additional Requirements Perform a short Internet search on HashSets and HashMaps and add a comment at the end of your program with a response to this question . o What are the differences in the way HashSet and HashMap are internally implemented in Java? Do you think HashSet is preferred to HashMap for storing a collection of names? Why or why not? Cite your sources.

Explanation / Answer

JAVA CODE

import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;


public class SetTest {

   // Scanner object to accept input from user.
   private static Scanner input = new Scanner(System.in);
   public static void main(String[] args) {
       //creating a generic set with creating an object of hash set.
       Set<String> names = new HashSet<String>();
       System.out.println("The program will prompt you to enter names into a Set");
       System.out.println("The names in the Set will be displayed");
       System.out.println("Finally, you will have an opportunity to search a name in the Set.");

       do {
           System.out.print("Add a name to the set; enter "end" to terminate input: ");
           String name = input.next(); // Accepting input
           if(name.equals("end")) // Check if input is "end"
               break;
           names.add(name); // Adding the name to set.
       }while(true);
      
       System.out.println("Set : " + names); // This calls the toString() method of Hash Set.
      
       do{
           System.out.print("Enter a search name; enter "end" to terminate searching : ");
           String searchName = input.next(); // Accept Search input
           if(searchName.equals("end")) // Check if the input "end"
               break;
           if(names.contains(searchName)) // contains function look for element in set.
               System.out.println(searchName + " was found in the set.");
           else
               System.out.println(searchName + " was not found in the set.");
       }while(true);
   }

}

SAMPLE OUTPUT

The program will prompt you to enter names into a Set
The names in the Set will be displayed
Finally, you will have an opportunity to search a name in the Set.
Add a name to the set; enter "end" to terminate input: Noah
Add a name to the set; enter "end" to terminate input: Oliver
Add a name to the set; enter "end" to terminate input: Sophia
Add a name to the set; enter "end" to terminate input: Zoe
Add a name to the set; enter "end" to terminate input: Liam
Add a name to the set; enter "end" to terminate input: Oliver
Add a name to the set; enter "end" to terminate input: end
Set : [Zoe, Oliver, Liam, Noah, Sophia]
Enter a search name; enter "end" to terminate searching : Joe
Joe was not found in the set.
Enter a search name; enter "end" to terminate searching : sophia
sophia was not found in the set.
Enter a search name; enter "end" to terminate searching : Sophia
Sophia was found in the set.
Enter a search name; enter "end" to terminate searching : end

ADDITIONAL REQUIREMENT

Please follow the link for insights.

https://beginnersbook.com/2014/08/hashset-vs-hashmap-java/

Also for collection of names we must use HASHSET as we don't have any key value pairs which is the basic principle of using HASHMAP