简体   繁体   中英

Java - Iteration over HashMap replacing ArrayList

Previously I set up an ArrayList to contain objects of records. 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.

The class of this directory implemented Iterable through implements Iterable<Object> (just a one off question, but why is <Object> required?).

The previous code used to iterate over the ArrayList was:

    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(); seems to raise issues, so how do I go about this behaviour with a HashMap?

If you're only interested in the keys, you can iterate through the keySet() of the map:

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

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

If you only need the values, use values():

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

Finally, if you want both the key and value, use 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. The entrySet contains the sets of keys and values. The keys and values are then accessible through getKey() and 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. If your HashMap is only supposed to contain TelephoneRecords objects, replace

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!). 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() ).

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(); (which didn't work) with records.values().iterator(); . It seems you cannot iterate directly over a HashMap but you can iterate over the values (objects) within it.

Furthermore, the issue of getting and printing the contents of the HashMap, I solved through the following code. It iterates over the TelephoneRecords objects within the directory, as specified by TelephoneRecords o : directory and the Iterator method within the directory class. 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.

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.

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