Uploaded image for project: 'Java Driver'
  1. Java Driver
  2. JAVA-65

BasicDBObject.entrySet().iterator does not produce values in the same order as BasicDBObject.keySet()

    • Type: Icon: Bug Bug
    • Resolution: Done
    • Priority: Icon: Minor - P4 Minor - P4
    • 1.2
    • Affects Version/s: 1.1
    • Component/s: None
    • None
    • Environment:
      All

      BasicDBObject preserves insertion order of keys - and really needs to - so keySet() is returned in insertion order, BUT it does not provide this functionality for entrySet() - which I generally use a lot more often than keySet() when iterating through maps.

      Digging into the code I see BasicDBObject is a HashMap combined with an OrderedSet was used to maintain insertion key order, but entrySet is not overridden.

      I'm guessing the original author was unaware of java.util.LinkedHashMap. I changed BasicDBObject to extend LinkedHashMap, and removed _keys. All unit tests pass. I'm seeing pretty significant speed improvements in some benchmarks I put together to compare implementations. And the code is a little simpler too.

      I have attached a modified copy of BasicDBObject for consideration, as well as a copy of DBObjectTest that includes a new test - testEntrySetOrder that asserts that the entrySet and keySet are both maintained in insertion order.

        1. BasicDBObject.java
          6 kB
        2. DBObjectTest.java
          5 kB

            Assignee:
            eliot Eliot Horowitz (Inactive)
            Reporter:
            keithbranton Keith Branton
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved: