Q:

What key ordering in HashMap?

I'm surprised by the order my HashMap comes out. 
I'm getting 
Chandler
Joe
Ross
1

whereas I expected 
Joe
Chandler
Ross
1

in line with the order I added data. 
Can you explain this issue?
java code:
HashMap testing:
public class HashCodeTest { 
    static int count = 0; 

    public static void main(String[] args) { 

        Map employees = new HashMap(); 

        employees.put(new Employee("Joe"), new Integer("1")); 
        employees.put(new Employee("Chandler"), new Integer("2")); 
        employees.put(new Employee("Chandler"), new Integer("2")); 
        employees.put(new Employee("Ross"), new Integer("3")); 

        Iterator iterator = employees.keySet().iterator(); 

        while (iterator. hasNext()) { 
            System.out.println(iterator.next()); 
        } 

        System.out.println(count); 
    } 
} 

class for map key:
import java.util.*; 

class Employee { 
    private String name; 

    public Employee(String name){ 
        this.name = name; 
    } 

    public String toString(){ 
        return name; 
    } 

    public boolean equals(Object obj){ 
        HashCodeTest.count++; 

        if (obj == null) { 
            return false; 
        } 
        if (obj.getClass() != getClass()){ 
            return false; 
        } 

        Employee emp = (Employee)obj; 
        if (this.name == emp.name){ 
            return true; 
        } 
        return false; 
    } 

    public int hashCode(){ 
        return name.hashCode(); 
    } 
} 

A:

The ordering of a plain HashMap is not defined:

This class makes no guarantees as to the order of the map; in particular, it does not guarantee that the order will remain constant over time.

You can instead use a LinkedHashMap, which guarantees an iteration order equal to the order of insertion:

Hash table and linked list implementation of the Map interface, with predictable iteration order. This implementation differs from HashMap in that it maintains a doubly-linked list running through all of its entries. This linked list defines the iteration ordering, which is normally the order in which keys were inserted into the map (insertion-order).
...
This implementation spares its clients from the unspecified, generally chaotic ordering provided by HashMap (and Hashtable), without incurring the increased cost associated with TreeMap.

