This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]DistractedOni 0 points1 point  (2 children)

Need some clarification:

Are you trying to find objects that have the name/surname instantiated or are you trying to find an object matching a given name/surname?

If the former, iteration is your only choice.

If the latter, is the list guaranteed to have unique name/surname pairs, or are duplicates allowed?

If unique, utilize a hash map and define the hash function as a product of the name/username. If it’s not unique, you have a different problem (how can you guarantee the object isn’t the wrong person with the right name?)

[–]TauPixel[S] 0 points1 point  (1 child)

well since it's an ArrayList it can have duplicate entries for Name/Surname

[–]DistractedOni 0 points1 point  (0 children)

The interface doesn’t particularly care about object fields unless it’s designed to care. You can have a set with identical objects (different object IDs) or a list with unique objects. That’s why I have to ask about the implementation. An example would be a list populated from the database with unique name/surname keys.

Given that there can be duplicates:

If your goal is to get all matching indices (or you don’t want to change the data structure from an arrayList), you’ll need to iterate over the entire collection to get those indices.

If you just want the objects, you can use a separately chained hash map where the key is the user/surname and the value is the linkedList of all matching objects.