0
# Functional Interfaces
1
2
Eclipse Collections provides rich functional programming support through comprehensive Function, Predicate, and Procedure interfaces. These interfaces extend Java's standard functional interfaces while adding Eclipse Collections-specific functionality and primitive specializations for all 8 primitive types.
3
4
## Core Functional Interfaces
5
6
### Functions
7
8
Functions transform input values to output values, providing the foundation for map/collect operations.
9
10
#### Function<T,V>
11
Single argument transformation function extending Java's Function interface.
12
13
```java { .api }
14
package org.eclipse.collections.api.block.function;
15
16
@FunctionalInterface
17
public interface Function<T, V> extends java.util.function.Function<T, V> {
18
// Primary transformation method
19
V valueOf(T argument);
20
21
// Java 8 compatibility (delegates to valueOf)
22
@Override
23
default V apply(T argument) {
24
return this.valueOf(argument);
25
}
26
}
27
```
28
29
#### Multi-Argument Functions
30
Functions for multiple input parameters.
31
32
```java { .api }
33
// Zero argument function
34
package org.eclipse.collections.api.block.function;
35
36
@FunctionalInterface
37
public interface Function0<R> extends java.util.function.Supplier<R> {
38
R value();
39
40
@Override
41
default R get() {
42
return this.value();
43
}
44
}
45
46
// Two argument function
47
@FunctionalInterface
48
public interface Function2<T1, T2, R> extends java.util.function.BiFunction<T1, T2, R> {
49
R value(T1 argument1, T2 argument2);
50
51
@Override
52
default R apply(T1 argument1, T2 argument2) {
53
return this.value(argument1, argument2);
54
}
55
}
56
57
// Three argument function
58
@FunctionalInterface
59
public interface Function3<T1, T2, T3, R> {
60
R value(T1 argument1, T2 argument2, T3 argument3);
61
}
62
```
63
64
### Predicates
65
66
Predicates test boolean conditions, providing the foundation for filter/select/reject operations.
67
68
#### Predicate<T>
69
Single argument boolean test function extending Java's Predicate interface.
70
71
```java { .api }
72
package org.eclipse.collections.api.block.predicate;
73
74
@FunctionalInterface
75
public interface Predicate<T> extends java.util.function.Predicate<T> {
76
// Primary testing method
77
boolean accept(T argument);
78
79
// Java 8 compatibility (delegates to accept)
80
@Override
81
default boolean test(T argument) {
82
return this.accept(argument);
83
}
84
}
85
```
86
87
#### Multi-Argument Predicates
88
Predicates for multiple input parameters.
89
90
```java { .api }
91
// Two argument predicate
92
package org.eclipse.collections.api.block.predicate;
93
94
@FunctionalInterface
95
public interface Predicate2<T, P> extends java.util.function.BiPredicate<T, P> {
96
boolean accept(T argument1, P argument2);
97
98
@Override
99
default boolean test(T argument1, P argument2) {
100
return this.accept(argument1, argument2);
101
}
102
}
103
```
104
105
### Procedures
106
107
Procedures perform side-effect operations without returning values, used in forEach operations.
108
109
#### Procedure<T>
110
Single argument side-effect operation.
111
112
```java { .api }
113
package org.eclipse.collections.api.block.procedure;
114
115
@FunctionalInterface
116
public interface Procedure<T> extends java.util.function.Consumer<T> {
117
// Primary side-effect method
118
void value(T argument);
119
120
// Java 8 compatibility (delegates to value)
121
@Override
122
default void accept(T argument) {
123
this.value(argument);
124
}
125
}
126
```
127
128
#### Multi-Argument Procedures
129
Procedures for multiple input parameters.
130
131
```java { .api }
132
// Two argument procedure
133
package org.eclipse.collections.api.block.procedure;
134
135
@FunctionalInterface
136
public interface Procedure2<T, P> extends java.util.function.BiConsumer<T, P> {
137
void value(T argument1, P argument2);
138
139
@Override
140
default void accept(T argument1, P argument2) {
141
this.value(argument1, argument2);
142
}
143
}
144
145
// Zero argument procedure (Runnable equivalent)
146
@FunctionalInterface
147
public interface Procedure0 extends Runnable {
148
void value();
149
150
@Override
151
default void run() {
152
this.value();
153
}
154
}
155
```
156
157
## Primitive Functional Interfaces
158
159
Eclipse Collections provides primitive specializations of all functional interfaces for each of the 8 primitive types, avoiding boxing overhead.
160
161
### Primitive Functions
162
163
#### Object-to-Primitive Functions
164
Functions that transform objects to primitive values.
165
166
```java { .api }
167
// Transform objects to int values
168
package org.eclipse.collections.api.block.function.primitive;
169
170
@FunctionalInterface
171
public interface IntFunction<T> extends java.util.function.ToIntFunction<T> {
172
int intValueOf(T argument);
173
174
@Override
175
default int applyAsInt(T value) {
176
return this.intValueOf(value);
177
}
178
}
179
180
// Similar interfaces for all primitive types:
181
@FunctionalInterface
182
public interface BooleanFunction<T> {
183
boolean booleanValueOf(T argument);
184
}
185
186
@FunctionalInterface
187
public interface ByteFunction<T> {
188
byte byteValueOf(T argument);
189
}
190
191
@FunctionalInterface
192
public interface CharFunction<T> {
193
char charValueOf(T argument);
194
}
195
196
@FunctionalInterface
197
public interface DoubleFunction<T> extends java.util.function.ToDoubleFunction<T> {
198
double doubleValueOf(T argument);
199
200
@Override
201
default double applyAsDouble(T value) {
202
return this.doubleValueOf(value);
203
}
204
}
205
206
@FunctionalInterface
207
public interface FloatFunction<T> {
208
float floatValueOf(T argument);
209
}
210
211
@FunctionalInterface
212
public interface LongFunction<T> extends java.util.function.ToLongFunction<T> {
213
long longValueOf(T argument);
214
215
@Override
216
default long applyAsLong(T value) {
217
return this.longValueOf(value);
218
}
219
}
220
221
@FunctionalInterface
222
public interface ShortFunction<T> {
223
short shortValueOf(T argument);
224
}
225
```
226
227
#### Primitive-to-Object Functions
228
Functions that transform primitive values to objects.
229
230
```java { .api }
231
// Transform int values to objects
232
package org.eclipse.collections.api.block.function.primitive;
233
234
@FunctionalInterface
235
public interface IntToObjectFunction<V> extends java.util.function.IntFunction<V> {
236
V valueOf(int argument);
237
238
@Override
239
default V apply(int value) {
240
return this.valueOf(value);
241
}
242
}
243
244
// Similar interfaces for all primitive types:
245
@FunctionalInterface
246
public interface BooleanToObjectFunction<V> {
247
V valueOf(boolean argument);
248
}
249
250
@FunctionalInterface
251
public interface ByteToObjectFunction<V> {
252
V valueOf(byte argument);
253
}
254
255
@FunctionalInterface
256
public interface CharToObjectFunction<V> {
257
V valueOf(char argument);
258
}
259
260
@FunctionalInterface
261
public interface DoubleToObjectFunction<V> extends java.util.function.DoubleFunction<V> {
262
V valueOf(double argument);
263
264
@Override
265
default V apply(double value) {
266
return this.valueOf(value);
267
}
268
}
269
270
@FunctionalInterface
271
public interface FloatToObjectFunction<V> {
272
V valueOf(float argument);
273
}
274
275
@FunctionalInterface
276
public interface LongToObjectFunction<V> extends java.util.function.LongFunction<V> {
277
V valueOf(long argument);
278
279
@Override
280
default V apply(long value) {
281
return this.valueOf(value);
282
}
283
}
284
285
@FunctionalInterface
286
public interface ShortToObjectFunction<V> {
287
V valueOf(short argument);
288
}
289
```
290
291
#### Primitive-to-Primitive Functions
292
Functions that transform between primitive types.
293
294
```java { .api }
295
// Transform int to int
296
package org.eclipse.collections.api.block.function.primitive;
297
298
@FunctionalInterface
299
public interface IntToIntFunction extends java.util.function.IntUnaryOperator {
300
int valueOf(int argument);
301
302
@Override
303
default int applyAsInt(int operand) {
304
return this.valueOf(operand);
305
}
306
}
307
308
// Cross-type transformations
309
@FunctionalInterface
310
public interface IntToLongFunction extends java.util.function.IntToLongFunction {
311
long valueOf(int argument);
312
313
@Override
314
default long applyAsLong(int value) {
315
return this.valueOf(value);
316
}
317
}
318
319
@FunctionalInterface
320
public interface IntToDoubleFunction extends java.util.function.IntToDoubleFunction {
321
double valueOf(int argument);
322
323
@Override
324
default double applyAsDouble(int value) {
325
return this.valueOf(value);
326
}
327
}
328
329
@FunctionalInterface
330
public interface IntToFloatFunction {
331
float valueOf(int argument);
332
}
333
334
@FunctionalInterface
335
public interface IntToCharFunction {
336
char valueOf(int argument);
337
}
338
339
@FunctionalInterface
340
public interface IntToByteFunction {
341
byte valueOf(int argument);
342
}
343
344
@FunctionalInterface
345
public interface IntToShortFunction {
346
short valueOf(int argument);
347
}
348
349
@FunctionalInterface
350
public interface IntToBooleanFunction {
351
boolean valueOf(int argument);
352
}
353
```
354
355
### Primitive Predicates
356
357
#### Primitive Predicates
358
Boolean tests on primitive values.
359
360
```java { .api }
361
// Test int values
362
package org.eclipse.collections.api.block.predicate.primitive;
363
364
@FunctionalInterface
365
public interface IntPredicate extends java.util.function.IntPredicate {
366
boolean accept(int value);
367
368
@Override
369
default boolean test(int value) {
370
return this.accept(value);
371
}
372
}
373
374
// Similar interfaces for all primitive types:
375
@FunctionalInterface
376
public interface BooleanPredicate {
377
boolean accept(boolean value);
378
}
379
380
@FunctionalInterface
381
public interface BytePredicate {
382
boolean accept(byte value);
383
}
384
385
@FunctionalInterface
386
public interface CharPredicate {
387
boolean accept(char value);
388
}
389
390
@FunctionalInterface
391
public interface DoublePredicate extends java.util.function.DoublePredicate {
392
boolean accept(double value);
393
394
@Override
395
default boolean test(double value) {
396
return this.accept(value);
397
}
398
}
399
400
@FunctionalInterface
401
public interface FloatPredicate {
402
boolean accept(float value);
403
}
404
405
@FunctionalInterface
406
public interface LongPredicate extends java.util.function.LongPredicate {
407
boolean accept(long value);
408
409
@Override
410
default boolean test(long value) {
411
return this.accept(value);
412
}
413
}
414
415
@FunctionalInterface
416
public interface ShortPredicate {
417
boolean accept(short value);
418
}
419
```
420
421
#### Object-Primitive Predicates
422
Boolean tests on object-primitive pairs.
423
424
```java { .api }
425
// Test object-int pairs
426
package org.eclipse.collections.api.block.predicate.primitive;
427
428
@FunctionalInterface
429
public interface ObjectIntPredicate<T> {
430
boolean accept(T object, int intParameter);
431
}
432
433
// Similar interfaces for all primitive types:
434
@FunctionalInterface
435
public interface ObjectBooleanPredicate<T> {
436
boolean accept(T object, boolean booleanParameter);
437
}
438
439
@FunctionalInterface
440
public interface ObjectBytePredicate<T> {
441
boolean accept(T object, byte byteParameter);
442
}
443
444
@FunctionalInterface
445
public interface ObjectCharPredicate<T> {
446
boolean accept(T object, char charParameter);
447
}
448
449
@FunctionalInterface
450
public interface ObjectDoublePredicate<T> {
451
boolean accept(T object, double doubleParameter);
452
}
453
454
@FunctionalInterface
455
public interface ObjectFloatPredicate<T> {
456
boolean accept(T object, float floatParameter);
457
}
458
459
@FunctionalInterface
460
public interface ObjectLongPredicate<T> {
461
boolean accept(T object, long longParameter);
462
}
463
464
@FunctionalInterface
465
public interface ObjectShortPredicate<T> {
466
boolean accept(T object, short shortParameter);
467
}
468
```
469
470
### Primitive Procedures
471
472
#### Primitive Procedures
473
Side-effect operations on primitive values.
474
475
```java { .api }
476
// Side effects on int values
477
package org.eclipse.collections.api.block.procedure.primitive;
478
479
@FunctionalInterface
480
public interface IntProcedure extends java.util.function.IntConsumer {
481
void value(int argument);
482
483
@Override
484
default void accept(int value) {
485
this.value(value);
486
}
487
}
488
489
// Similar interfaces for all primitive types:
490
@FunctionalInterface
491
public interface BooleanProcedure {
492
void value(boolean argument);
493
}
494
495
@FunctionalInterface
496
public interface ByteProcedure {
497
void value(byte argument);
498
}
499
500
@FunctionalInterface
501
public interface CharProcedure {
502
void value(char argument);
503
}
504
505
@FunctionalInterface
506
public interface DoubleProcedure extends java.util.function.DoubleConsumer {
507
void value(double argument);
508
509
@Override
510
default void accept(double value) {
511
this.value(value);
512
}
513
}
514
515
@FunctionalInterface
516
public interface FloatProcedure {
517
void value(float argument);
518
}
519
520
@FunctionalInterface
521
public interface LongProcedure extends java.util.function.LongConsumer {
522
void value(long argument);
523
524
@Override
525
default void accept(long value) {
526
this.value(value);
527
}
528
}
529
530
@FunctionalInterface
531
public interface ShortProcedure {
532
void value(short argument);
533
}
534
```
535
536
#### Object-Primitive Procedures
537
Side-effect operations on object-primitive pairs.
538
539
```java { .api }
540
// Side effects on object-int pairs
541
package org.eclipse.collections.api.block.procedure.primitive;
542
543
@FunctionalInterface
544
public interface ObjectIntProcedure<T> {
545
void value(T object, int intParameter);
546
}
547
548
// Similar interfaces for all primitive types:
549
@FunctionalInterface
550
public interface ObjectBooleanProcedure<T> {
551
void value(T object, boolean booleanParameter);
552
}
553
554
@FunctionalInterface
555
public interface ObjectByteProcedure<T> {
556
void value(T object, byte byteParameter);
557
}
558
559
@FunctionalInterface
560
public interface ObjectCharProcedure<T> {
561
void value(T object, char charParameter);
562
}
563
564
@FunctionalInterface
565
public interface ObjectDoubleProcedure<T> {
566
void value(T object, double doubleParameter);
567
}
568
569
@FunctionalInterface
570
public interface ObjectFloatProcedure<T> {
571
void value(T object, float floatParameter);
572
}
573
574
@FunctionalInterface
575
public interface ObjectLongProcedure<T> {
576
void value(T object, long longParameter);
577
}
578
579
@FunctionalInterface
580
public interface ObjectShortProcedure<T> {
581
void value(T object, short shortParameter);
582
}
583
```
584
585
#### Primitive-Primitive Procedures
586
Side-effect operations on primitive pairs.
587
588
```java { .api }
589
// Side effects on int-int pairs
590
package org.eclipse.collections.api.block.procedure.primitive;
591
592
@FunctionalInterface
593
public interface IntIntProcedure {
594
void value(int argument1, int argument2);
595
}
596
597
// Cross-type combinations (examples):
598
@FunctionalInterface
599
public interface IntLongProcedure {
600
void value(int intParameter, long longParameter);
601
}
602
603
@FunctionalInterface
604
public interface IntDoubleProcedure {
605
void value(int intParameter, double doubleParameter);
606
}
607
608
@FunctionalInterface
609
public interface LongIntProcedure {
610
void value(long longParameter, int intParameter);
611
}
612
613
// Similar interfaces exist for all meaningful primitive type combinations
614
```
615
616
## Comparator Interfaces
617
618
### Primitive Comparators
619
Efficient comparison operations for primitive types.
620
621
```java { .api }
622
// Compare int values
623
package org.eclipse.collections.api.block.comparator.primitive;
624
625
@FunctionalInterface
626
public interface IntComparator {
627
int compare(int o1, int o2);
628
}
629
630
// Similar interfaces for all primitive types:
631
@FunctionalInterface
632
public interface BooleanComparator {
633
int compare(boolean o1, boolean o2);
634
}
635
636
@FunctionalInterface
637
public interface ByteComparator {
638
int compare(byte o1, byte o2);
639
}
640
641
@FunctionalInterface
642
public interface CharComparator {
643
int compare(char o1, char o2);
644
}
645
646
@FunctionalInterface
647
public interface DoubleComparator {
648
int compare(double o1, double o2);
649
}
650
651
@FunctionalInterface
652
public interface FloatComparator {
653
int compare(float o1, float o2);
654
}
655
656
@FunctionalInterface
657
public interface LongComparator {
658
int compare(long o1, long o2);
659
}
660
661
@FunctionalInterface
662
public interface ShortComparator {
663
int compare(short o1, short o2);
664
}
665
```
666
667
## Usage Examples
668
669
### Using Object Functions
670
```java { .api }
671
// Transform strings to their lengths
672
Function<String, Integer> lengthFunction = String::length;
673
MutableList<String> words = Lists.mutable.with("hello", "world", "java");
674
MutableList<Integer> lengths = words.collect(lengthFunction);
675
676
// Multi-argument functions
677
Function2<String, String, String> concat = (s1, s2) -> s1 + s2;
678
String result = concat.value("Hello", " World"); // "Hello World"
679
680
// Zero-argument function
681
Function0<String> supplier = () -> "Default Value";
682
String value = supplier.value();
683
```
684
685
### Using Predicates
686
```java { .api }
687
// Filter even numbers
688
Predicate<Integer> isEven = n -> n % 2 == 0;
689
MutableList<Integer> numbers = Lists.mutable.with(1, 2, 3, 4, 5);
690
MutableList<Integer> evens = numbers.select(isEven);
691
692
// Two-argument predicate
693
Predicate2<String, Integer> hasMinLength = (str, minLen) -> str.length() >= minLen;
694
MutableList<String> words = Lists.mutable.with("a", "hello", "hi", "world");
695
MutableList<String> longWords = words.selectWith(hasMinLength, 4);
696
```
697
698
### Using Procedures
699
```java { .api }
700
// Print each element
701
Procedure<String> printer = System.out::println;
702
Lists.mutable.with("a", "b", "c").forEach(printer);
703
704
// Two-argument procedure
705
Procedure2<String, Integer> indexedPrinter = (str, index) ->
706
System.out.println(index + ": " + str);
707
Lists.mutable.with("a", "b", "c").forEachWithIndex(indexedPrinter);
708
```
709
710
### Using Primitive Functions
711
```java { .api }
712
// Convert objects to int values (no boxing)
713
IntFunction<String> stringLength = String::length;
714
MutableList<String> words = Lists.mutable.with("hello", "world");
715
MutableIntList lengths = words.collectInt(stringLength);
716
717
// Convert int values to objects
718
IntToObjectFunction<String> intToString = String::valueOf;
719
MutableIntList numbers = IntLists.mutable.with(1, 2, 3);
720
MutableList<String> strings = numbers.collect(intToString);
721
722
// Primitive-to-primitive transformations
723
IntToIntFunction square = n -> n * n;
724
MutableIntList numbers2 = IntLists.mutable.with(1, 2, 3, 4, 5);
725
MutableIntList squares = numbers2.collectInt(square);
726
```
727
728
### Using Primitive Predicates
729
```java { .api }
730
// Filter primitive collections (no boxing)
731
IntPredicate isEven = n -> n % 2 == 0;
732
MutableIntList numbers = IntLists.mutable.with(1, 2, 3, 4, 5);
733
MutableIntList evens = numbers.select(isEven);
734
735
// Object-primitive predicates
736
ObjectIntPredicate<String> hasLength = (str, len) -> str.length() == len;
737
MutableList<String> words = Lists.mutable.with("cat", "dog", "bird");
738
MutableList<String> threeLetter = words.selectWith(hasLength, 3);
739
```
740
741
### Using Primitive Procedures
742
```java { .api }
743
// Process primitive values (no boxing)
744
IntProcedure printer = System.out::println;
745
IntLists.mutable.with(1, 2, 3).forEach(printer);
746
747
// Object-primitive procedures
748
ObjectIntProcedure<String> indexedPrinter = (str, index) ->
749
System.out.println("Item " + index + ": " + str);
750
Lists.mutable.with("a", "b", "c").forEachWithIndex(indexedPrinter);
751
752
// Primitive-primitive procedures (e.g., for maps)
753
IntIntProcedure keyValuePrinter = (key, value) ->
754
System.out.println("Key: " + key + ", Value: " + value);
755
IntIntMaps.mutable.with(1, 10, 2, 20).forEachKeyValue(keyValuePrinter);
756
```
757
758
The comprehensive functional interface system in Eclipse Collections provides both type safety and performance optimization, allowing developers to write expressive functional code while maintaining efficiency through primitive specializations.