简体   繁体   中英

How to retrieve a specific object from an array of objects in java

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.

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.

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 Set s, 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 Set s to a Map or Map s 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 .

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();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM