简体   繁体   中英

What is the Best Way to Store some data in Java? (Array vs ArrayList)

So currently, I am extracting two different attributes from an XML file in java that (for my project) are related to each other and just printing them out to the console. However, I want to be able to store these in a way in which referencing one value will retrieve it's corresponding counterpart. For example:

Id: rId11 & Target: image3
Id: rId10 & Target: image2
Id: rId9 & Target: image1

With those 3 values, I'd want a way to store each line, but when I reference "rId" I could get it's corresponding "Target" value. I was thinking about using either an array or an arrayList, but I'm not really sure which would be better for my purposes or how exactly I would go about referencing only one value and getting the other. Could anyone offer me some advice? Thank you in advance.

If your keys are unique, use a Map .

Map<String, String> mak = new HashMap<String, String>();
map.put("rId11","image3");
map.put("rId10","image2");
map.put("rId9","image1");

Reference:

Otherwise, create a custom Object that holds key and value and create a 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; }
    // also implement equals() and hashCode(), please
}

List<Entry> entries = new ArrayList<Entry>();
entries.add(new Entry("rId11","image3"));

Reference:

Use a Map , with the Id ad the key and the Target as the value. Note that Map is an interface and thus defines behavior only. You will need to pick a specific implementation, such as HashMap.

You're being a bit ambiguous about what you want. If you want to lookup a value based on a given key, then store the pairs in a HashMap (faster) or Hashtable (slower but thread-safe).

Primitive arrays (and more advanced List -based collections such and ArrayList or Vector ) don't work with name-value pairs out of the box. They are simply, well... lists. Primitive arrays can offer a bit more performance, since you avoid creating objects, but the more advanced List-type collections can be safer and more flexible.

Still, it sounds (?) like you want a Map type collection rather List type one.

UPDATE : By the way, if you use a Map then you can still work with a list of all your "rId" values. It will be a Set datatype actually, but that's just a special cousin of List that doesn't allow duplicates:

Map<String, String> myMap = new HashMap<String, String>();
myMap.put("rId11","image3");
// ... additional put's for the other values

Set<String> myRids = myMap.keySet();
for(String rId : myRids) {
   // do whatever you want with each rId one-by-one, etc
   // You could also use "myRids.iterator()" to work with an Iterator instead
}

I think a java.util.HashMap would be better suited for this requirement especially if sorting is not required.

// not sure what types these are but this would work better
Map<String, String> m = new HashMap<String, String>();
m.put("rId11", "image3");
String other = m.get("rId11");

If i understand correctly, you want to be able to do look for something like "rId10" and get the value "image2" (and only that).

If that is the case,I think the best (in terms of speed) and easiest solution will be a hash table (java.util.Hashtable) - be careful to use Java Generics as well (after Java 1.5). Check out http://en.wikipedia.org/wiki/Hash_table also.

If the "keys" to your target values will be unique and only ever have one target mapped to them, then I would recommend using java.util.HashMap instead. You can retrieve any target value by passing in the key. Plus you can Iterate over HashMap like you could an ArrayList.

public class Item {
    private String id;
    private String target;

    public Item(String id, String target) {
        this.id = id;
        this.target = target;
    }

    public String getId() {
        return this.id;
    }

    public String getTarget() {
        return this.target;
    }
}

 List<Item> items = new ArrayList<Item>();
 // or
 Map<String, Item> itemsIndexedById = new HashMap<String, Item>();
 // depending on your use-case

Read the Java tutorial about collections .

ArrayList is useful if you need to add elements to it dynamically

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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