Soothsayer_14
Soothsayer_14

Reputation: 3

Matching objects with specified properties using Java

I have 1,000,000 objects. I need to match them with 4 different properties within a specified range .

  1. Length (10meters - 20meters)
  2. Breadth (12meters - 18meters)
  3. Height (3meters - 6meters)
  4. Color (RED)

A simple for loop does it but certainly isnt the most efficient way to go about it. What type of collections should i be looking to implement? How should I've approached this problem.

Upvotes: 0

Views: 352

Answers (2)

Mike Baranczak
Mike Baranczak

Reputation: 8394

A collection would have to load all the data into memory, which is horribly inefficient for a data set that big. You should be using a database - that's what they're designed for.

Upvotes: 3

Tim Bender
Tim Bender

Reputation: 20442

A NavigableSet might work for you. You would need to define a different Comparator for each of the properties. Basically, start with the set sorted by one property and get the subset that fits the range. Place the subset into another NavigableSet sorted by the Comparator for the second property... repeat.

But I don't think any of that beats O(n) time :P since the sort is O(nlogn). At the very least it is interesting.

Upvotes: 0

Related Questions