N
N
Nikita072021-10-15 13:13:37
Java
Nikita07, 2021-10-15 13:13:37

How does the computeIfAbsent function work in Java?

Please help me with the following Java code:

private void loadYamlMatcher(MappingNode entry, String filename) {
        List<MappingNode> matcherConfigList = matcherConfigs
            .computeIfAbsent(filename, k -> new ArrayList<>(32));
        matcherConfigList.add(entry);
    }


In this case, the following file names are passed to filename in sequence:

Amazon
AmazonDevices
Android
AndroidDeviceOverrides
Anonymized
Apple
And in matcherConfigs they are written in the following order

Apple
Android
Anonymized
Amazon
AmazonDevices
AndroidDeviceOverrides The
question is, why is the alphabetical sorting lost?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Gornostaev, 2021-10-15
@Nikita07

I suspect matcherConfigs is a HashMap and HashMap is an unordered collection.

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.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question