简体   繁体   English

在 Java 中存储一些数据的最佳方法是什么? (数组与数组列表)

[英]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.所以目前,我正在从 java 中的 XML 文件中提取两个不同的属性,这些属性(对于我的项目)彼此相关,然后将它们打印到控制台。 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.使用这 3 个值,我想要一种存储每一行的方法,但是当我引用“rId”时,我可以获得它对应的“目标”值。 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.我正在考虑使用数组或 arrayList,但我不确定哪个更适合我的目的,或者我不确定 go 关于仅引用一个值并获取另一个值的确切程度。 Could anyone offer me some advice?谁能给我一些建议? Thank you in advance.先感谢您。

If your keys are unique, use a Map .如果您的密钥是唯一的,请使用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.否则,创建一个包含键和值的自定义 Object 并创建一个List (或Set ???)。

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.使用Map ,以 Id ad 作为键,以 Target 作为值。 Note that Map is an interface and thus defines behavior only.请注意,Map 是一个接口,因此仅定义行为。 You will need to pick a specific implementation, such as HashMap.您将需要选择一个特定的实现,例如 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).如果要根据给定键查找值,则将这些对存储在HashMap (更快)或Hashtable (更慢但线程安全)中。

Primitive arrays (and more advanced List -based collections such and ArrayList or Vector ) don't work with name-value pairs out of the box.原始 arrays (以及更高级的基于List的 collections 和ArrayListVector )不适用于开箱即用的名称-值对。 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.原始 arrays 可以提供更高的性能,因为您避免创建对象,但更高级的列表类型 collections 可以更安全、更灵活。

Still, it sounds (?) like you want a Map type collection rather List type one.尽管如此,听起来(?)就像你想要一个Map类型集合而不是List类型一。

UPDATE : By the way, if you use a Map then you can still work with a list of all your "rId" values.更新:顺便说一句,如果您使用 Map 那么您仍然可以使用所有“rId”值的列表。 It will be a Set datatype actually, but that's just a special cousin of List that doesn't allow duplicates:它实际上是一个Set数据类型,但这只是 List 的一个特殊表亲,不允许重复:

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.我认为 java.util.HashMap 会更适合此要求,尤其是在不需要排序的情况下。

// 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).如果我理解正确,您希望能够寻找“rId10”之类的东西并获得值“image2”(仅此而已)。

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). 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.查看http://en.wikipedia.org/wiki/Hash_table也。

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.如果目标值的“键”是唯一的并且只有一个目标映射到它们,那么我建议使用 java.util.HashMap 代替。 You can retrieve any target value by passing in the key.您可以通过传入键来检索任何目标值。 Plus you can Iterate over HashMap like you could an ArrayList.另外,您可以像 ArrayList 一样迭代 HashMap。

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 .阅读有关 collections 的 Java 教程

ArrayList is useful if you need to add elements to it dynamically如果您需要动态添加元素,ArrayList 很有用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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