-1

i wrote a small java program that uses Hashmap. The program prompts to enter 3 employee names with its salary. After entering the 3 employees with salaries, I just want to simply output the map key and values in the console. However, I have observed that the output is not ordered the same way as I input.

Sample input

Enter employee name 1:> Dara

Enter salary for Dara:> 200

Enter employee name 2:> Logan

Enter salary for Logan:> 300

Enter employee name 3:> Agnes

Enter salary for Logan:> 300

Output looks like this:

Agnes > 300.0

Logan > 300.0

Dara > 200.0

Observe that Dara is now placed at the bottom and Agnes is on top. It should be the other way around as how I input it. Please see my code below.

public class Maps {

static String name = "";
static double salary = 0;
static Scanner sc = new Scanner(System.in);
static Map<String, Double> m = new HashMap<String, Double>();


public static void main(String[] args) {

    Maps ms = new Maps();

    int count = 1;

    while(count <= 3){
        System.out.println("Enter employee " + count);
        name = sc.next();

        System.out.println("------------------");

        System.out.println("Enter salary for " + name);
        salary = sc.nextDouble();

        System.out.println("\n");

        m.put(name, salary);

        count += 1;


    }

    ms.displayMap();


}

public void displayMap(){

    System.out.println("========================");

    **I think the issue lies in here**
    for(Entry<String, Double> employee : m.entrySet()){
        String key = employee.getKey();
        Double value = employee.getValue();

        System.out.println(key + " \t " + value);
    }







}

}

Jong Onin
  • 215
  • 1
  • 5
  • 12
  • `HashMap` is not ordered. Try using a [TreeMap](http://stackoverflow.com/a/663396/4622673). – AleSod Nov 16 '16 at 10:24
  • 1
    Possible duplicate of [Java Ordered Map](http://stackoverflow.com/questions/663374/java-ordered-map) – Arnaud Nov 16 '16 at 10:25
  • Try reading a JavaDoc for HashMap class first. It is all there - why spend time and posting on SO if you have documentation right there? – Rafal G. Nov 16 '16 at 10:25

3 Answers3

2

You need to use LinkedHashMapif you want to keep the elements in same order in which they were inserted.

LinkedHashMap

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

Bagira
  • 2,149
  • 4
  • 26
  • 55
  • Please check for duplicate question, there is already complete answers about this subject – AxelH Nov 16 '16 at 10:26
0

Hash Map does not guarantee order. Please refer the below link.

Java 8 Hash Map

Hash table based implementation of the Map interface. This implementation provides all of the optional map operations, and permits null values and the null key. (The HashMap class is roughly equivalent to Hashtable, except that it is unsynchronized and permits nulls.) 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.

notionquest
  • 37,595
  • 6
  • 111
  • 105
  • Please check for duplicate question, there is already complete answers about this subject – AxelH Nov 16 '16 at 10:27
0

Use TreeMap or LinkedHashMap.

I suggest a LinkedHashMap or a TreeMap. A LinkedHashMap keeps the keys in the order they were inserted, while a TreeMap is kept sorted via a Comparator or the natural Comparable ordering of the elements.

Since it doesn't have to keep the elements sorted, LinkedHashMap should be faster for most cases; TreeMap has O(log n) performance for containsKey, get, put, and remove, according to the Javadocs, while LinkedHashMap is O(1) for each.

Sanjay
  • 1,078
  • 11
  • 15