So I've got a program that contains a bunch of records in a Set. The set could have a few items or potentially hundreds of thousands. One bit of data each record has is a timestamp. I need to eliminate all items in a set but one that are within 15 seconds of each other. What is the most efficient way to do that?
Currently I create a duplicate of the set. Then I iterate through the set comparing the first item to every other item, and repeat. If a match is found that is within 15 seconds, I remove it from the duplicate set. Then the duplicate set is written out to a file.
Obviously this works but I finally realized that this is ridiculously inefficient. For large sets this seems to take a crazy long time, assuming it's not some other problem occurring. Can someone provide me with a smarter, faster, more efficient (or just a proper) way to do this in Java? I'm realizing since the records contain timestamps that sorting them would probably help tremendously. I'd like to keep this all contained within the program though so I guess I need to look into sorting and Comparators.
I just can't quite wrap my head around the problem. I've come up with some other thoughts to improve my code but I can't help but feeling I'm still coming at this entirely wrong. Thanks for any suggestions.
Oh, and this is for work, not school or anything so any help is appreciated.