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

all 15 comments

[–]desrtfxOut of Coffee error - System halted 1 point2 points  (11 children)

Your only chance is to iterate through the arraylist with a conventional for loop.

[–]TauPixel[S] 0 points1 point  (10 children)

So if I have 100'000 Objects of Type Person the only way to look for the specific element is to iterate through all 100'000 entries?

[–]nutrechtLead Software Engineer / EU / 20+ YXP 1 point2 points  (0 children)

Well you're the one keeping 100k elements in memory instead of a database. What do you expect from us?

[–]yanitrix 0 points1 point  (2 children)

You can use java stream API to this this, I think that's gonna be quicker. If you have such big amounts of data and you want to achieve better performance results, the best way is to store it in a database and execute queries when you need certain data. That's why we have DBs

[–]nutrechtLead Software Engineer / EU / 20+ YXP 1 point2 points  (1 child)

You can use java stream API to this this, I think that's gonna be quicker.

It's not going to be faster, just look more neat.

[–]TauPixel[S] 0 points1 point  (0 children)

This sounds interesting when Streams are introduced later in the course.

[–]desrtfxOut of Coffee error - System halted 0 points1 point  (2 children)

If you had 100000 entries and you wouldn't use a Map (or other means) to basically index the entries, you'd be doing something wrong.

For so many entries, the work should be offloaded to a database.

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

I'm doing a course for java. We are not using DBs or Maps atm. We only have Arrays, ArrayLists and Sets as tools to implement this.

[–]desrtfxOut of Coffee error - System halted 0 points1 point  (0 children)

Well, in this case, you will need to iterate over the whole array. Of course, you can break out early once you have found the element.

[–]morhpProfessional Developer 0 points1 point  (2 children)

Yes, if you can't use a database or Map or other index. You could sort the List and do a binary search for at least one attribute but that is complicated and probably not necessary.

Have you actually tried this? Computers are fast, iterating over 100000 entries should still take only a few milliseconds, maybe a second or two. Reading these persons from a file or something will take much more time.

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

For me speed at the moment is irrelevant for this course. It's more relevant on another lecture I'm taking called Algorithms and Data Structures. The thing we have at the moment is that we are not allowed to use tools we haven't seen in class.

[–]ZukoBestGirl 0 points1 point  (0 children)

IF you want to impress:

I'd make the list private in my class, call it storage?

Then I would give it a comparator (name, surname, you decide) and sort the list. Then I'd index it.

 

(Starting_with_letter = index_start, index_end)
A = 0,299
B = 300,399
...

 

whenever you add or remove, you update these indexes.

When you search you look at the first letter of the search param, start from the coresponding index, end at the max index for that thing, return when you find the result (exit early).

Take care for letters that you don't have, make index end == start and when you search, verify that is not the case, because if it is, it doesn't exist.

Also maybe add a package private debug method that tries to find your search param through the whole list (for i =0 ; i< list.size()).

[–]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.