简体   繁体   English

HashMap:打印特定条目

[英]HashMap: printing specific entries

I want to write a program that prints out entries "0" and "4" of the HashMap (ie entry.getKey(0) and entry.getKey(4) but it won't let me do this) What would be another way using what I already have? 我想编写一个程序,打印出HashMap的条目“ 0”和“ 4”(即entry.getKey(0)和entry.getKey(4),但不允许我这样做)这是另一种方式使用我已经拥有的?

Basically I have this: 基本上我有这个:

HashMap<String, Integer> hm = new HashMap<String, Integer>();

I can iterate over each entry using this code: 我可以使用以下代码遍历每个条目:

for (Map.Entry<String,Integer> entry : hm.entrySet())
{
    System.out.println(entry.getKey() + "/" + entry.getValue());
}

Since people have asked for more contextual information, I am storing a set of strings in the HashMap. 由于人们要求提供更多的上下文信息,因此我将一组字符串存储在HashMap中。 For example the 0th entry is "Bob", the 1st entry is "Mindy", the 2nd is "Yasser", the 3rd is "Greg" and the 4th is "Jacky." 例如,第0个条目是“ Bob”,第1个条目是“ Mindy”,第2个条目是“ Yasser”,第3个条目是“ Greg”,第4个条目是“ Jacky”。 I want the program to print out the 0th and 4th entries of the populated HashMap. 我希望程序打印出已填充的HashMap的第0和第4个条目。

If you are specific about the keys in the Map , you can directly use get() method.Like this, 如果您特定于Map的键,则可以直接使用get()方法。

Integer value = hm.get("0");

If you want to iterate then use something like the code below : 如果要迭代,请使用以下代码:

HashMap<String, Integer> hm = new HashMap<String, Integer>();
for (Entry<String, Integer> entry : hm.entrySet())
{
    String key = entry.getKey();
    if(key.equals("0") || key.equals("1"))
    System.out.println(key + "/" + entry.getValue());
}

You cannot pass index to the getKey() method like getKey(0) etc. Refer the documentation. 您不能将索引传递给getKey()方法,如getKey(0)等。请参考文档。

HashMap class makes no guarantees as to the order of the map; HashMap 类不保证地图的顺序。 in particular, it does not guarantee that the order will remain constant over time. 特别是,它不能保证顺序会随着时间的推移保持恒定。 So, if you are looking to fetch values from a HashMap based on index, probably it is not possible. 因此,如果您希望基于索引从HashMap获取值,则可能是不可能的。 Closest to your requirement will be something like LinkedHashMap , which maintains the order of keys for insertion/access order. 最接近您的需求的是LinkedHashMap东西,它维护插入/访问顺序的键顺序。

HashMap works on principle of hashing, we have put(key, value) and get(key) method for storing and retrieving Objects from HashMap . HashMap工作原理是哈希,我们有put(key, value)get(key)方法用于从HashMap存储和检索对象。 When we pass Key and Value object to put() method on Java HashMap , HashMap implementation calls hashCode() method on Key object and applies returned hashcode() into its own hashing function to find a bucket location for storing Entry object, important point to mention is that HashMap in Java stores both key and value object as Map.Entry in bucket which is essential to understand the retrieving logic. 当我们将键和值对象传递put() Java HashMap put()方法时, HashMap实现调用键对象上的hashCode()方法,并将返回的hashcode()到其自己的哈希函数中,以找到用于存储Entry对象的存储桶位置,值得一提的是,Java中的HashMap将键和值对象都作为Map.Entry在存储桶中,这对于理解检索逻辑至关重要。

The HashMap has no defined ordering of keys. HashMap没有定义的键顺序。 You may use LinkedHashMap instead of HashMap It will always return keys in same order (as insertion) when calling keySet(). 您可以使用LinkedHashMap代替HashMap。在调用keySet()时,它将始终以相同的顺序(与插入相同)返回键。 And then you pick the 0th or 4th key.Later you can retrieve the value for the keys you fetched at 0th and 4th location. 然后选择第0个或第4个密钥。之后,您可以检索在第0个和第4个位置获取的密钥的值。

I would recommend simply using the get() method with the provided key. 我建议仅将get()方法与提供的密钥一起使用。 Iteration is not necessary in this case. 在这种情况下,不需要迭代。

Since people have asked for more contextual information, I am storing a set of strings in the HashMap. 由于人们要求提供更多的上下文信息,因此我将一组字符串存储在HashMap中。 For example the 0th entry is "Bob", the 1st entry is "Mindy", the 2nd is "Yasser", the 3rd is "Greg" and the 4th is "Jacky." 例如,第0个条目是“ Bob”,第1个条目是“ Mindy”,第2个条目是“ Yasser”,第3个条目是“ Greg”,第4个条目是“ Jacky”。 I want the program to print out the 0th and 4th entries of the populated HashMap. 我希望程序打印出已填充的HashMap的第0和第4个条目。

With how it is being used, an HashMap makes no sense. 对于如何使用它, HashMap毫无意义。 Use a String[] instead! 请改用String[]

    String[] names = {"Bob", "Mindy", "Yasser", "Greg", "Jacky" };
    System.out.println("When " + names[0] + " met " + names[4]);

Why not a targeted approach: 为什么不采用针对性的方法:

String[] targets = {"0", "4"};
for (String target : targets) {
    System.out.println(target + "/" + hm.get(target));
}

Way more efficient than the "big hammer" full iteration approach, and you get output order for free. 比“大锤子”完整迭代方法更有效,并且您可以免费获得输出订单。

If you have a lot of entries in your Map you may considerate to use one of the navigable collections, like TreeMap. 如果您的地图中有很多条目,则可以考虑使用其中一个可导航的集合之一,例如TreeMap。

    NavigableMap<String,String> map = new TreeMap<>()
    map.subMap("0", true, "4", true);

Visit: http://docs.oracle.com/javase/6/docs/api/java/util/NavigableMap.html 造访: http : //docs.oracle.com/javase/6/docs/api/java/util/NavigableMap.html

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

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