I'm fairly new in Java, and I'm making a structure for wrapping this kind of data:
Object:
name:'Obj1'
otherAttr:'...'
Object:
name:'Obj2'
otherAttr:'...'
Object:
...
I know that I can make my custom 'Object' class*, but for using builtin java structures I use List<Map<String, String>>. So, for setting new one:
List<Map<String, String>> objects = new ArrayList<Map<String, String>>();
foreach(databaseRow){
Map<String, String> newObject = new HashMap<String, String>();
newObject.put('name', '...');
newObject.put('otherAttr', '...');
objects.add(newObject);
}
I have two questions:
1- If the initializations of the structures (as an ArrayList and as an HashMap) are the best choises between the huge amount of Java structures.
2- If there is a builtin structure that doesen't implies all those initializations (for the List and for each Map).
Thank you!
(*)CLARIFICATION: I use plenty of structures similar of that in my code, and I would avoid to make a lot of custom classes. But if it's the best way, let's do it!
If each of your objects has only these two attributes (name and otherAttr), you could create a class with fields instead of using Map:
public class Data {
private final String name;
private final String otherAttr;
// constructor, getters
}
It may be useful to do so even if you have more attributes, because it gives you compile-time safety - you will never misspell a key. You can also have fields of different types, not just Strings that you would have to parse.
To make your code more meaningful you can also extend existing data structures, e.g.:
public class Data extends HashMap<String, String> {
// extra convenience constructors
}
public class DataList extends ArrayList<Data> {
// extra convenience constructors
}
In regards to your question what collections to use, it really depends on what you need. ArrayList is usually a good choice (or thread-safe Vector), unless you frequently modify the beginning of the list in which case LinkedList would be recommended. HashMap has very good performance, but if you need sorted data you could use TreeMap instead. If you need to maintain order of insertion LinkedHashMap will be the best.
If the structure may vary, yes, it is the correct approach.
If each name is unique, you may use a Map<String,String>.
If the structure is always the same (pairs of name and otherAttr), you may use a POJO (plain old java object), which is a fancy name for an object with getters and setters:
public class MyData {
private String name;
private String otherAttr;
public String getName() {return name;}
public void setName(String name) {this.name=name;}
public String getOtherAttr() {return otherAttr;}
public void setOtherAttr(String otherAttr) {this.otherAttr=otherAttr;}
}
Then you store them in a List<MyData>.
You may provide a constructor for MyData, setting the values of name and otherAttr
MyData(String name, String otherAttr) {
this.name=name;
this.otherAttr=otherAttr
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With