Comp Sci Keyword search in a hashmap(using retainsAll) in java

  • Thread starter Thread starter Arnoldjavs3
  • Start date Start date
  • Tags Tags
    Java Search
Click For Summary
The discussion revolves around implementing a keyword search in a Java HashMap that contains a phonebook of Person objects. The user aims to create a method that returns names and addresses matching a keyword, sorted alphabetically. The initial code attempts to use retainAll incorrectly, as it is meant for collections rather than individual objects. The user realizes the need to implement a comparator for sorting and to override the hashCode and equals methods for proper functionality. Ultimately, the user successfully compiles the code after making necessary adjustments to how objects are added to the ArrayList.
Arnoldjavs3
Messages
191
Reaction score
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
 
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.
 

Similar threads

  • · Replies 16 ·
Replies
16
Views
5K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 2 ·
Replies
2
Views
13K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 17 ·
Replies
17
Views
6K
  • · Replies 3 ·
Replies
3
Views
4K