I have set of Objects of the same class which have different values of their attributes. and I need to find the best match from a function under given scenarios out of these objects.

In future these objects might increase as well.

Quite similar to the way we have Color class in awt. we have some static color objects in the class with diff rgb values. But in my case say, I need to chose the suitable color out of these static ones based on certain criteria.

So should I keep them in an arrayList or enum or keep them as static vars as in case of Colors. because I will need to parse through all of them and decide upon the best match. so I need them in some sort of collection. But in future if I need to add another type I will have to modify the class and add another list.add(object) call for this one and then it will violate the open-close principle. How should I go about it ?

EDIT: To be more precise I have a roughly 7-8 branches of restaurants which wont increase that much..may be by 3-4 in few years and I need a function that will return the nearest Restaurant satisfying few other criteria.

and For that I need to parse through all of them to decide which one suits the customer the most.


4 Answers 4


Store the stores on disk. I would recommend using a database but for the small amount of data xml would do fine as well. On application start up read this list into a collection. You wouldn't need to create constants for each because you will just be querying the list when you need a store. This will keep you from having to update your code just to add a new store. Also since you are computing distances you will need a list of all the zip codes and their corresponding latitude and longitude so that you can compute the distance between two zipcodes. This could also be stored on disk and most likely will need updating more frequently than your store list. In addition, you could create an admin console that will let you add new stores and update zip codes without having to manually write to the storage mechanism you have chosen.


It really depends on how many objects you have and how many dimensions each object is free to move in.

Finding the closest match for a mutli-dimensional value is something which lends itself to spatial databases and space partitioning trees e.g. a k-d tree. Of course if you only have a few dozen objects then just checking them all is also an option.

I think your analogy of the Color class may be throwing you slightly as (AFAIK) I don't think Color provides any nearest matching capabilities anyway and also you probably need to leave the RGB colour space to define a useful distance metric


Go with an ArrayList. You can modify it freely and search it fast. You won't violate the open-close principle.

Exception: if you have a lot of entries, say over a million, and you're running at 80%+ on your CPU and want more speed, things get interesting. Large arrays can do odd things to memory in this situation if you are not careful, and ArrayList will do awkward things when you're not looking. Also, if you want to spot the closest object in the list to some arbitrary object--say, the closest color you have to something with an RGB of 233.121.55--you'll need a multi-dimensional storage system. Say a quadtree or, with colors an octree. (If your colors have an alpha component, I suppose you'd need a hexadecimaltree.)

But I'm betting the ArrayList will work just fine for you. You can scan 100,000 entries in an ArrayList in the blink of an eye.


Simply store your objects in an ArrayList of type Object. Then, you can add any type of object to that collection. It can be a mixed, aka heterogeneous collection.

List<Object> mixedList = new ArrayList<Object>();
list.add(new String("I'm a string"));
list.add(new Integer(1));
list.add(new Long(1l));
  • 1
    He said they are of the same class, so I don't think this is relevant.
    – Jeremy
    Mar 21, 2012 at 20:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.