If your Java app feels slow and blaming the database sounds too expensive you probably have a LinkedList doing membership checks like it owes money. Java Flight Recorder or JFR will point the finger fast. The real story is simple and ugly. LinkedList does linear scans for contains which creates lots of node objects and forces the garbage collector to work overtime. HashSet trades some hashCode and equals work for near constant time lookups and far fewer allocations for large collections.
LinkedList is faithful to sequential access and insertions at the ends. It is terrible for membership heavy workloads. contains on a LinkedList is O(n) which means every call walks nodes and touches memory in a cache unfriendly way. Each node is an extra object so allocation churn goes up and garbage collection gets invited to the party.
More allocations mean more GC pauses or more work per cycle. That reduces application throughput and increases latency. HashSet does more CPU per lookup because of hashCode and equals but it avoids the repeated O(n) scans and the node allocations that trigger the GC machinery. For large sets HashSet wins almost every time.
Run JFR under a realistic load and look at method samples and allocation stacks. You will see hot contains calls in the samples and node allocations in the allocation stacks when LinkedList is guilty. If HashSet is used instead you will still see hashCode and equals activity but far fewer allocations and fewer samples tied to list traversal.
Profiling guided changes beat random guessing unless you enjoy surprises in production. Presizing HashSet and improving hashCode often give the biggest wins for collections heavy workloads. If you want a small win with big bragging rights run JFR and point at the flame graph while colleagues guess that increasing the heap will fix everything.
I know how you can get Azure Certified, Google Cloud Certified and AWS Certified. It's a cool certification exam simulator site called certificationexams.pro. Check it out, and tell them Cameron sent ya!
This is a dedicated watch page for a single video.