Java compilation error in this HashMap. Why is the compiler catching an Object being converted into a String?
<pre><code>import java.util.HashMap;
import java.util.TreeMap;
import java.util.Map;
import java.util.Iterator;
import java.util.Set;

public class Maps {
  public static void main(String[] args) {
    Map mapA = new HashMap();
    Map mapB = new TreeMap();

    mapA.put("key1", "element 1");
    mapA.put("key2", "element 2");
    mapA.put("key3", "element 3");

    // The three put() calls maps a string value to a string key. You can then
    // obtain the value using the key. To do that you use the get() method like this:

    String element1 = (String) mapA.get("key1");
    // why do I need the type cast on the right?
    System.out.println(element1);

    //Another examples with maps
    Map vehicles = new HashMap();
    vehicles.put("BMW", 5);
    vehicles.put("Mercedes", 3);
    vehicles.put("Audi", 4);
    vehicles.put("Ford", 10);

    System.out.println("Total vehicles: " + vehicles.size());

    for(String key: vehicles.keySet())
      System.out.println(key + " - " + vehicles.get(key));
    System.out.println();

    String searchKey = "Audi";
    if (vehicles.containsKey(searchKey))
      System.out.println("Found total " + vehicles.get(searchKey) + " "
        + searchKey + " cars!\n");

    // clears vehicles
    vehicles.clear();

    //should equal to 0 now
    System.out.println("Vehicle now contains this many vehicles :" + vehicles.size());

    // Lets iterate through the keys of this map:

    Iterator iterator = mapA.keySet().iterator();
    System.out.println(iterator);  // How to inspect this? Is it a kind of map?

    Map mapC = new HashMap();
    while(iterator.hasNext()){
      Object key   = iterator.next();
      Object value = mapA.get(key);
      mapC.put(key,value);
    } // Is there a better way to take the contents of the iterator and put them in a new map?
    System.out.println(mapC);
    //create a new hashmap
    HashMap hm = new HashMap();
    // put elements to the map

    hm.put("Zara", new Double(3434.34));
    hm.put("Mahnaz", new Double(123.22));
    hm.put("Ayan", new Double(1378.00));
    hm.put("Daisy", new Double(99.22));
    hm.put("Qadir", new Double(-19.08));

    //get a set of the entries
    // The entrySet( ) method declared by the Map interface returns a Set containing the
    // map entries.

    Set set = hm.entrySet();

    // get an iterator
    Iterator i = set.iterator();

    // Display elements

    while(i.hasNext()) {
      Map.Entry me = (Map.Entry)i.next();
      System.out.println(i.getClass());  // get the class of i
      System.out.println(i instanceof Iterator); // checks to see if i is of class Iterator
      System.out.print(me.getKey() + ": ");
      System.out.println(me.getValue());
    }
    System.out.println();
    // Deposit 1000 into Zara's account
    double balance = ((Double)hm.get("Zara")).doubleValue();
    hm.put("Zara", new Double(balance + 1000));
    System.out.println("Zara's new balance: " +
      hm.get("Zara"));
  }
}
</code></pre>

<p>This is my error:</p>

<blockquote>
  <p>Maps.java:53: error: incompatible types: Object cannot be converted to
  String
      for(String key: vehicles.keySet())</p>
</blockquote>

<p>My questions are</p>

<ol>
<li><p>Why is that error occurring? Why is an object trying to be converted   to a string? I thought I had put strings as keys into the vehicles HashMap. What is going on?</p></li>
<li><p>Why is the typecast needed in the line:</p>

<pre><code> String element1 = (String) mapA.get("key1");
</code></pre></li>
</ol>