0
# Maps and Multimaps
1
2
Eclipse Collections provides advanced key-value collection types including traditional Maps with rich functional operations, Multimaps for one-to-many relationships, and BiMaps for bidirectional mapping. All types come in mutable and immutable variants with comprehensive functional programming support.
3
4
## Maps
5
6
### MapIterable<K,V>
7
The base interface for all map collections providing rich functional operations on key-value pairs.
8
9
```java { .api }
10
package org.eclipse.collections.api.map;
11
12
public interface MapIterable<K, V> extends RichIterable<V> {
13
// Basic map operations
14
V get(Object key);
15
V getIfAbsent(K key, Function0<? extends V> function);
16
V getIfAbsentPut(K key, Function0<? extends V> function);
17
V getIfAbsentWith(K key, Function<? super P, ? extends V> function, P parameter);
18
19
// Key-value iteration
20
void forEachKey(Procedure<? super K> procedure);
21
void forEachValue(Procedure<? super V> procedure);
22
void forEachKeyValue(Procedure2<? super K, ? super V> procedure);
23
24
// Views
25
RichIterable<K> keysView();
26
RichIterable<V> valuesView();
27
RichIterable<Pair<K, V>> keyValuesView();
28
29
// Functional transformations
30
<R> MapIterable<K, R> collectValues(Function2<? super K, ? super V, ? extends R> function);
31
<VV> MapIterable<VV, V> groupByUniqueKey(Function<? super V, ? extends VV> function);
32
33
// Filtering
34
MapIterable<K, V> select(Predicate2<? super K, ? super V> predicate);
35
MapIterable<K, V> reject(Predicate2<? super K, ? super V> predicate);
36
37
// Aggregation and reduction
38
<R> R injectIntoKeyValue(R injectedValue, Function3<? super R, ? super K, ? super V, ? extends R> function);
39
40
// Map transformations
41
MapIterable<V, K> flipUniqueValues();
42
43
// Conversion
44
MutableMap<K, V> toMap();
45
ImmutableMap<K, V> toImmutableMap();
46
}
47
```
48
49
### MutableMap<K,V>
50
Mutable map implementation extending Java's Map interface with Eclipse Collections enhancements.
51
52
```java { .api }
53
package org.eclipse.collections.api.map;
54
55
public interface MutableMap<K, V> extends Map<K, V>, MapIterable<K, V> {
56
// Enhanced put operations
57
V getIfAbsentPut(K key, Function0<? extends V> function);
58
V getIfAbsentPut(K key, V value);
59
V getIfAbsentPutWith(K key, Function<? super P, ? extends V> function, P parameter);
60
61
// Update operations
62
V updateValue(K key, Function0<? extends V> factory, Function<? super V, ? extends V> function);
63
V updateValueWith(K key, Function0<? extends V> factory, Function2<? super V, ? super P, ? extends V> function, P parameter);
64
65
// Modification operations
66
MutableMap<K, V> withKeyValue(K key, V value);
67
MutableMap<K, V> withAllKeyValues(Iterable<? extends Pair<? extends K, ? extends V>> keyValues);
68
MutableMap<K, V> withAllKeyValueArguments(Pair<? extends K, ? extends V>... keyValuePairs);
69
MutableMap<K, V> withoutKey(K key);
70
MutableMap<K, V> withoutAllKeys(Iterable<? extends K> keys);
71
72
// Functional transformations (in-place when possible)
73
<R> MutableMap<K, R> collectValues(Function2<? super K, ? super V, ? extends R> function);
74
MutableMap<K, V> select(Predicate2<? super K, ? super V> predicate);
75
MutableMap<K, V> reject(Predicate2<? super K, ? super V> predicate);
76
77
// Map inversion
78
MutableBiMap<V, K> flipUniqueValues();
79
80
// Immutable copy
81
ImmutableMap<K, V> toImmutable();
82
}
83
```
84
85
### ImmutableMap<K,V>
86
Thread-safe immutable map where all operations return new instances.
87
88
```java { .api }
89
package org.eclipse.collections.api.map;
90
91
public interface ImmutableMap<K, V> extends MapIterable<K, V> {
92
// Modification operations return new instances
93
ImmutableMap<K, V> newWithKeyValue(K key, V value);
94
ImmutableMap<K, V> newWithAllKeyValues(Iterable<? extends Pair<? extends K, ? extends V>> keyValues);
95
ImmutableMap<K, V> newWithAllKeyValueArguments(Pair<? extends K, ? extends V>... keyValuePairs);
96
ImmutableMap<K, V> newWithoutKey(K key);
97
ImmutableMap<K, V> newWithoutAllKeys(Iterable<? extends K> keys);
98
99
// Functional transformations return new instances
100
<R> ImmutableMap<K, R> collectValues(Function2<? super K, ? super V, ? extends R> function);
101
ImmutableMap<K, V> select(Predicate2<? super K, ? super V> predicate);
102
ImmutableMap<K, V> reject(Predicate2<? super K, ? super V> predicate);
103
104
// Map inversion returns new instance
105
ImmutableBiMap<V, K> flipUniqueValues();
106
}
107
```
108
109
## Ordered Maps
110
111
Maps that maintain insertion order or iteration order.
112
113
### OrderedMap<K,V>
114
Base interface for maps with defined iteration order.
115
116
```java { .api }
117
package org.eclipse.collections.api.map;
118
119
public interface OrderedMap<K, V> extends MapIterable<K, V> {
120
// Ordered access
121
Pair<K, V> getKeyValueAtIndex(int index);
122
123
// Ordered views
124
OrderedMap<K, V> select(Predicate2<? super K, ? super V> predicate);
125
OrderedMap<K, V> reject(Predicate2<? super K, ? super V> predicate);
126
<R> OrderedMap<K, R> collectValues(Function2<? super K, ? super V, ? extends R> function);
127
}
128
```
129
130
### MutableOrderedMap<K,V> / ImmutableOrderedMap<K,V>
131
Mutable and immutable variants of ordered maps.
132
133
```java { .api }
134
package org.eclipse.collections.api.map;
135
136
public interface MutableOrderedMap<K, V> extends MutableMap<K, V>, OrderedMap<K, V> {
137
// Ordered modification operations
138
MutableOrderedMap<K, V> withKeyValue(K key, V value);
139
MutableOrderedMap<K, V> withoutKey(K key);
140
141
// Ordered transformations
142
MutableOrderedMap<K, V> select(Predicate2<? super K, ? super V> predicate);
143
MutableOrderedMap<K, V> reject(Predicate2<? super K, ? super V> predicate);
144
<R> MutableOrderedMap<K, R> collectValues(Function2<? super K, ? super V, ? extends R> function);
145
146
ImmutableOrderedMap<K, V> toImmutable();
147
}
148
149
public interface ImmutableOrderedMap<K, V> extends ImmutableMap<K, V>, OrderedMap<K, V> {
150
// Ordered modification operations return new instances
151
ImmutableOrderedMap<K, V> newWithKeyValue(K key, V value);
152
ImmutableOrderedMap<K, V> newWithoutKey(K key);
153
154
// Ordered transformations return new instances
155
ImmutableOrderedMap<K, V> select(Predicate2<? super K, ? super V> predicate);
156
ImmutableOrderedMap<K, V> reject(Predicate2<? super K, ? super V> predicate);
157
<R> ImmutableOrderedMap<K, R> collectValues(Function2<? super K, ? super V, ? extends R> function);
158
}
159
```
160
161
## Sorted Maps
162
163
Maps with keys sorted according to a comparator.
164
165
### SortedMapIterable<K,V>
166
Base interface for maps with sorted keys.
167
168
```java { .api }
169
package org.eclipse.collections.api.map.sorted;
170
171
public interface SortedMapIterable<K, V> extends OrderedMap<K, V>, SortedIterable<K> {
172
// Comparator access
173
Comparator<? super K> comparator();
174
175
// Sorted range operations
176
SortedMapIterable<K, V> headMap(K toKey);
177
SortedMapIterable<K, V> tailMap(K fromKey);
178
SortedMapIterable<K, V> subMap(K fromKey, K toKey);
179
180
// Sorted transformations
181
SortedMapIterable<K, V> select(Predicate2<? super K, ? super V> predicate);
182
SortedMapIterable<K, V> reject(Predicate2<? super K, ? super V> predicate);
183
<R> MapIterable<K, R> collectValues(Function2<? super K, ? super V, ? extends R> function);
184
}
185
```
186
187
### MutableSortedMap<K,V> / ImmutableSortedMap<K,V>
188
Mutable and immutable variants of sorted maps.
189
190
```java { .api }
191
package org.eclipse.collections.api.map.sorted;
192
193
public interface MutableSortedMap<K, V> extends MutableOrderedMap<K, V>, SortedMapIterable<K, V> {
194
// Sorted modification operations
195
MutableSortedMap<K, V> withKeyValue(K key, V value);
196
MutableSortedMap<K, V> withoutKey(K key);
197
198
// Sorted range operations
199
MutableSortedMap<K, V> headMap(K toKey);
200
MutableSortedMap<K, V> tailMap(K fromKey);
201
MutableSortedMap<K, V> subMap(K fromKey, K toKey);
202
203
ImmutableSortedMap<K, V> toImmutable();
204
}
205
206
public interface ImmutableSortedMap<K, V> extends ImmutableOrderedMap<K, V>, SortedMapIterable<K, V> {
207
// Sorted modification operations return new instances
208
ImmutableSortedMap<K, V> newWithKeyValue(K key, V value);
209
ImmutableSortedMap<K, V> newWithoutKey(K key);
210
211
// Sorted range operations return new instances
212
ImmutableSortedMap<K, V> headMap(K toKey);
213
ImmutableSortedMap<K, V> tailMap(K fromKey);
214
ImmutableSortedMap<K, V> subMap(K fromKey, K toKey);
215
}
216
```
217
218
## Multimaps
219
220
Collections that allow multiple values per key, providing one-to-many key-value associations.
221
222
### Multimap<K,V>
223
Base interface for multimap collections supporting multiple values per key.
224
225
```java { .api }
226
package org.eclipse.collections.api.multimap;
227
228
public interface Multimap<K, V> extends RichIterable<V> {
229
// Basic multimap operations
230
RichIterable<V> get(K key);
231
boolean containsKey(Object key);
232
boolean containsValue(Object value);
233
boolean containsKeyAndValue(Object key, Object value);
234
235
// Size operations
236
int size();
237
int sizeDistinct();
238
boolean isEmpty();
239
boolean notEmpty();
240
241
// Key-value iteration
242
void forEachKey(Procedure<? super K> procedure);
243
void forEachValue(Procedure<? super V> procedure);
244
void forEachKeyValue(Procedure2<? super K, ? super V> procedure);
245
void forEachKeyMultiValues(Procedure2<? super K, ? super RichIterable<V>> procedure);
246
247
// Views
248
RichIterable<K> keysView();
249
RichIterable<V> valuesView();
250
RichIterable<Pair<K, V>> keyValuePairsView();
251
RichIterable<RichIterable<V>> multiValuesView();
252
253
// Conversion
254
MapIterable<K, RichIterable<V>> toMap();
255
MutableMultimap<K, V> toMutable();
256
ImmutableMultimap<K, V> toImmutable();
257
}
258
```
259
260
### MutableMultimap<K,V>
261
Mutable multimap allowing modification of key-value associations.
262
263
```java { .api }
264
package org.eclipse.collections.api.multimap;
265
266
public interface MutableMultimap<K, V> extends Multimap<K, V> {
267
// Modification operations
268
boolean put(K key, V value);
269
boolean remove(Object key, Object value);
270
boolean removeAll(Object key);
271
void clear();
272
273
// Bulk operations
274
boolean putAll(K key, Iterable<? extends V> values);
275
<KK extends K, VV extends V> boolean putAll(Multimap<KK, VV> multimap);
276
boolean putAllPairs(Pair<K, V>... pairs);
277
278
// Replacement operations
279
RichIterable<V> replaceValues(K key, Iterable<? extends V> values);
280
281
// Immutable copy
282
ImmutableMultimap<K, V> toImmutable();
283
}
284
```
285
286
### ImmutableMultimap<K,V>
287
Thread-safe immutable multimap where all operations return new instances.
288
289
```java { .api }
290
package org.eclipse.collections.api.multimap;
291
292
public interface ImmutableMultimap<K, V> extends Multimap<K, V> {
293
// Modification operations return new instances
294
ImmutableMultimap<K, V> newWith(K key, V value);
295
ImmutableMultimap<K, V> newWithout(Object key, Object value);
296
ImmutableMultimap<K, V> newWithoutAll(Object key);
297
ImmutableMultimap<K, V> newWithAll(K key, Iterable<? extends V> values);
298
}
299
```
300
301
## Specialized Multimaps
302
303
Multimaps with specific value collection types.
304
305
### ListMultimap<K,V>
306
Multimap where values for each key are stored in a List (ordered, allows duplicates).
307
308
```java { .api }
309
package org.eclipse.collections.api.multimap.list;
310
311
public interface ListMultimap<K, V> extends Multimap<K, V> {
312
ListIterable<V> get(K key);
313
314
MutableListMultimap<K, V> toMutable();
315
ImmutableListMultimap<K, V> toImmutable();
316
}
317
318
public interface MutableListMultimap<K, V> extends MutableMultimap<K, V>, ListMultimap<K, V> {
319
MutableList<V> get(K key);
320
MutableList<V> replaceValues(K key, Iterable<? extends V> values);
321
322
ImmutableListMultimap<K, V> toImmutable();
323
}
324
325
public interface ImmutableListMultimap<K, V> extends ImmutableMultimap<K, V>, ListMultimap<K, V> {
326
ImmutableList<V> get(K key);
327
328
ImmutableListMultimap<K, V> newWith(K key, V value);
329
ImmutableListMultimap<K, V> newWithout(Object key, Object value);
330
ImmutableListMultimap<K, V> newWithoutAll(Object key);
331
}
332
```
333
334
### SetMultimap<K,V>
335
Multimap where values for each key are stored in a Set (unique values).
336
337
```java { .api }
338
package org.eclipse.collections.api.multimap.set;
339
340
public interface SetMultimap<K, V> extends Multimap<K, V> {
341
SetIterable<V> get(K key);
342
343
MutableSetMultimap<K, V> toMutable();
344
ImmutableSetMultimap<K, V> toImmutable();
345
}
346
347
public interface MutableSetMultimap<K, V> extends MutableMultimap<K, V>, SetMultimap<K, V> {
348
MutableSet<V> get(K key);
349
MutableSet<V> replaceValues(K key, Iterable<? extends V> values);
350
351
ImmutableSetMultimap<K, V> toImmutable();
352
}
353
354
public interface ImmutableSetMultimap<K, V> extends ImmutableMultimap<K, V>, SetMultimap<K, V> {
355
ImmutableSet<V> get(K key);
356
357
ImmutableSetMultimap<K, V> newWith(K key, V value);
358
ImmutableSetMultimap<K, V> newWithout(Object key, Object value);
359
ImmutableSetMultimap<K, V> newWithoutAll(Object key);
360
}
361
```
362
363
### BagMultimap<K,V>
364
Multimap where values for each key are stored in a Bag (allows duplicates with occurrence counting).
365
366
```java { .api }
367
package org.eclipse.collections.api.multimap.bag;
368
369
public interface BagMultimap<K, V> extends Multimap<K, V> {
370
Bag<V> get(K key);
371
372
MutableBagMultimap<K, V> toMutable();
373
ImmutableBagMultimap<K, V> toImmutable();
374
}
375
376
public interface MutableBagMultimap<K, V> extends MutableMultimap<K, V>, BagMultimap<K, V> {
377
MutableBag<V> get(K key);
378
MutableBag<V> replaceValues(K key, Iterable<? extends V> values);
379
380
ImmutableBagMultimap<K, V> toImmutable();
381
}
382
383
public interface ImmutableBagMultimap<K, V> extends ImmutableMultimap<K, V>, BagMultimap<K, V> {
384
ImmutableBag<V> get(K key);
385
386
ImmutableBagMultimap<K, V> newWith(K key, V value);
387
ImmutableBagMultimap<K, V> newWithout(Object key, Object value);
388
ImmutableBagMultimap<K, V> newWithoutAll(Object key);
389
}
390
```
391
392
## BiMaps
393
394
Bidirectional maps providing one-to-one key-value mapping where both directions can be queried efficiently.
395
396
### BiMap<K,V>
397
Base interface for bidirectional maps with one-to-one key-value mapping.
398
399
```java { .api }
400
package org.eclipse.collections.api.bimap;
401
402
public interface BiMap<K, V> extends MapIterable<K, V> {
403
// Bidirectional operations
404
BiMap<V, K> inverse();
405
V forcePut(K key, V value); // Removes existing mapping for value if present
406
407
// BiMap-specific views
408
BiMap<K, V> select(Predicate2<? super K, ? super V> predicate);
409
BiMap<K, V> reject(Predicate2<? super K, ? super V> predicate);
410
<R> BiMap<K, R> collectValues(Function2<? super K, ? super V, ? extends R> function);
411
412
// Conversion
413
MutableBiMap<K, V> toMutable();
414
ImmutableBiMap<K, V> toImmutable();
415
}
416
```
417
418
### MutableBiMap<K,V>
419
Mutable bidirectional map with modification operations.
420
421
```java { .api }
422
package org.eclipse.collections.api.bimap;
423
424
public interface MutableBiMap<K, V> extends BiMap<K, V>, MutableMap<K, V> {
425
// Bidirectional operations
426
MutableBiMap<V, K> inverse();
427
V forcePut(K key, V value);
428
429
// Modification operations
430
MutableBiMap<K, V> withKeyValue(K key, V value);
431
MutableBiMap<K, V> withoutKey(K key);
432
MutableBiMap<K, V> withAllKeyValues(Iterable<? extends Pair<? extends K, ? extends V>> keyValues);
433
MutableBiMap<K, V> withoutAllKeys(Iterable<? extends K> keys);
434
435
// BiMap-specific transformations
436
MutableBiMap<K, V> select(Predicate2<? super K, ? super V> predicate);
437
MutableBiMap<K, V> reject(Predicate2<? super K, ? super V> predicate);
438
<R> MutableBiMap<K, R> collectValues(Function2<? super K, ? super V, ? extends R> function);
439
440
// Immutable copy
441
ImmutableBiMap<K, V> toImmutable();
442
}
443
```
444
445
### ImmutableBiMap<K,V>
446
Thread-safe immutable bidirectional map where all operations return new instances.
447
448
```java { .api }
449
package org.eclipse.collections.api.bimap;
450
451
public interface ImmutableBiMap<K, V> extends BiMap<K, V>, ImmutableMap<K, V> {
452
// Bidirectional operations return new instances
453
ImmutableBiMap<V, K> inverse();
454
ImmutableBiMap<K, V> newWithKeyValue(K key, V value);
455
456
// Modification operations return new instances
457
ImmutableBiMap<K, V> newWithoutKey(K key);
458
ImmutableBiMap<K, V> newWithAllKeyValues(Iterable<? extends Pair<? extends K, ? extends V>> keyValues);
459
ImmutableBiMap<K, V> newWithoutAllKeys(Iterable<? extends K> keys);
460
461
// BiMap-specific transformations return new instances
462
ImmutableBiMap<K, V> select(Predicate2<? super K, ? super V> predicate);
463
ImmutableBiMap<K, V> reject(Predicate2<? super K, ? super V> predicate);
464
<R> ImmutableBiMap<K, R> collectValues(Function2<? super K, ? super V, ? extends R> function);
465
}
466
```
467
468
## Usage Examples
469
470
### Working with Maps
471
```java { .api }
472
// Create and manipulate maps
473
MutableMap<String, Integer> ages = Maps.mutable.with("Alice", 30, "Bob", 25);
474
475
// Enhanced map operations
476
Integer age = ages.getIfAbsent("Charlie", () -> 0);
477
ages.getIfAbsentPut("David", () -> 28);
478
479
// Functional operations
480
MutableMap<String, String> categories = ages.collectValues((name, age) -> age >= 30 ? "Senior" : "Junior");
481
MutableMap<String, Integer> filtered = ages.select((name, age) -> age > 25);
482
483
// Key-value iteration
484
ages.forEachKeyValue((name, age) -> System.out.println(name + " is " + age + " years old"));
485
```
486
487
### Working with Multimaps
488
```java { .api }
489
// Create multimap
490
MutableListMultimap<String, String> phoneBook = Multimaps.mutable.list.empty();
491
492
// Add multiple values per key
493
phoneBook.put("Alice", "555-1234");
494
phoneBook.put("Alice", "555-5678");
495
phoneBook.put("Bob", "555-9999");
496
497
// Access values
498
ListIterable<String> aliceNumbers = phoneBook.get("Alice"); // ["555-1234", "555-5678"]
499
500
// Check containment
501
boolean hasValue = phoneBook.containsKeyAndValue("Alice", "555-1234"); // true
502
```
503
504
### Working with BiMaps
505
```java { .api }
506
// Create bidirectional map
507
MutableBiMap<String, Integer> studentIds = BiMaps.mutable.empty();
508
studentIds.put("Alice", 101);
509
studentIds.put("Bob", 102);
510
511
// Bidirectional lookup
512
BiMap<Integer, String> inverse = studentIds.inverse();
513
String student = inverse.get(101); // "Alice"
514
515
// Force put (removes existing mapping for value)
516
studentIds.forcePut("Charlie", 101); // Removes Alice -> 101, adds Charlie -> 101
517
```
518
519
### Advanced Map Operations
520
```java { .api }
521
// Map transformations
522
MutableMap<String, List<String>> groups = people.groupBy(Person::getDepartment);
523
MapIterable<String, Integer> departmentSizes = groups.collectValues((dept, people) -> people.size());
524
525
// Map aggregation
526
int totalAge = ages.injectIntoKeyValue(0, (sum, name, age) -> sum + age);
527
528
// Map flipping
529
MutableBiMap<Integer, String> ageToName = ages.flipUniqueValues();
530
```
531
532
## Sorted Maps
533
534
Eclipse Collections provides sorted variants of maps that maintain key-value pairs in sorted order by their keys.
535
536
### SortedMapIterable<K,V>
537
Base interface for sorted maps extending MapIterable with sorted key access.
538
539
```java { .api }
540
package org.eclipse.collections.api.map.sorted;
541
542
public interface SortedMapIterable<K, V> extends MapIterable<K, V> {
543
// Sorted-specific access
544
K firstKey();
545
K lastKey();
546
547
// Range operations
548
SortedMapIterable<K, V> takeWhile(Predicate2<? super K, ? super V> predicate);
549
SortedMapIterable<K, V> dropWhile(Predicate2<? super K, ? super V> predicate);
550
551
// Submap operations
552
SortedMapIterable<K, V> subMap(K fromKey, K toKey);
553
SortedMapIterable<K, V> headMap(K toKey);
554
SortedMapIterable<K, V> tailMap(K fromKey);
555
556
// Views with sorting
557
RichIterable<K> keysView();
558
RichIterable<V> valuesView();
559
560
// Functional operations returning sorted maps
561
<R> SortedMapIterable<K, R> collectValues(Function2<? super K, ? super V, ? extends R> function);
562
SortedMapIterable<K, V> select(Predicate2<? super K, ? super V> predicate);
563
SortedMapIterable<K, V> reject(Predicate2<? super K, ? super V> predicate);
564
565
// Conversion
566
MutableSortedMap<K, V> toSortedMap();
567
ImmutableSortedMap<K, V> toImmutableSortedMap();
568
}
569
```
570
571
### MutableSortedMap<K,V>
572
Mutable sorted map implementation extending Java's SortedMap interface.
573
574
```java { .api }
575
package org.eclipse.collections.api.map.sorted;
576
577
public interface MutableSortedMap<K, V> extends MutableMap<K, V>, SortedMap<K, V>, SortedMapIterable<K, V> {
578
// Modification operations
579
MutableSortedMap<K, V> withKeyValue(K key, V value);
580
MutableSortedMap<K, V> withoutKey(K key);
581
MutableSortedMap<K, V> withAllKeyValues(Iterable<? extends Pair<? extends K, ? extends V>> keyValues);
582
MutableSortedMap<K, V> withoutAllKeys(Iterable<? extends K> keys);
583
584
// Functional operations
585
<R> MutableSortedMap<K, R> collectValues(Function2<? super K, ? super V, ? extends R> function);
586
MutableSortedMap<K, V> select(Predicate2<? super K, ? super V> predicate);
587
MutableSortedMap<K, V> reject(Predicate2<? super K, ? super V> predicate);
588
589
// Range operations
590
MutableSortedMap<K, V> subMap(K fromKey, K toKey);
591
MutableSortedMap<K, V> headMap(K toKey);
592
MutableSortedMap<K, V> tailMap(K fromKey);
593
594
// Immutable copy
595
ImmutableSortedMap<K, V> toImmutable();
596
}
597
```
598
599
### ImmutableSortedMap<K,V>
600
Thread-safe immutable sorted map where all operations return new instances.
601
602
```java { .api }
603
package org.eclipse.collections.api.map.sorted;
604
605
public interface ImmutableSortedMap<K, V> extends SortedMapIterable<K, V> {
606
// Modification operations return new instances
607
ImmutableSortedMap<K, V> newWithKeyValue(K key, V value);
608
ImmutableSortedMap<K, V> newWithoutKey(K key);
609
ImmutableSortedMap<K, V> newWithAllKeyValues(Iterable<? extends Pair<? extends K, ? extends V>> keyValues);
610
ImmutableSortedMap<K, V> newWithoutAllKeys(Iterable<? extends K> keys);
611
612
// Functional operations return new instances
613
<R> ImmutableSortedMap<K, R> collectValues(Function2<? super K, ? super V, ? extends R> function);
614
ImmutableSortedMap<K, V> select(Predicate2<? super K, ? super V> predicate);
615
ImmutableSortedMap<K, V> reject(Predicate2<? super K, ? super V> predicate);
616
617
// Range operations return new instances
618
ImmutableSortedMap<K, V> subMap(K fromKey, K toKey);
619
ImmutableSortedMap<K, V> headMap(K toKey);
620
ImmutableSortedMap<K, V> tailMap(K fromKey);
621
}
622
```
623
624
### Working with Sorted Maps
625
626
```java { .api }
627
// Create sorted map
628
MutableSortedMap<String, Integer> scores = SortedMaps.mutable.with("zebra", 100, "apple", 85, "banana", 92);
629
// Keys are automatically sorted: {"apple": 85, "banana": 92, "zebra": 100}
630
631
// Access sorted keys
632
String firstKey = scores.firstKey(); // "apple"
633
String lastKey = scores.lastKey(); // "zebra"
634
635
// Range operations
636
SortedMapIterable<String, Integer> subMap = scores.subMap("apple", "zebra"); // {"apple": 85, "banana": 92}
637
SortedMapIterable<String, Integer> headMap = scores.headMap("banana"); // {"apple": 85}
638
639
// Functional operations preserve sorting
640
MutableSortedMap<String, String> grades = scores.collectValues((name, score) ->
641
score >= 90 ? "A" : "B");
642
// Result: {"apple": "B", "banana": "A", "zebra": "A"}
643
```
644
645
This comprehensive map and multimap API provides powerful abstractions for key-value data structures with extensive functional programming capabilities, efficient bidirectional access, one-to-many relationships, and sorted key ordering.