简体   繁体   中英

Why does my hashmap is not printing in chronological order?

why is that my output is not in chronological order or it is not in according of user input base on the output i found it like randomly entries of value How can i print the value of my map in chronological order without using Treemap to sort i'm just using the Hashmap and Map package so what do you think is happening in this logical error? how can i meet the expected output using just Hashmap? is it posibble?

   import java.util.Scanner;
    import java.util.Map;
    import java.util.HashMap;
    
    public class StudentList
    {
        public static void main(String[] args) 
        {
            Scanner sc = new Scanner(System.in);
            Map<String,String> students = new HashMap<>();
    
            for (int x=1; x<=3; x++) 
            {
                System.out.print("Enter student number " + x + ": ");
                String studnum = sc.nextLine();
                System.out.print("Enter first name " + x + ": ");
                String studfname = sc.nextLine();
    
                students.put(studnum, studfname);
                if (x==3) 
                {
                    System.out.println("\nPlease hit ENTER");
                    String enter = sc.nextLine();
                    if (enter.equals("")) {
                        System.out.println("Processing...");
                        students.remove(studnum,studfname);
                        System.out.println("3rd entry removed!\n");
                        System.out.print("Enter student number 3: ");
                        String studnum1 = sc.nextLine();
                        System.out.print("Enter first name 3: ");
                        String studfname1 = sc.nextLine();
                        students.put(studnum1, studfname1);
                }
                else
                {
                    System.out.println("Invalid Please Try again....");
                }
                }
            
            }
            System.out.println("\nStudent List: ");
            for (Map.Entry e : students.entrySet()  ) 
            {
    
                System.out.println(e.getKey() + " : " + e.getValue());
            }
        }
    }

Output:

Enter student number 1: 2018-0004
Enter first name 1: Mark
Enter student number 2: 2018-0017
Enter first name 2: Nika
Enter student number 3: 2018-0134
Enter first name 3: Mairo

Please hit ENTER

Processing...
3rd entry removed!

Enter student number 3: 2018-0134
Enter first name 3: Mairo

Student List:
2018-0017 : Nika
2018-0004 : Mark
2018-0134 : Mairo

Expected Output:

Enter student number 1: 2018-0004
Enter first name 1: Mark
Enter student number 2: 2018-0017
Enter first name 2: Nika
Enter student number 3: 2018-0134
Enter first name 3: Mairo

Please hit ENTER

Processing...
3rd entry removed!

Enter student number 3: 2018-0134
Enter first name 3: Mairo

Student List:
2018-0004 : Mark
2018-0017 : Nika
2018-0134 : Mairo

For chronological order you should use LinkedHashMap .

This is different data structures. HashMap does not remember the insertion order.

It's not part of HashMap , but you can do something like the following with java streams to sort it:

map.entrySet().stream().sorted((s1, s2) -> s1.getKey().compareTo(s2.getKey()))
      .forEach(s -> System.out.println(s.getKey() + " : " + s.getValue()));

You asked:

Why does my hashmap is not printing in chronological order?

why is that my output is not in chronological order or it is not in according of user input

Because such ordering is not a feature of that class. Why does my microwave oven not wash my dishes?

HashMap makes no promise on its iteration order. If you care about iteration order, use a different implementation of Map .

To quote the HashMap Javadoc:

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.

  • If you want the map ordered by the natural order of its keys, use a SortedMap or NavigableMap . TreeMap is one such implementation. You inexplicably refused to use TreeMap which makes no sense to me. If you want to drive, don't refuse the offer of a vehicle.
  • If you want the map ordered by the original insertion order, use LinkedHashMap .

Here is a table diagram graphic I made showing various attributes of the Map implementations bundled with Java. Pay attention to the iteration order column.

在此处输入图像描述

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