Keyword search in a hashmap(using retainsAll) in java

  • Comp Sci
  • Thread starter Arnoldjavs3
  • Start date
  • Tags
    Java Search
In summary, the task at hand is to create a method that can search a hashmap and return results based on a user-inputted keyword. The hashmap contains Person objects, with the key being the person's name. The method should return all names and addresses that contain the keyword, in alphabetical order. The attempted solution involves creating an ArrayList to store the results, using a for loop to check for the keyword in both the key and the address, and sorting the results using a comparator. There may be some issues with the retainAll method and it may be necessary to override the hashcode and equals methods.
  • #1
Arnoldjavs3
191
3

Homework Statement


Essentially, I am expected to have a method that can do a keyword search through my hashmap and return respective results. My hashmap is as follows:
HashMap<String, Person> phonebook
The string as a key is generally the name of the entry. A person object has 3 fields(Name, address, and phone number). In this case the person's name is also equal to the key. Essentially, when the user inputs a keyword it is expected to return results(in order) of all names and addresses that contain the keyword in alphabetical order.

(Hopefully I explained this properly)

Homework Equations

The Attempt at a Solution


So far I have this:
Java:
public void giveInfo(String keyword) {
        ArrayList<Person> searcher = new ArrayList<Person>();
      
        for (Map.Entry<String, Person> entry : phoneBook.entrySet()) {
            if (entry.getKey().contains(keyword) || entry.getValue().getAddress().contains(keyword)) {
                entry.getValue().retainAll(searcher);
            }
            Collections.sort(searcher);
        }
        for (Person results : searcher) {
            System.out.println(results);
        }
    }

Things I know before hand:
I believe I need to implement the comparator and override my compareTo method so that it can effectively sort the arraylist.

I'm not sure exactly why retainAll isn't working but I believe I need to override hashcode and equals maybe? I think if the condition is true in the foreach loop then it should add the object from entry into the arraylist but I'm not sure.

Thanks.

If I need to show all of my source code let me know
 
  • #3
If it matters all I changed was instead of having the arraylist of person retainall the elements from entry.getValue() (I'm assuming this shouldn't work because it's just an object, not a list structure) i had simply added them into the arraylist if the condition was fulfilled.

Then I implemented comparator in the person object and everything compiled fine.
 

Related to Keyword search in a hashmap(using retainsAll) in java

1. How does keyword search work in a hashmap in Java using retainsAll?

The retainsAll method in Java is used to find the intersection of two hashmaps, meaning it will only keep the elements that are present in both hashmaps. In the context of keyword search, the retainsAll method can be used to find the common keywords between two hashmaps, making it an efficient way to search for specific keywords.

2. What is the time complexity of keyword search using retainsAll in Java?

The time complexity of keyword search using retainsAll in Java is O(n), where n is the number of elements in the hashmap. This is because the method iterates through all the elements in the hashmap to find the common keywords, making it a linear time operation.

3. Can retainsAll be used for partial keyword search in a hashmap in Java?

Yes, retainsAll can be used for partial keyword search in a hashmap in Java. This can be achieved by using the contains method to check if the keyword exists in the hashmap, and then using retainsAll to find the intersection of the hashmaps with the common keywords.

4. Is retainsAll case-sensitive in a hashmap keyword search in Java?

Yes, retainsAll is case-sensitive in a hashmap keyword search in Java. This means that it will only find the common keywords if they are an exact match in terms of case. To account for case-insensitive search, you can use methods like toLowerCase() or toUpperCase() before performing the keyword search using retainsAll.

5. Can other data types be used for keyword search using retainsAll in Java?

Yes, retainsAll can be used for keyword search in hashmaps with other data types in Java. However, the data types must implement the equals() and hashCode() methods for retainsAll to work properly. This is because the method uses these methods to determine if two elements are equal or not.

Similar threads

  • Programming and Computer Science
Replies
5
Views
2K
  • Programming and Computer Science
Replies
3
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
12K
  • Programming and Computer Science
Replies
20
Views
31K
  • Programming and Computer Science
Replies
6
Views
1K
  • Sticky
  • Feedback and Announcements
Replies
2
Views
495K
Back
Top