• 0

Java ArrayList's and Collections


Question

Hi All,

I was wondering if I could get some advise on ArrayList's and Collections,

I currently have an ArrayList which becomes big in size, roughly 100,000 - 1,000,000 items, which are of a type I have created, Customers,

When searching the ArrayList, which I currently do with a for loop, I am searching for a match on 2 string items in the Customer object,

My question is how can I make this more efficient as from what I have seen, maps use numerical indexes, when the data I need a match on are Strings,

Also so you know, it is safe to assume the 2 string items in the Customer object are unique when put together,

Any ideas or advise on the best route to go would be great :)

Thanks, Tim

Link to comment
https://www.neowin.net/forum/topic/1097901-java-arraylists-and-collections/
Share on other sites

6 answers to this question

Recommended Posts

  • 0

A HashMap would work fine, it provides a constant-time search. Just use the 2 strings put together (or separated by some divider) as the key, map it to the Customer object.

Thanks, just had a look into HashMaps, what sort speed different should I expect for finding a match when compared to a for loop through an ArrayList?

OP: Are you searching for an exact match between the two strings or is there some level of partial mapping needed?

Exact match

  • 0

Thanks, just had a look into HashMaps, what sort speed different should I expect for finding a match when compared to a for loop through an ArrayList?

I don't have that much experience, but with a HashMap search should always be pretty much instant and almost non-variable in speed (except for some very, very rare cases that get handled automatically). With an ArrayList looking for something at the beginning of your list is extremely fast, but something near the end will be really extremely slow. But I can assure you HashMap is the best you can get when it comes to speed (as long as things don't have to be sorted).

I can help you with the implementation if you want me to. It's good practice for me during the holidays (just finished my first year of software development, we did a lot of hashmapping the last semester :p)

  • 0

Thanks, just had a look into HashMaps, what sort speed different should I expect for finding a match when compared to a for loop through an ArrayList?

HashMap lookup is O(1 + n/k) (where n = number of elements, k = some constant) while what you were doing is O(N). So for large sets you're looking at several orders of magnitude of improvement. The larger the set the better a hash map becomes compared to a linear search.

http://en.wikipedia....wiki/Hash_table

This topic is now closed to further replies.
  • Posts

    • Chrome is Google's commercial web browser product; it consists of their proprietary features (Googlified everything including profile sync) plus their chromium project barebones web browser. Google developers control the chromium project. The chromium project is the "core" for the web browser product from other vendors including Microsoft Edge (their own proprietary features), Opera (their own features), Brave (their own features), etc... The "downstream" teams at Microsoft, Opera, Brave, etc., can either integrate their original MV2-supporting code into future builds, or they can integrate chromium wholesale and simply add-on their own features/functionalities -- their 'current' build pipeline, so to speak. THIS is why changes at the chromium project affect so many products besides only Google's commercial Chrome browser. -- Edit to add: The chromium project is open-source, and is the piece that's Google's code contribution to the W3C and world wide web at large; there are no licensing fees for others to use the code in their own products... which is what they do. Other browser engines do exist (Firefox's for example) but it's nearly impossible to have both engines bundled into the same 1 browser product.
    • You're comparing settler colonies to colonized war torn nations. It's easy to become the richest by coming in and stealing other people's land, culture and resources.
    • ABP has become "old news" when MV3 started rolling out. They've gone downhill and is now simply irrelevant..... in my experience.
    • About two years ago, I switched to Brave and haven't looked back.
    • FWIW StatCounter has been trash for over 25+ years! Back in the day (circa 2000 and GeoCities pre-Blogger era), it was useful to paste a number on your webpage indicating how many visitors you had. In the ensuing 25+ years, they've grown in reputation and changed their ways... but their overall consumer value has remained abysmal. Serious marketing agencies only cite StatCounter when there's literally no other sources available to support any marketing claims! They are the absolute lowest threshold serious companies use to push any sort of narrative about this-or-that happening. Besides their credibility being what it is, they are forever subject to quality issues. They're so bad that my DNS-level ad-filter prevents me from even viewing their main website! HA!
  • Recent Achievements

    • Week One Done
      rubentuben8 earned a badge
      Week One Done
    • Week One Done
      ARaclen earned a badge
      Week One Done
    • One Year In
      jojodbn earned a badge
      One Year In
    • One Month Later
      jojodbn earned a badge
      One Month Later
    • Week One Done
      jojodbn earned a badge
      Week One Done
  • Popular Contributors

    1. 1
      +primortal
      531
    2. 2
      PsYcHoKiLLa
      231
    3. 3
      +Edouard
      131
    4. 4
      ATLien_0
      88
    5. 5
      Steven P.
      81
  • Tell a friend

    Love Neowin? Tell a friend!