Class CompactMap.Builder<K,V>
- Type Parameters:
K
- the type of keys maintained by the mapV
- the type of mapped values
- Enclosing class:
CompactMap<K,
V>
Simple example with common options:
CompactMap<String, Object> map = CompactMap.<String, Object>builder()
.caseSensitive(false)
.sortedOrder()
.build();
Note the type witness (<String, Object>
) in the example above. This explicit type
information is required when method chaining directly from builder() due to Java's type
inference limitations. Alternatively, you can avoid the type witness by splitting the
builder creation and configuration:
// Using type witness
CompactMap<String, Object> map1 = CompactMap.<String, Object>builder()
.sortedOrder()
.build();
// Without type witness
Builder<String, Object> builder = CompactMap.builder();
CompactMap<String, Object> map2 = builder
.sortedOrder()
.build();
Comprehensive example with all options:
CompactMap<String, Object> map = CompactMap.<String, Object>builder()
.caseSensitive(false) // Enable case-insensitive key comparison
.compactSize(80) // Set threshold for switching to backing map
.mapType(LinkedHashMap.class) // Specify backing map implementation
.singleValueKey("uuid") // Optimize storage for single entry with this key
.sourceMap(existingMap) // Initialize with entries from another map
.insertionOrder() // Or: .reverseOrder(), .sortedOrder(), .noOrder()
.build();
Method | Description | Default |
---|---|---|
caseSensitive(boolean) |
Controls case sensitivity for string keys | true |
compactSize(int) |
Maximum size before switching to backing map | 70 |
mapType(Class) |
Type of backing map when size exceeds compact size | HashMap.class |
singleValueKey(Object) |
Special key that enables optimized storage when map contains only one entry with this key | "id" |
sourceMap(Map) |
Initializes the CompactMap with entries from the provided map | null |
sortedOrder() |
Maintains keys in sorted order | unordered |
reverseOrder() |
Maintains keys in reverse order | unordered |
insertionOrder() |
Maintains keys in insertion order | unordered |
noOrder() |
Explicitly sets unordered behavior | unordered |
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptionCompactMap
<K, V> build()
Creates a new CompactMap instance with the configured options.caseSensitive
(boolean caseSensitive) Sets whether String keys should be compared case-sensitively.compactSize
(int size) Sets the maximum size for compact array storage.Configures the map to maintain keys in insertion order.Sets the type of Map to use when size exceeds compact storage threshold.noOrder()
Explicitly configures the map to not maintain any specific ordering.Configures the map to maintain keys in reverse sorted order.singleValueKey
(K key) Sets a special key for optimized single-entry storage.Configures the map to maintain keys in natural sorted order.Initializes the map with entries from the specified source map.
-
Method Details
-
caseSensitive
Sets whether String keys should be compared case-sensitively.When set to false, String keys will be compared ignoring case. For example, "Key", "key", and "KEY" would all be considered equal. This setting only affects String keys; other key types are compared normally. Maps can contain heterogeneous content (Strings, Numbers, null, as keys).
- Parameters:
caseSensitive
- true for case-sensitive comparison (default), false for case-insensitive comparison- Returns:
- this builder instance for method chaining
-
mapType
Sets the type of Map to use when size exceeds compact storage threshold.Common map types include:
HashMap
- Default, unordered storageTreeMap
- Sorted key orderLinkedHashMap
- Insertion order
IdentityHashMap
andWeakHashMap
are not supported.- Parameters:
mapType
- the Class object representing the desired Map implementation- Returns:
- this builder instance for method chaining
- Throws:
IllegalArgumentException
- if mapType is not a Map class
-
singleValueKey
Sets a special key for optimized single-entry storage.When the map contains exactly one entry with this key, the value is stored directly without wrapper objects, reducing memory overhead. The default single value key is "id".
- Parameters:
key
- the key to use for optimized single-entry storage- Returns:
- this builder instance for method chaining
-
compactSize
Sets the maximum size for compact array storage.When the map size is between 2 and this value, entries are stored in a compact array format. Above this size, entries are moved to a backing map. Must be greater than or equal to 2.
- Parameters:
size
- the maximum number of entries to store in compact format- Returns:
- this builder instance for method chaining
-
sortedOrder
Configures the map to maintain keys in natural sorted order.Keys must be
Comparable
or aClassCastException
will be thrown when incomparable keys are inserted. For String keys, the ordering respects the case sensitivity setting.- Returns:
- this builder instance for method chaining
-
reverseOrder
Configures the map to maintain keys in reverse sorted order.Keys must be
Comparable
or aClassCastException
will be thrown when incomparable keys are inserted. For String keys, the ordering respects the case sensitivity setting.- Returns:
- this builder instance for method chaining
-
insertionOrder
Configures the map to maintain keys in insertion order.The iteration order will match the order in which entries were added to the map. This ordering is preserved even when entries are updated.
- Returns:
- this builder instance for method chaining
-
noOrder
Explicitly configures the map to not maintain any specific ordering.This is the default behavior if no ordering is specified. The iteration order may change as entries are added or removed.
- Returns:
- this builder instance for method chaining
-
sourceMap
Initializes the map with entries from the specified source map.- Parameters:
source
- the map whose entries are to be copied- Returns:
- this builder instance for method chaining
- Throws:
IllegalArgumentException
- if source map's ordering conflicts with configured ordering
-
build
Creates a new CompactMap instance with the configured options.This method validates all options and creates a specialized implementation based on the configuration. The resulting map is optimized for the specified combination of options.
- Returns:
- a new CompactMap instance
- Throws:
IllegalArgumentException
- if any configuration options are invalid or incompatible
-