简体   繁体   中英

NetBeans changing order of keys in HashMap

It appears that NetBeans tends to modify the order of key-value pairs when placing them into a HashMap as compared to the HashMap objects created by calling javac.exe and java.exe directly.

Consider the following class definitions ( package and import declarations omitted):

class Coder {

    enum Gender { FEMALE, MALE  }
    String name;
    Gender gender;

    Coder(String name, Gender gender) {
        this.name = name;
        this.gender = gender;
    }

    String getName() { return name; }
    Gender getGender() { return gender; }
}

class Test{
    public static void main(String[] args) {
        List<Coder> list = Arrays.asList(
                new Coder("Alice", Coder.Gender.FEMALE),
                new Coder("Chuck", Coder.Gender.MALE),
                new Coder("Bob", Coder.Gender.MALE));
        Map<Coder.Gender, List<String>> classification = list.stream()
                        .collect(Collectors.groupingBy(Coder::getGender,
                                 Collectors.mapping(Coder::getName, Collectors.toList())));
        System.out.println(classification);

        // Getting metainfo:
        System.out.println("Actual map's type at runtime is " +
                           classification.getClass().getSimpleName());
        System.out.println("OS: " + System.getProperty("os.name") +
                           ", ver." + System.getProperty("os.version"));
        System.out.println("Java ver.: " + System.getProperty("java.version"));

        // Serializing:
        String filename = "classification_cmd.ser";
        // String filename = "classification_NetBeans.ser";  // toggle commenting-out as needed
        File file = new File(filename);
        try (
            FileOutputStream fos = new FileOutputStream(file);
            ObjectOutputStream outs = new ObjectOutputStream(fos)
        )
        { outs.writeObject(classification);
        } catch (IOException ioe) { ioe.printStackTrace(); };
    }
}

Compiling and running the above code directly from command line in Windows (Java ver.1.8.0_066) produced this:

{MALE=[Chuck, Bob], {FEMALE=[Alice]}

Under Linux from the CLI on my Raspberry Pi (Java ver.1.80_065) and via ideone.com (Java ver.1.8.0_112) results were essentially the same: first MALE group, then FEMALE one. However, in NetBeans 8.1 under Windows (Java ver.1.8.0_066) the output was reversed:

{FEMALE=[Alice], MALE=[Chuck, Bob]}

In all cases the actual type of the map in question was HashMap . Serializing the map to disk and peeking inside confirmed that the objects were indeed different in terms of the order of key-value pairs:

- from command line: 在此处输入图片说明

- from inside NetBeans: 在此处输入图片说明

Hence my questions:

  • Why does it happen, and
  • What should be done (I mean, apart from forced sorting such as employing a TreeMap built around a custom Comparator , etc.) to ensure that results will be exactly the same regardless of how we compile and run our programs, from command line or from within NetBeans?

There is no guarantee that a general Map implementation retains any order of the added elements.

If you do care about that order, you do need to use a NavigableMap like TreeMap which keeps the keys ordered. You don't usually need to provide a Comparator, it uses they key type's natural order if none is given.

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