what is the fastest way of sorting through thousands of objects?

I need to sort through thousands of objects using some sort of identifier, but im not sure what the fastest way is? Arraylist List Map<string, user>?
14 Replies
JavaBot
JavaBot5mo ago
This post has been reserved for your question.
Hey @Ferra! Please use /close or the Close Post button above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically marked as dormant after 300 minutes of inactivity.
TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here. 💤 Post marked as dormant
This post has been inactive for over 300 minutes, thus, it has been archived. If your question was not answered yet, feel free to re-open this post or create a new one. In case your post is not getting any attention, you can try to use /help ping. Warning: abusing this will result in moderative actions taken against you.
Madjosz
Madjosz5mo ago
What do you mean by "sorting through"? Do you need it to be sorted? Do you need to find things based on some key?
JavaBot
JavaBot5mo ago
💤 Post marked as dormant
This post has been inactive for over 300 minutes, thus, it has been archived. If your question was not answered yet, feel free to re-open this post or create a new one. In case your post is not getting any attention, you can try to use /help ping. Warning: abusing this will result in moderative actions taken against you.
Ferra
FerraOP5mo ago
I have a list of user objects, and I need to be able to get one by user ID string
Madjosz
Madjosz5mo ago
Use a HashMap then.
ayylmao123xdd
ayylmao123xdd5mo ago
well in this case map is fastest
Ferra
FerraOP5mo ago
I am using a hashmap, but running through it still takes around 3 minutes
ayylmao123xdd
ayylmao123xdd5mo ago
lol wtf show the code
Madjosz
Madjosz5mo ago
Well you need to be more specific what you want to do then.
ayylmao123xdd
ayylmao123xdd5mo ago
thousands of objects and 3 min to get the key sounds like somethings wrong with the implementation
Ferra
FerraOP5mo ago
String searchResults = "";
for (Map.Entry<String, String> entry : data.entrySet()) {
if ((entry.getKey().toLowerCase().contains(searchTerm.toLowerCase()) || entry.getValue().toLowerCase().contains(searchTerm.toLowerCase()) && !searchResults.contains(entry.getKey() + "," + entry.getValue() + "\n") && !searchTerm.contains(";"))) {
searchResults += entry.getKey() + "," + entry.getValue() + "\n";
} else if (entry.getKey().toLowerCase().contains(searchTerm.toLowerCase().split(";")[0]) && entry.getValue().toLowerCase().contains(searchTerm.toLowerCase().split(";")[1]) && searchTerm.contains(";")) {
searchResults += entry.getKey() + "," + entry.getValue() + "\n";
} else {
//searchResults += "no matches";
}
}
//System.out.println("Search results: " + searchResults);
String searchResults = "";
for (Map.Entry<String, String> entry : data.entrySet()) {
if ((entry.getKey().toLowerCase().contains(searchTerm.toLowerCase()) || entry.getValue().toLowerCase().contains(searchTerm.toLowerCase()) && !searchResults.contains(entry.getKey() + "," + entry.getValue() + "\n") && !searchTerm.contains(";"))) {
searchResults += entry.getKey() + "," + entry.getValue() + "\n";
} else if (entry.getKey().toLowerCase().contains(searchTerm.toLowerCase().split(";")[0]) && entry.getValue().toLowerCase().contains(searchTerm.toLowerCase().split(";")[1]) && searchTerm.contains(";")) {
searchResults += entry.getKey() + "," + entry.getValue() + "\n";
} else {
//searchResults += "no matches";
}
}
//System.out.println("Search results: " + searchResults);
trying to search for words that are maybe in around 50ish entries work just fine, but if i search for something thats in the majority, then it takes ages it doesnt make sense to me since either way it is going through the same amount of map entries
Madjosz
Madjosz5mo ago
Ok, this is not getting entries by a key. You want to find by search terms and this is a different use case. First of all you are lowercasing a lot in your search and each lowercase is allocating new strings. An easy improvement might be to just store the lowercase strings in a separate map. That will improve the search time a lot. If you want to scale more though you need to investigate for better string-search-optimized datastructures like radix trees or similar.
ayylmao123xdd
ayylmao123xdd5mo ago
can you explain what you wanna do with this code cuz you are doing a lot of checking with entry and search term
JavaBot
JavaBot5mo ago
💤 Post marked as dormant
This post has been inactive for over 300 minutes, thus, it has been archived. If your question was not answered yet, feel free to re-open this post or create a new one. In case your post is not getting any attention, you can try to use /help ping. Warning: abusing this will result in moderative actions taken against you.

Did you find this page helpful?