What is the Best Way to Store some data in Java? (Array vs ArrayList) -
so currently, extracting 2 different attributes xml file in java (for project) related each other , printing them out console. however, want able store these in way in referencing 1 value retrieve it's corresponding counterpart. example:
id: rid11 & target: image3 id: rid10 & target: image2 id: rid9 & target: image1
with 3 values, i'd want way store each line, when reference "rid" it's corresponding "target" value. thinking using either array or arraylist, i'm not sure better purposes or how go referencing 1 value , getting other. offer me advice? thank in advance.
if keys unique, use map
.
map<string, string> mak = new hashmap<string, string>(); map.put("rid11","image3"); map.put("rid10","image2"); map.put("rid9","image1");
reference:
otherwise, create custom object holds key , value , create list
(or set
???) of these.
public class entry { private final string id; private final string value; public entry(string id, string value) { this.id = id; this.value = value; } public string getid() { return id; } public string getvalue() { return value; } // implement equals() , hashcode(), please } list<entry> entries = new arraylist<entry>(); entries.add(new entry("rid11","image3"));
reference:
Comments
Post a Comment