0
# Core Collections
1
2
Eclipse Collections provides four primary collection types: Lists (ordered, indexed), Sets (unique elements), Bags (multisets with occurrence counting), and Stacks (LIFO collections). Each type comes in mutable and immutable variants with comprehensive functional programming support.
3
4
```java { .api }
5
// Required imports for types used in this documentation
6
import java.util.Random;
7
import java.util.function.Supplier;
8
import org.eclipse.collections.api.block.predicate.primitive.IntPredicate;
9
```
10
11
## Lists
12
13
### ListIterable<T>
14
The base interface for all list collections providing ordered, indexed access.
15
16
```java { .api }
17
package org.eclipse.collections.api.list;
18
19
public interface ListIterable<T> extends RichIterable<T> {
20
// Indexed access
21
T get(int index);
22
T getFirst();
23
T getLast();
24
int indexOf(Object object);
25
int lastIndexOf(Object object);
26
27
// Sublist operations
28
ListIterable<T> subList(int fromIndex, int toIndex);
29
ListIterable<T> take(int count);
30
ListIterable<T> drop(int count);
31
32
// List-specific transformations
33
ListIterable<T> toReversed();
34
ListIterable<T> distinct();
35
36
// Functional operations returning Lists
37
<V> ListIterable<V> collect(Function<? super T, ? extends V> function);
38
ListIterable<T> select(Predicate<? super T> predicate);
39
ListIterable<T> reject(Predicate<? super T> predicate);
40
41
// Zipping operations
42
<S> ListIterable<Pair<T, S>> zip(Iterable<S> that);
43
ListIterable<Pair<T, Integer>> zipWithIndex();
44
45
// Bi-directional iteration
46
void forEachInBoth(ListIterable<T> other, Procedure2<? super T, ? super T> procedure);
47
48
// Conversion
49
MutableList<T> toList();
50
ImmutableList<T> toImmutableList();
51
}
52
```
53
54
### MutableList<T>
55
Mutable list implementation extending Java's List interface with Eclipse Collections enhancements.
56
57
```java { .api }
58
package org.eclipse.collections.api.list;
59
60
public interface MutableList<T> extends MutableCollection<T>, List<T>, ListIterable<T> {
61
// Modification operations
62
MutableList<T> with(T element);
63
MutableList<T> without(T element);
64
MutableList<T> withAll(Iterable<? extends T> elements);
65
MutableList<T> withoutAll(Iterable<? extends T> elements);
66
67
// List-specific mutations
68
MutableList<T> reverseThis();
69
MutableList<T> sortThis();
70
MutableList<T> sortThis(Comparator<? super T> comparator);
71
MutableList<T> shuffleThis();
72
MutableList<T> shuffleThis(Random rnd);
73
74
// Functional transformations (in-place)
75
<V> MutableList<V> collect(Function<? super T, ? extends V> function);
76
MutableList<T> select(Predicate<? super T> predicate);
77
MutableList<T> reject(Predicate<? super T> predicate);
78
79
// Sublist operations
80
MutableList<T> subList(int fromIndex, int toIndex);
81
MutableList<T> take(int count);
82
MutableList<T> drop(int count);
83
84
// Immutable copy
85
ImmutableList<T> toImmutable();
86
}
87
```
88
89
### ImmutableList<T>
90
Thread-safe immutable list where all operations return new instances.
91
92
```java { .api }
93
package org.eclipse.collections.api.list;
94
95
public interface ImmutableList<T> extends ListIterable<T> {
96
// Modification operations return new instances
97
ImmutableList<T> newWith(T element);
98
ImmutableList<T> newWithout(T element);
99
ImmutableList<T> newWithAll(Iterable<? extends T> elements);
100
ImmutableList<T> newWithoutAll(Iterable<? extends T> elements);
101
102
// Functional transformations return new instances
103
<V> ImmutableList<V> collect(Function<? super T, ? extends V> function);
104
ImmutableList<T> select(Predicate<? super T> predicate);
105
ImmutableList<T> reject(Predicate<? super T> predicate);
106
107
// Sublist operations return new instances
108
ImmutableList<T> subList(int fromIndex, int toIndex);
109
ImmutableList<T> take(int count);
110
ImmutableList<T> drop(int count);
111
ImmutableList<T> toReversed();
112
ImmutableList<T> distinct();
113
}
114
```
115
116
## Sets
117
118
### SetIterable<T>
119
Base interface for set collections with mathematical set operations.
120
121
```java { .api }
122
package org.eclipse.collections.api.set;
123
124
public interface SetIterable<T> extends RichIterable<T> {
125
// Mathematical set operations
126
SetIterable<T> union(SetIterable<? extends T> set);
127
SetIterable<T> intersect(SetIterable<? extends T> set);
128
SetIterable<T> difference(SetIterable<? extends T> subtrahendSet);
129
SetIterable<T> symmetricDifference(SetIterable<? extends T> setB);
130
131
// Set relationships
132
boolean isSubsetOf(SetIterable<? extends T> candidateSuperset);
133
boolean isProperSubsetOf(SetIterable<? extends T> candidateSuperset);
134
135
// Cartesian product
136
<R extends Set<Pair<T, B>>, B> R cartesianProduct(
137
SetIterable<B> set,
138
Supplier<R> setFactory
139
);
140
141
// Functional operations returning Sets
142
<V> SetIterable<V> collect(Function<? super T, ? extends V> function);
143
SetIterable<T> select(Predicate<? super T> predicate);
144
SetIterable<T> reject(Predicate<? super T> predicate);
145
146
// Conversion
147
MutableSet<T> toSet();
148
ImmutableSet<T> toImmutableSet();
149
}
150
```
151
152
### MutableSet<T>
153
Mutable set implementation with Eclipse Collections enhancements.
154
155
```java { .api }
156
package org.eclipse.collections.api.set;
157
158
public interface MutableSet<T> extends MutableCollection<T>, Set<T>, SetIterable<T> {
159
// Modification operations
160
MutableSet<T> with(T element);
161
MutableSet<T> without(T element);
162
MutableSet<T> withAll(Iterable<? extends T> elements);
163
MutableSet<T> withoutAll(Iterable<? extends T> elements);
164
165
// Mathematical set operations (mutating)
166
MutableSet<T> unionInPlace(SetIterable<? extends T> set);
167
MutableSet<T> intersectInPlace(SetIterable<? extends T> set);
168
MutableSet<T> differenceInPlace(SetIterable<? extends T> subtrahendSet);
169
MutableSet<T> symmetricDifferenceInPlace(SetIterable<? extends T> setB);
170
171
// Mathematical set operations (new instances)
172
MutableSet<T> union(SetIterable<? extends T> set);
173
MutableSet<T> intersect(SetIterable<? extends T> set);
174
MutableSet<T> difference(SetIterable<? extends T> subtrahendSet);
175
MutableSet<T> symmetricDifference(SetIterable<? extends T> setB);
176
177
// Functional operations
178
<V> MutableSet<V> collect(Function<? super T, ? extends V> function);
179
MutableSet<T> select(Predicate<? super T> predicate);
180
MutableSet<T> reject(Predicate<? super T> predicate);
181
182
// Immutable copy
183
ImmutableSet<T> toImmutable();
184
}
185
```
186
187
### ImmutableSet<T>
188
Thread-safe immutable set where all operations return new instances.
189
190
```java { .api }
191
package org.eclipse.collections.api.set;
192
193
public interface ImmutableSet<T> extends SetIterable<T> {
194
// Modification operations return new instances
195
ImmutableSet<T> newWith(T element);
196
ImmutableSet<T> newWithout(T element);
197
ImmutableSet<T> newWithAll(Iterable<? extends T> elements);
198
ImmutableSet<T> newWithoutAll(Iterable<? extends T> elements);
199
200
// Mathematical set operations return new instances
201
ImmutableSet<T> union(SetIterable<? extends T> set);
202
ImmutableSet<T> intersect(SetIterable<? extends T> set);
203
ImmutableSet<T> difference(SetIterable<? extends T> subtrahendSet);
204
ImmutableSet<T> symmetricDifference(SetIterable<? extends T> setB);
205
206
// Functional operations return new instances
207
<V> ImmutableSet<V> collect(Function<? super T, ? extends V> function);
208
ImmutableSet<T> select(Predicate<? super T> predicate);
209
ImmutableSet<T> reject(Predicate<? super T> predicate);
210
}
211
```
212
213
## Bags
214
215
### Bag<T>
216
Multiset collection that allows duplicates and provides occurrence counting.
217
218
```java { .api }
219
package org.eclipse.collections.api.bag;
220
221
public interface Bag<T> extends RichIterable<T> {
222
// Occurrence operations
223
int occurrencesOf(Object item);
224
void forEachWithOccurrences(ObjectIntProcedure<? super T> procedure);
225
226
// Occurrence-based operations
227
Bag<T> selectByOccurrences(IntPredicate predicate);
228
Map<T, Integer> toMapOfItemToCount();
229
230
// Bag-specific operations
231
int sizeDistinct();
232
Bag<T> topOccurrences(int count);
233
Bag<T> bottomOccurrences(int count);
234
235
// Functional operations returning Bags
236
<V> Bag<V> collect(Function<? super T, ? extends V> function);
237
Bag<T> select(Predicate<? super T> predicate);
238
Bag<T> reject(Predicate<? super T> predicate);
239
240
// Conversion
241
MutableBag<T> toBag();
242
ImmutableBag<T> toImmutableBag();
243
}
244
```
245
246
### MutableBag<T>
247
Mutable bag implementation with occurrence manipulation.
248
249
```java { .api }
250
package org.eclipse.collections.api.bag;
251
252
public interface MutableBag<T> extends MutableBagIterable<T>, Bag<T> {
253
// Occurrence-based modifications
254
boolean addOccurrences(T item, int occurrences);
255
boolean removeOccurrences(Object item, int occurrences);
256
boolean setOccurrences(T item, int occurrences);
257
258
// Modification operations
259
MutableBag<T> with(T element);
260
MutableBag<T> without(T element);
261
MutableBag<T> withAll(Iterable<? extends T> elements);
262
MutableBag<T> withoutAll(Iterable<? extends T> elements);
263
264
// Functional operations
265
<V> MutableBag<V> collect(Function<? super T, ? extends V> function);
266
MutableBag<T> select(Predicate<? super T> predicate);
267
MutableBag<T> reject(Predicate<? super T> predicate);
268
269
// Occurrence-based operations
270
MutableBag<T> selectByOccurrences(IntPredicate predicate);
271
272
// Immutable copy
273
ImmutableBag<T> toImmutable();
274
}
275
```
276
277
### ImmutableBag<T>
278
Thread-safe immutable bag where all operations return new instances.
279
280
```java { .api }
281
package org.eclipse.collections.api.bag;
282
283
public interface ImmutableBag<T> extends Bag<T> {
284
// Modification operations return new instances
285
ImmutableBag<T> newWith(T element);
286
ImmutableBag<T> newWithout(T element);
287
ImmutableBag<T> newWithAll(Iterable<? extends T> elements);
288
ImmutableBag<T> newWithoutAll(Iterable<? extends T> elements);
289
290
// Occurrence operations return new instances
291
ImmutableBag<T> newWithOccurrences(T item, int occurrences);
292
ImmutableBag<T> newWithoutOccurrences(Object item, int occurrences);
293
294
// Functional operations return new instances
295
<V> ImmutableBag<V> collect(Function<? super T, ? extends V> function);
296
ImmutableBag<T> select(Predicate<? super T> predicate);
297
ImmutableBag<T> reject(Predicate<? super T> predicate);
298
ImmutableBag<T> selectByOccurrences(IntPredicate predicate);
299
}
300
```
301
302
## Stacks
303
304
### StackIterable<T>
305
LIFO (Last-In-First-Out) collection interface with stack-specific operations.
306
307
```java { .api }
308
package org.eclipse.collections.api.stack;
309
310
public interface StackIterable<T> extends OrderedIterable<T> {
311
// Stack access operations
312
T peek();
313
T peekAt(int index);
314
315
// Stack-specific queries
316
ListIterable<T> peek(int count);
317
318
// Stack-specific functional operations
319
boolean allSatisfyFromTop(Predicate<? super T> predicate, int count);
320
boolean anySatisfyFromTop(Predicate<? super T> predicate, int count);
321
int countFromTop(Predicate<? super T> predicate, int count);
322
323
// Functional operations returning Stacks
324
<V> StackIterable<V> collect(Function<? super T, ? extends V> function);
325
StackIterable<T> select(Predicate<? super T> predicate);
326
StackIterable<T> reject(Predicate<? super T> predicate);
327
328
// Conversion
329
MutableStack<T> toStack();
330
ImmutableStack<T> toImmutableStack();
331
}
332
```
333
334
### MutableStack<T>
335
Mutable stack implementation with LIFO operations.
336
337
```java { .api }
338
package org.eclipse.collections.api.stack;
339
340
public interface MutableStack<T> extends MutableCollection<T>, StackIterable<T> {
341
// Stack modification operations
342
void push(T item);
343
T pop();
344
ListIterable<T> pop(int count);
345
void clear();
346
347
// Modification operations
348
MutableStack<T> with(T element);
349
MutableStack<T> without(T element);
350
MutableStack<T> withAll(Iterable<? extends T> elements);
351
MutableStack<T> withoutAll(Iterable<? extends T> elements);
352
353
// Functional operations
354
<V> MutableStack<V> collect(Function<? super T, ? extends V> function);
355
MutableStack<T> select(Predicate<? super T> predicate);
356
MutableStack<T> reject(Predicate<? super T> predicate);
357
358
// Immutable copy
359
ImmutableStack<T> toImmutable();
360
}
361
```
362
363
### ImmutableStack<T>
364
Thread-safe immutable stack where all operations return new instances.
365
366
```java { .api }
367
package org.eclipse.collections.api.stack;
368
369
public interface ImmutableStack<T> extends StackIterable<T> {
370
// Stack operations return new instances
371
ImmutableStack<T> push(T item);
372
ImmutableStack<T> pop();
373
ImmutableStack<T> pop(int count);
374
375
// Modification operations return new instances
376
ImmutableStack<T> newWith(T element);
377
ImmutableStack<T> newWithout(T element);
378
ImmutableStack<T> newWithAll(Iterable<? extends T> elements);
379
ImmutableStack<T> newWithoutAll(Iterable<? extends T> elements);
380
381
// Functional operations return new instances
382
<V> ImmutableStack<V> collect(Function<? super T, ? extends V> function);
383
ImmutableStack<T> select(Predicate<? super T> predicate);
384
ImmutableStack<T> reject(Predicate<? super T> predicate);
385
}
386
```
387
388
## Usage Examples
389
390
### Working with Lists
391
```java { .api }
392
// Create and manipulate lists
393
MutableList<String> names = Lists.mutable.with("Alice", "Bob", "Charlie");
394
names.add("David");
395
396
// List-specific operations
397
String first = names.getFirst(); // "Alice"
398
String last = names.getLast(); // "David"
399
int index = names.indexOf("Bob"); // 1
400
401
// Functional operations
402
MutableList<String> filtered = names.select(name -> name.length() > 3);
403
MutableList<Integer> lengths = names.collect(String::length);
404
405
// Immutable operations
406
ImmutableList<String> immutableNames = names.toImmutable();
407
ImmutableList<String> withNew = immutableNames.newWith("Eve");
408
```
409
410
### Working with Sets
411
```java { .api }
412
// Create sets
413
MutableSet<Integer> set1 = Sets.mutable.with(1, 2, 3);
414
MutableSet<Integer> set2 = Sets.mutable.with(3, 4, 5);
415
416
// Mathematical operations
417
MutableSet<Integer> union = set1.union(set2); // {1, 2, 3, 4, 5}
418
MutableSet<Integer> intersect = set1.intersect(set2); // {3}
419
MutableSet<Integer> diff = set1.difference(set2); // {1, 2}
420
421
// Set relationships
422
boolean isSubset = set1.isSubsetOf(union); // true
423
```
424
425
### Working with Bags
426
```java { .api }
427
// Create bag with duplicates
428
MutableBag<String> words = Bags.mutable.with("hello", "world", "hello", "java");
429
430
// Occurrence operations
431
int count = words.occurrencesOf("hello"); // 2
432
words.addOccurrences("hello", 3); // now has 5 "hello"s
433
434
// Occurrence-based filtering
435
MutableBag<String> frequent = words.selectByOccurrences(count -> count > 1);
436
```
437
438
### Working with Stacks
439
```java { .api }
440
// Create and manipulate stack
441
MutableStack<String> stack = Stacks.mutable.empty();
442
stack.push("first");
443
stack.push("second");
444
stack.push("third");
445
446
// Stack operations
447
String top = stack.peek(); // "third"
448
String popped = stack.pop(); // "third", removes from stack
449
ListIterable<String> top2 = stack.peek(2); // ["second", "first"]
450
```
451
452
## Sorted Collections
453
454
Eclipse Collections provides sorted variants of Sets and Bags that maintain elements in sorted order according to their natural ordering or a provided Comparator.
455
456
### SortedSetIterable<T>
457
Base interface for sorted sets extending both SetIterable and SortedIterable.
458
459
```java { .api }
460
package org.eclipse.collections.api.set.sorted;
461
462
public interface SortedSetIterable<T> extends SetIterable<T>, SortedIterable<T> {
463
// Sorted-specific access
464
T first();
465
T last();
466
467
// Range operations
468
SortedSetIterable<T> takeWhile(Predicate<? super T> predicate);
469
SortedSetIterable<T> dropWhile(Predicate<? super T> predicate);
470
471
// Subset operations
472
SortedSetIterable<T> subSet(T fromElement, T toElement);
473
SortedSetIterable<T> headSet(T toElement);
474
SortedSetIterable<T> tailSet(T fromElement);
475
476
// Functional operations returning sorted sets
477
<V> SortedSetIterable<V> collect(Function<? super T, ? extends V> function);
478
SortedSetIterable<T> select(Predicate<? super T> predicate);
479
SortedSetIterable<T> reject(Predicate<? super T> predicate);
480
481
// Conversion
482
MutableSortedSet<T> toSortedSet();
483
ImmutableSortedSet<T> toImmutableSortedSet();
484
}
485
```
486
487
### MutableSortedSet<T>
488
Mutable sorted set implementation extending Java's SortedSet interface.
489
490
```java { .api }
491
package org.eclipse.collections.api.set.sorted;
492
493
public interface MutableSortedSet<T> extends MutableSet<T>, SortedSet<T>, SortedSetIterable<T> {
494
// Modification operations
495
MutableSortedSet<T> with(T element);
496
MutableSortedSet<T> without(T element);
497
MutableSortedSet<T> withAll(Iterable<? extends T> elements);
498
MutableSortedSet<T> withoutAll(Iterable<? extends T> elements);
499
500
// Functional operations
501
<V> MutableSortedSet<V> collect(Function<? super T, ? extends V> function);
502
MutableSortedSet<T> select(Predicate<? super T> predicate);
503
MutableSortedSet<T> reject(Predicate<? super T> predicate);
504
505
// Range operations
506
MutableSortedSet<T> subSet(T fromElement, T toElement);
507
MutableSortedSet<T> headSet(T toElement);
508
MutableSortedSet<T> tailSet(T fromElement);
509
510
// Immutable copy
511
ImmutableSortedSet<T> toImmutable();
512
}
513
```
514
515
### ImmutableSortedSet<T>
516
Thread-safe immutable sorted set where all operations return new instances.
517
518
```java { .api }
519
package org.eclipse.collections.api.set.sorted;
520
521
public interface ImmutableSortedSet<T> extends SortedSetIterable<T> {
522
// Modification operations return new instances
523
ImmutableSortedSet<T> newWith(T element);
524
ImmutableSortedSet<T> newWithout(T element);
525
ImmutableSortedSet<T> newWithAll(Iterable<? extends T> elements);
526
ImmutableSortedSet<T> newWithoutAll(Iterable<? extends T> elements);
527
528
// Functional operations return new instances
529
<V> ImmutableSortedSet<V> collect(Function<? super T, ? extends V> function);
530
ImmutableSortedSet<T> select(Predicate<? super T> predicate);
531
ImmutableSortedSet<T> reject(Predicate<? super T> predicate);
532
533
// Range operations return new instances
534
ImmutableSortedSet<T> subSet(T fromElement, T toElement);
535
ImmutableSortedSet<T> headSet(T toElement);
536
ImmutableSortedSet<T> tailSet(T fromElement);
537
}
538
```
539
540
### SortedBag<T>
541
Base interface for sorted bags (multisets) that maintain elements in sorted order.
542
543
```java { .api }
544
package org.eclipse.collections.api.bag.sorted;
545
546
public interface SortedBag<T> extends Bag<T>, SortedIterable<T> {
547
// Sorted-specific access
548
T first();
549
T last();
550
551
// Range operations
552
SortedBag<T> takeWhile(Predicate<? super T> predicate);
553
SortedBag<T> dropWhile(Predicate<? super T> predicate);
554
555
// Functional operations returning sorted bags
556
<V> SortedBag<V> collect(Function<? super T, ? extends V> function);
557
SortedBag<T> select(Predicate<? super T> predicate);
558
SortedBag<T> reject(Predicate<? super T> predicate);
559
SortedBag<T> selectByOccurrences(IntPredicate predicate);
560
561
// Conversion
562
MutableSortedBag<T> toSortedBag();
563
ImmutableSortedBag<T> toImmutableSortedBag();
564
}
565
```
566
567
### MutableSortedBag<T>
568
Mutable sorted bag implementation with occurrence counting in sorted order.
569
570
```java { .api }
571
package org.eclipse.collections.api.bag.sorted;
572
573
public interface MutableSortedBag<T> extends MutableBag<T>, SortedBag<T> {
574
// Occurrence-based modifications
575
boolean addOccurrences(T item, int occurrences);
576
boolean removeOccurrences(Object item, int occurrences);
577
boolean setOccurrences(T item, int occurrences);
578
579
// Modification operations
580
MutableSortedBag<T> with(T element);
581
MutableSortedBag<T> without(T element);
582
MutableSortedBag<T> withAll(Iterable<? extends T> elements);
583
MutableSortedBag<T> withoutAll(Iterable<? extends T> elements);
584
585
// Functional operations
586
<V> MutableSortedBag<V> collect(Function<? super T, ? extends V> function);
587
MutableSortedBag<T> select(Predicate<? super T> predicate);
588
MutableSortedBag<T> reject(Predicate<? super T> predicate);
589
MutableSortedBag<T> selectByOccurrences(IntPredicate predicate);
590
591
// Immutable copy
592
ImmutableSortedBag<T> toImmutable();
593
}
594
```
595
596
### ImmutableSortedBag<T>
597
Thread-safe immutable sorted bag where all operations return new instances.
598
599
```java { .api }
600
package org.eclipse.collections.api.bag.sorted;
601
602
public interface ImmutableSortedBag<T> extends SortedBag<T> {
603
// Modification operations return new instances
604
ImmutableSortedBag<T> newWith(T element);
605
ImmutableSortedBag<T> newWithout(T element);
606
ImmutableSortedBag<T> newWithAll(Iterable<? extends T> elements);
607
ImmutableSortedBag<T> newWithoutAll(Iterable<? extends T> elements);
608
609
// Occurrence operations return new instances
610
ImmutableSortedBag<T> newWithOccurrences(T item, int occurrences);
611
ImmutableSortedBag<T> newWithoutOccurrences(Object item, int occurrences);
612
613
// Functional operations return new instances
614
<V> ImmutableSortedBag<V> collect(Function<? super T, ? extends V> function);
615
ImmutableSortedBag<T> select(Predicate<? super T> predicate);
616
ImmutableSortedBag<T> reject(Predicate<? super T> predicate);
617
ImmutableSortedBag<T> selectByOccurrences(IntPredicate predicate);
618
}
619
```
620
621
### Working with Sorted Collections
622
623
```java { .api }
624
// Create sorted collections
625
MutableSortedSet<String> sortedSet = SortedSets.mutable.with("zebra", "apple", "banana");
626
// Elements are automatically sorted: ["apple", "banana", "zebra"]
627
628
// Range operations
629
SortedSetIterable<String> subset = sortedSet.subSet("apple", "zebra");
630
SortedSetIterable<String> headSet = sortedSet.headSet("banana"); // ["apple"]
631
632
// Sorted bags with duplicates
633
MutableSortedBag<Integer> sortedBag = SortedBags.mutable.with(3, 1, 4, 1, 5);
634
// Elements maintain sort order: [1, 1, 3, 4, 5]
635
636
// Occurrence operations preserve sorting
637
sortedBag.addOccurrences(2, 3); // [1, 1, 2, 2, 2, 3, 4, 5]
638
```