0
public static HashMap<Character,Double> symbols = new HashMap<>();

    public static void getSymbol() {
        char ch = ' ';
        double pro;
        while (ch!='`') {
            Scanner scanner = new Scanner(System.in);
            System.out.println("Input the character");
             ch = scanner.next().charAt(0);
            if (ch == '`')break;
            Scanner num = new Scanner(System.in);
            pro = num.nextDouble();
            symbols.put(ch, pro);

        }
        System.out.println(symbols);

    }
}

when I input a,b,c,q in this particular order
the output of HashMap will be : a,q,b,c

deHaar
  • 17,687
  • 10
  • 38
  • 51

1 Answers1

0

HashMap does not maintain insertion order of the elements.
Use LinkedHashMap instead of HashMap implementation if you want to maintain insertion order

refer: https://docs.oracle.com/javase/8/docs/api/java/util/LinkedHashMap.html

Shivam Puri
  • 1,578
  • 12
  • 25