简体   繁体   English

Java-通过HashMap进行的迭代替换了ArrayList

[英]Java - Iteration over HashMap replacing ArrayList

Previously I set up an ArrayList to contain objects of records. 以前,我设置了一个ArrayList来包含记录对象。 I have since replaced the ArrayList with a HashMap where the objects are stored and use the username string of an individual as the key. 从那以后,我用存储对象的HashMap替换了ArrayList,并使用个人的用户名字符串作为键。

The class of this directory implemented Iterable through implements Iterable<Object> (just a one off question, but why is <Object> required?). 通过implements Iterable<Object>implements Iterable<Object>的此目录的类(只是一个问题,但是为什么需要<Object> ?)。

The previous code used to iterate over the ArrayList was: 用于遍历ArrayList的先前代码是:

    public Iterator iterator() {
        return records.iterator();
    }

I then used this iterator for all objects in that class as follows: 然后,将该迭代器用于该类中的所有对象,如下所示:

   for (Object o : directory) { 
            TelephoneRecords temp = (TelephoneRecords) o;
            System.out.println(temp.toString());
    }

Unfortunately, the HashMapName.iterable(); 不幸的是,HashMapName.iterable(); seems to raise issues, so how do I go about this behaviour with a HashMap? 似乎引发了问题,那么我该如何使用HashMap来解决此问题?

If you're only interested in the keys, you can iterate through the keySet() of the map: 如果您仅对键感兴趣,则可以遍历地图的keySet():

Map<String, Object> map = ...;

for (String key : map.keySet()) {
    // ...
}

If you only need the values, use values(): 如果只需要这些值,请使用values():

for (Object value : map.values()) {
    // ...
}

Finally, if you want both the key and value, use entrySet(): 最后,如果需要键和值,请使用entrySet():

for (Map.Entry<String, Object> entry : map.entrySet()) {
    String key = entry.getKey();
    Object value = entry.getValue();
    // ...
}

Hope this helps you. 希望这对您有所帮助。

You can iterate over the entrySet of the HashMap. 您可以遍历HashMap的entrySet。 The entrySet contains the sets of keys and values. entrySet包含键和值的集合。 The keys and values are then accessible through getKey() and getValue(). 然后可以通过getKey()和getValue()访问键和值。 This can be done by the following code: 可以通过以下代码完成:

for (Map.Entry<String, Object> entry : map.entrySet()) {
    String username = entry.getKey();
    TelephoneRecord record = (TelephoneRecord)entry.getValue();
    // Do something with username and record ...
}

Your off-question: 您的疑问:

Object is a type parameter for the HashMap, which says that the Iterable contains Objects. Object是HashMap的类型参数,它表示Iterable包含Objects。 If your HashMap is only supposed to contain TelephoneRecords objects, replace 如果您的HashMap仅应包含TelephoneRecords对象,请替换

implements Iterable<Object>

with

implements Iterable<TelephoneRecords>

That way you're saying that your Iterable contains TelephoneRecords, which in turn lets you avoid casting and get compile errors instead of runtime errors if you're doing something wrong (which is prefered!). 这样,您说的是Iterable包含TelephoneRecords,如果您做错了事,那么它就可以避免转换并获得编译错误,而不是运行时错误(这是首选!)。 That would improve the above code to: 这样可以将上面的代码改进为:

for (Map.Entry<String, TelephoneRecord> entry : map.entrySet()) {
    String username = entry.getKey();
    TelephoneRecord record = entry.getValue();
    // Do something with username and record ...
}

What does "seems to raise issues" mean? “似乎提出了问题”是什么意思? In a HashMap, you can iterate over keys ( map.keySet() ), values ( map.values() ) or key-value pairs ( map.entrySet() ). 在HashMap中,可以迭代键( map.keySet() ),值( map.values() )或键值对( map.entrySet() )。

You can use the entryset from the hashmap, then iterate over that in the same manner as you do already eg 您可以使用哈希图中的入口集,然后以与您已经执行的相同方式对其进行迭代,例如

for (Object o : Directory.getEntrySet()) {

} }

Also, if you type your hashmap it will remove the need for the cast - 另外,如果您输入哈希图,则将无需进行强制转换-

Map<String, TelephoneRecords>

I solved my issue with replacing records.iterator(); 我通过替换records.iterator();解决了我的问题records.iterator(); (which didn't work) with records.values().iterator(); (无效)与records.values().iterator(); . It seems you cannot iterate directly over a HashMap but you can iterate over the values (objects) within it. 似乎您不能直接在HashMap上进行迭代,但是可以在其中的值(对象)上进行迭代。

Furthermore, the issue of getting and printing the contents of the HashMap, I solved through the following code. 此外,通过以下代码解决了获取和打印HashMap内容的问题。 It iterates over the TelephoneRecords objects within the directory, as specified by TelephoneRecords o : directory and the Iterator method within the directory class. 遍历目录中的TelephoneRecords对象,这由TelephoneRecords o : directory和目录类中的Iterator方法指定。 Then a temporary object is assigned to the TelephoneRecords object being iterated over, and the [custom] toString() method used to print out the string of that specific TelephoneRecords object. 然后,将一个临时对象分配给要迭代的TelephoneRecords对象,并使用[custom] toString()方法来打印出该特定TelephoneRecords对象的字符串。

for (TelephoneRecords o : directory) { 
    TelephoneRecords temp = (TelephoneRecords) o;
    System.out.println(temp.toString());
}

And I ended up solving my little side question by following the help provided, using Iterator<TelephoneRecords> (rather than simply using Object) to iterate over the TelephoneRecords objects contained within the directory object. 最后,我通过遵循提供的帮助解决了我的小问题,使用Iterator<TelephoneRecords> (而不是简单地使用Object)遍历目录对象中包含的TelephoneRecords对象。

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

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