Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LinkedHashMap entrySet's order not being preserved in a stream (Android)

I'm creating a very simple form validation utility for a sign up screen, and I'm running into some unexpected behavior concerning LinkedHashMap and a stream created from its entrySet.

I'm storing validation results in a LinkedHashMap, with the following ordering of statements:

Map<ValidationResult.SignUpField, Boolean> fieldStatuses = new LinkedHashMap<>();

fieldStatuses.put(EMAIL, isValidEmail(emailAddress));
fieldStatuses.put(USERNAME, isValidUsername(username));
fieldStatuses.put(BIRTHDAY, isValidBirthday(birthday));
fieldStatuses.put(PASSWORD, isValidPassword(password));
fieldStatuses.put(CONFIRM_PASSWORD, password.equals(confirmedPassword));

List<ValidationEntry> invalidFields = aggregateInvalidFields(fieldStatuses);

One particular iteration yields all of the above fields invalid except for "confirm password". Using a simple for loop over the entry set and omitting valid results, the invalid results appear in the following order:

  • Email
  • Username
  • Birthday
  • Password

I then attempted to make use of the subset of Stream API's available on Android (targeting version 25 with a min of 19, hence the lack of Collectors.toMap()):

private static List<ValidationEntry> aggregateInvalidFields(Map<ValidationResult.SignUpField, Boolean> fields) {
    List<ValidationEntry> invalidFields = new ArrayList<>();
    fields.entrySet()
            .stream()
            .filter(entry -> !entry.getValue())
            .forEachOrdered(entry -> {
                ValidationResult.SignUpField field = entry.getKey();
                invalidFields.add(new ValidationEntry(field, lookUpErrorCode(field)));
            });
    return invalidFields;
}

But that code yields the following order:

  • Birthday
  • Password
  • Username
  • Email

What exactly is happening here, why is the result of the stream not honoring the insertion order of the LinkedHashMap? Note that if I swap out forEachOrdered with forEach, it's still not insertion ordered.

like image 744
Chris Avatar asked Mar 10 '17 06:03

Chris


People also ask

Does LinkedHashMap maintain order?

The LinkedHashMap Class is just like HashMap with an additional feature of maintaining an order of elements inserted into it.

How does LinkedHashMap maintain access order?

This class extends HashMap and maintains a linked list of the entries in the map, in the order in which they were inserted. This allows insertion-order iteration over the map. That is, when iterating a LinkedHashMap, the elements will be returned in the order in which they were inserted.

Can LinkedHashMap be sorted?

LinkedHashMap maintains insertion order. Convert LinkedHashMap into TreeMap and after that print keys of TreeMap which are sorted in nature.

Is LinkedHashMap values ordered?

LinkedHashMap is a predefined class in Java that is similar to HashMap, contains key and its respective value, unlike HashMap. In LinkedHashMap insertion order is preserved.


1 Answers

This behavior is a known bug in Android's 7.0 / 7.1 implementation of LinkedHashMap.

The LinkedHashMap's collection views' spliterators for entrySet, values and keySet correctly report that they are ORDERED but actually they aren't because the spliterator implementation of the parent class (HashMap) is used internally.

This behavior has already been documented in the Javadoc and workarounds are also proposed there.

The fix has been commited on 2016-08-16 and will appear in the next Android release.

For clarification: Google became aware of this bug at first in 2017-01, so the "fix" mentioned above was an accidental fix. If they had known about this problem earlier, the resolution would have been included in 7.1

like image 121
Stefan Zobel Avatar answered Oct 04 '22 20:10

Stefan Zobel