Reputation: 3181
I need to store information about two attributes of an instance and there could be more than one of them.
So, essentially, I need to store information about two attributes for a set of objects. Which data structure should I be using? I'm using Java. Also, of the two attributes, one is a string and the other is an object.
Upvotes: 0
Views: 228
Reputation: 1367
You can create a model class with two attributes mentioned in your question such as String and Object as follwos
public class Model {
String str;
Object object;
public String getStr() {
return str;
}
public void setStr(String str) {
this.str = str;
}
public Object getObject() {
return object;
}
public void setObject(Object object) {
this.object = object;
}
}
then u can use any collection framework to store the corresponding model class object such as
List<Model> list=new ArrayList<Model>();
list.add(new Model());
and so on
Map, Set ..
Upvotes: 0
Reputation: 62439
Do you need to search by one of them? Then use a HashMap
with that member as the key. For example if you need to search by the String:
Map<String, Object> map = new HashMap<String, Object>();
Upvotes: 1
Reputation: 240860
You can define an Helper class like
class DataRepresenterHelper {
private String name;
private Object foo;
//setters + getters + constructer
}
and then based on the need you could store them in Map, List or Set or something..
Upvotes: 3
Reputation: 9307
You can create a simple Pair class that holds the two attributes and add then to a map for each object that you want to store.
If you don't want the Pair then you can have two maps each holding one attribute.
Upvotes: 1