49
49
public class BsonDocument extends BsonValue implements Map <String , BsonValue >, Cloneable , Bson , Serializable {
50
50
private static final long serialVersionUID = 1L ;
51
51
52
- private final Map <String , BsonValue > map = new LinkedHashMap < String , BsonValue >() ;
52
+ private final Map <String , BsonValue > map ;
53
53
54
54
/**
55
55
* Parses a string in MongoDB Extended JSON format to a {@code BsonDocument}
@@ -69,6 +69,7 @@ public static BsonDocument parse(final String json) {
69
69
* @param bsonElements a list of {@code BsonElement}
70
70
*/
71
71
public BsonDocument (final List <BsonElement > bsonElements ) {
72
+ this (bsonElements .size ());
72
73
for (BsonElement cur : bsonElements ) {
73
74
put (cur .getName (), cur .getValue ());
74
75
}
@@ -81,13 +82,26 @@ public BsonDocument(final List<BsonElement> bsonElements) {
81
82
* @param value the value
82
83
*/
83
84
public BsonDocument (final String key , final BsonValue value ) {
85
+ this ();
84
86
put (key , value );
85
87
}
86
88
89
+ /**
90
+ * Construct an empty document with the specified initial capacity.
91
+ *
92
+ * @param initialCapacity the initial capacity
93
+ * @throws IllegalArgumentException if the initial capacity is negative
94
+ * @since 4.3
95
+ */
96
+ public BsonDocument (final int initialCapacity ) {
97
+ map = new LinkedHashMap <String , BsonValue >(initialCapacity );
98
+ }
99
+
87
100
/**
88
101
* Construct an empty document.
89
102
*/
90
103
public BsonDocument () {
104
+ map = new LinkedHashMap <String , BsonValue >();
91
105
}
92
106
93
107
@ Override
@@ -844,7 +858,7 @@ public String toString() {
844
858
845
859
@ Override
846
860
public BsonDocument clone () {
847
- BsonDocument to = new BsonDocument ();
861
+ BsonDocument to = new BsonDocument (this . size () );
848
862
for (Entry <String , BsonValue > cur : entrySet ()) {
849
863
switch (cur .getValue ().getBsonType ()) {
850
864
case DOCUMENT :
0 commit comments