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

时间:2022-02-26 13:17:45

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? And in that case how would I do this?

是否有任何数据类型在java中提供此功能,或者我是否必须创建自己的数据类型?在那种情况下,我该怎么做?

Thanks.

3 个解决方案

#1


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.

您可以为每个参数维护一个映射,并在参数更改时更新每个映射。如果你无法控制mutating或者mutable类,那么除了线性搜索你的对象之外别无他法。

#2


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..).

我有一个非常大的对象数组,其中对象不断添加,每个对象都是动态的,并包含定义它的不同参数(getParam1()等...)。

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.

数组是静态结构。如果您要不断添加元素,则应重新考虑使用动态集合,例如List,Set或Map。如果你没有修改数组的长度而你只是更新它上面的不同对象,你就可以了。但是,您将不得不跟踪数组当前状态的跟踪(索引,对象数量等),因为您需要知道放置对象的位置。

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.

我要说的是,不应该使用数组,而应该尝试使用带有条目的Map,其中键是参数,值是Set,包含与该参数相关的不同对象。

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 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.

我不知道您当前的上下文以及如何识别您的对象。如果您有ID或任何类型的唯一标识断言,您可以将集合映射转换为映射或映射,您可以在其中获取包含与特定函数关联的对象的映射,然后通过该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或一个非常好的数组。

#3


0  

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

#1


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.

您可以为每个参数维护一个映射,并在参数更改时更新每个映射。如果你无法控制mutating或者mutable类,那么除了线性搜索你的对象之外别无他法。

#2


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..).

我有一个非常大的对象数组,其中对象不断添加,每个对象都是动态的,并包含定义它的不同参数(getParam1()等...)。

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.

数组是静态结构。如果您要不断添加元素,则应重新考虑使用动态集合,例如List,Set或Map。如果你没有修改数组的长度而你只是更新它上面的不同对象,你就可以了。但是,您将不得不跟踪数组当前状态的跟踪(索引,对象数量等),因为您需要知道放置对象的位置。

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.

我要说的是,不应该使用数组,而应该尝试使用带有条目的Map,其中键是参数,值是Set,包含与该参数相关的不同对象。

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 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.

我不知道您当前的上下文以及如何识别您的对象。如果您有ID或任何类型的唯一标识断言,您可以将集合映射转换为映射或映射,您可以在其中获取包含与特定函数关联的对象的映射,然后通过该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或一个非常好的数组。

#3


0  

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