0

I have an very large array of objects where objects are constantly added and every object is dynamic and contains different parameters that define it (getParam1() etc..).

I need a data type that allows me to point directly to an object in the array that contains a specific parameter without having to index through the entire array every time that I require a specific object.

Does any datatype provide this functionality in java or would I have to create my own? And in that case how would I do this?

Thanks.

5
  • Do you want to point to a specific object or to a fixed array position? How is your pointer supposed to react to changes in the array? Commented Jan 20, 2013 at 1:30
  • 4
    Look into using a Map. Commented Jan 20, 2013 at 1:32
  • how large is "very large" 1000 objects, 1,000,000,000 objects? Commented Jan 20, 2013 at 1:36
  • To a specific object that contains a specified paramater, so no not by array position. I would guess 10,000 would be the average amount. I've looked into maps but it's not really ideal when it comes to my objects. Commented Jan 20, 2013 at 1:42
  • 1
    If maps are "not really ideal" then you'll need to say a lot more about your data structures, how and when you're creating, storing, and retrieving them. Otherwise people are going to tell you to use a map. :) Commented Jan 20, 2013 at 2:16

3 Answers 3

1

You could maintain one map per parameter and update each one when the parameter changes. If you don't have control over either the mutating, or the mutable class, then there's not much else you can do other than linearly search through your objects.

Sign up to request clarification or add additional context in comments.

Comments

1

I have an very large array of objects where objects are constantly added and every object is dynamic and contains different parameters that define it (getParam1() etc..).

An array is an static structure. If you're going to constantly add elements to it you should reconsider using a dynamic collection such as a List, a Set or a Map. If you're not modifying the length of the array and you're just updating the different objects on it, you're ok. Still, you are going to have to keep track (an index, amount of objects, etc.) of the array's current status, because you will need to know where to put your objects.

I need a data type that allows me to point directly to an object in the array that contains a specific parameter without having to index through the entire array every time that I require a specific object.

This will require some middle logic one way or another. If you point to an object that has certain parameter what happens if more than one object has it? You have to define which one is the correct one. On the other hand, if you point to the parameter you still need to know the related object.

I'd say that rather than using an array you should try with a Map with entries where the key is the parameter and the value is a Set, containing the different objects related to that parameter.

A Map is sufficient if you only map a parameter to one object, but I'll cover a more complex situation, just in case.

Please note that an object can be present in multiple Sets, because two parameters would require for it to be mapped twice to allow it to be found.

I've looked into maps but it's not really ideal when it comes to my objects.

I don't know your current context and how do you identify your objects. Should you have an ID or any sort of unique identity assertion, you can turn the Map of Sets to a Map or Maps where you can obtain a Map containing objects associated to a certain function and then obtain a particular object through that ID.

Finally, if nothing suffices, you should create a structure that covers your needs. Still, for instant access, you're better off using a Map or a really well-oiled array.

Comments

0

Use a Map instead of an array:

Map<String, MyObject> map = new HashMap<String, MyObject>();

MyObject o;
String someId = o.getId();  // use some identifying id for your objec

map.put(someid, o); // do this for all your objects

then when you need to retrieve one:

MyObject o = map.get(someId);

If you need all the objects (possible, but unlikely):

List<MyObject> objects = map.getValues();

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.