简体   繁体   English

选择最佳的数据结构

[英]Selecting best data structure

Name - Code (String)
A    - 123
B    - 123 
C    - 23
D    - 123
E    - 23
F    - 23
G    - 66
H    - 66

What's the best data structure to represent this data. 代表此数据的最佳数据结构是什么。 Names should be able to iterate easily. 名称应该能够轻松地进行迭代。

Edit 编辑

Names are unique. 名称是唯一的。 What's needed to be done is something like this. 需要做的是这样的事情。 Had doubts in using Hashmap that why I asked. 我对为什么使用Hashmap产生疑问。 Code is a STRING 代码为STRING

for( loop dataStructure names (lets say n)){ 
   if(NAME.equals(n){
     String code = dataStructure.get(n);
     do somthing 
   }
}

If the names are unique, a HashMap woulrd be apropriate. 如果名称是唯一的,则HashMap将是适当的。

You can iterate over the keys with keys() . 您可以使用keys()遍历keys()

To iterate over the entries you can iterate over the entrySet() . 要遍历条目,可以遍历entrySet() See the JavaDoc of Map 参见Map的JavaDoc

If you need to perform a reverse lookup you could use the BiMap from Guava. 如果需要执行反向查找,则可以使用Guava的BiMap (General a very good library) (一般一个很好的图书馆)

Map entries example: 地图条目示例:

public final class MapExample {

  public static void main(String[] args) {
  Map<String, String> map = new HashMap<>();
    map.put("A", "123");

    for (Map.Entry<String, String> mapEntry : map.entrySet()) {
      if (mapEntry.getKey().equals("A")) {
        final String code = mapEntry.getValue();
        System.out.println("Your desired code: " + code);
      }
    }
  }

}

But since NAME seems to be a constant, you could simple do String code = map.get(NAME) ? 但是由于NAME似乎是一个常数,因此您可以简单地执行String code = map.get(NAME)吗?

Sounds like a Map . 听起来像一张Map Specifically, if the order of the names is important, you can use a TreeMap . 具体来说,如果名称的顺序很重要,则可以使用TreeMap

You can populate it with the put method, and then iterate over the entries (or just the keys, or just the values): 您可以使用put方法填充它,然后遍历条目(或仅键,或仅值):

// Fill the map:
Map<String, String> map = new TreeMap<>();
map.put("A", "123");
map.put("B", "123");
// etc...

// Iterate over it:
for (Map.Entry<String, String> entry : map.entrySet()) {
    System.out.pritnln ("Key: " + entry.getKey() + " value: " + entry.getValue());
}

EDIT: 编辑:
If the order is not important, as noted in later edits to the OP, a HashMap would do just fine. 如果顺序不重要(如稍后在OP中所做的编辑所述),则HashMap会很好。

Note, however, that if you're looking for a specific key, like stated in the example in the OP, there's no point in looping over the keys - you just need to use get or containsKey : 但是请注意,如果要查找特定的键(如OP中的示例中所述),则没有必要遍历这些键-您只需要使用getcontainsKey

String name = ...;
String code = map.get(name);
if (code != null) {
    // do something...
}

I thinks you are considering this: 我认为您正在考虑以下问题:

public enum Code {
    A("123"),
    B("123"), 
    C("23"),
    D("123"),
    E("23"),
    F("23"),
    G("66"),
    H("66");

    final public String value;

    Code(String value) {
        this.value = value;
    }
}

String h = Code.H.value;

for (Code code : Code.values()) {
    System.out.printf("Name %s, code %s%n", code, code.value);
}

I would suggest go for HashMap 我建议去HashMap

  1. The HashMap class uses a hashtable to implement the Map interface. HashMap类使用哈希表来实现Map接口。 This allows the execution time of basic operations, such as get( ) and put( ), to remain constant even for large sets 这使得基本操作(例如get()和put())的执行时间即使对于大型集合也保持恒定

  2. HashMap are efficient for locating a value based on a key and inserting and deleting values based on a key. HashMap对于基于键定位值以及基于键插入和删除值非常有效。 The entries of a HashMap are not ordered. HashMap的条目未排序。

 import java.util.HashMap; import java.util.Set; public class MyHashMapRead { public static void main(String a[]){ HashMap<String, Integer> hm = new HashMap<String, Integer>(); //add key-value pair to hashmap hm.put("A", "1"); hm.put("B", "2"); hm.put("C","3"); System.out.println(hm); Set<String> keys = hm.keySet(); for(String key: keys){ System.out.println("Value of "+key+" is: "+hm.get(key)); } } } 

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

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