简体   繁体   English

如何从Java中的对象数组中检索特定对象

[英]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..). 我有大量的对象,其中的对象不断增加,每个对象都是动态的,并且包含定义它的不同参数(getParam1()等)。

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? 是否有任何数据类型都可以在Java中提供此功能,或者我必须创建自己的数据类型? 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..). 我有大量的对象,其中的对象不断增加,每个对象都是动态的,并且包含定义它的不同参数(getParam1()等)。

An array is an static structure. array是静态结构。 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 . 如果要不断向其中添加元素,则应重新考虑使用动态集合,例如ListSetMap 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. 我要说的是,不要使用array ,而应该尝试使用包含键为参数,值为Set条目的Map ,其中包含与该参数相关的不同对象。

A Map is sufficient if you only map a parameter to one object, but I'll cover a more complex situation, just in case. 如果仅将参数映射到一个对象,则Map就足够了,但是为了以防万一,我将介绍更复杂的情况。

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. 请注意,一个对象可以存在于多个Set ,因为需要两个参数才能将其映射两次以允许找到该对象。

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. 如果你有一个ID或任何形式的独特的身份断言,你可以打开MapSet s到一个MapMap S其中您可以获取包含关联到某个功能对象地图,然后通过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 . 不过,对于即时访问而言,最好还是使用Map或运行良好的array

Use a Map instead of an array: 使用Map而不是数组:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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