-
Notifications
You must be signed in to change notification settings - Fork 274
/
MapStream.java
1021 lines (914 loc) · 36.2 KB
/
MapStream.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (C) 2016 - present by OpenGamma Inc. and the OpenGamma group of companies
*
* Please see distribution for license.
*/
package com.opengamma.strata.collect;
import static com.opengamma.strata.collect.Guavate.entry;
import static java.util.stream.Collectors.collectingAndThen;
import static java.util.stream.Collectors.groupingBy;
import static java.util.stream.Collectors.mapping;
import static java.util.stream.Collectors.toList;
import java.util.Collection;
import java.util.Comparator;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Optional;
import java.util.Spliterator;
import java.util.function.BiConsumer;
import java.util.function.BiFunction;
import java.util.function.BiPredicate;
import java.util.function.BinaryOperator;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.IntFunction;
import java.util.function.Predicate;
import java.util.function.Supplier;
import java.util.function.ToDoubleBiFunction;
import java.util.function.ToDoubleFunction;
import java.util.function.ToIntBiFunction;
import java.util.function.ToIntFunction;
import java.util.function.ToLongFunction;
import java.util.stream.Collector;
import java.util.stream.DoubleStream;
import java.util.stream.IntStream;
import java.util.stream.LongStream;
import java.util.stream.Stream;
import com.google.common.collect.ImmutableListMultimap;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSetMultimap;
import com.google.common.collect.Multimap;
import com.google.common.collect.Streams;
/**
* A stream implementation based on {@code Map.Entry}.
* <p>
* This stream wraps a {@code Stream<Map.Entry>}, providing convenient methods for
* manipulating the keys and values. Unlike a {@code Map}, the keys in a {@code MapStream}
* do not have to be unique, although certain methods will fail if they are not unique.
*
* @param <K> the key type
* @param <V> the value type
*/
public final class MapStream<K, V>
implements Stream<Map.Entry<K, V>> {
/** The stream of map entries. */
private final Stream<Map.Entry<K, V>> underlying;
//-------------------------------------------------------------------------
/**
* Returns a stream over the entries in the map.
*
* @param <K> the key type
* @param <V> the value type
* @param map the map to wrap
* @return a stream over the entries in the map
*/
public static <K, V> MapStream<K, V> of(Map<K, V> map) {
return new MapStream<>(map.entrySet().stream());
}
/**
* Returns a stream over all the entries in the multimap.
* <p>
* This will typically create a stream with duplicate keys.
*
* @param <K> the key type
* @param <V> the value type
* @param multimap the multimap to wrap
* @return a stream over the entries in the multimap
*/
public static <K, V> MapStream<K, V> of(Multimap<K, V> multimap) {
return new MapStream<>(multimap.entries().stream());
}
/**
* Returns a stream of map entries where the keys and values are taken from a collection.
*
* @param <V> the key and value type
* @param collection the collection
* @return a stream of map entries derived from the values in the collection
*/
public static <V> MapStream<V, V> of(Collection<V> collection) {
return of(collection.stream());
}
/**
* Returns a stream of map entries where the keys and values are taken from a stream.
*
* @param <V> the key and value type
* @param stream the stream
* @return a stream of map entries derived from the values in the stream
*/
public static <V> MapStream<V, V> of(Stream<V> stream) {
return of(stream, key -> key);
}
/**
* Returns a stream of map entries where the values are taken from a collection
* and the keys are created by applying a function to each value.
*
* @param <K> the key type
* @param <V> the value type
* @param collection the collection of values
* @param keyFunction a function which returns the key for a value
* @return a stream of map entries derived from the values in the collection
*/
public static <K, V> MapStream<K, V> of(Collection<V> collection, Function<? super V, ? extends K> keyFunction) {
return of(collection.stream(), keyFunction);
}
/**
* Returns a stream of map entries where the keys and values are extracted from a
* collection by applying a function to each item in the collection.
*
* @param <T> the collection type
* @param <K> the key type
* @param <V> the value type
* @param collection the collection of values
* @param keyFunction a function which returns the key
* @param valueFunction a function which returns the value
* @return a stream of map entries derived from the collection
*/
public static <T, K, V> MapStream<K, V> of(
Collection<T> collection,
Function<? super T, ? extends K> keyFunction,
Function<? super T, ? extends V> valueFunction) {
return of(collection.stream(), keyFunction, valueFunction);
}
/**
* Returns a stream of map entries where the values are taken from a stream
* and the keys are created by applying a function to each value.
*
* @param <K> the key type
* @param <V> the value type
* @param stream the stream of values
* @param keyFunction a function which returns the key for a value
* @return a stream of map entries derived from the values in the stream
*/
public static <K, V> MapStream<K, V> of(Stream<V> stream, Function<? super V, ? extends K> keyFunction) {
return new MapStream<>(stream.map(v -> entry(keyFunction.apply(v), v)));
}
/**
* Returns a stream of map entries where the keys and values are extracted from a
* stream by applying a function to each item in the stream.
*
* @param <T> the collection type
* @param <K> the key type
* @param <V> the value type
* @param stream the stream of values
* @param keyFunction a function which returns the key for a value
* @param valueFunction a function which returns the value
* @return a stream of map entries derived from the stream
*/
public static <T, K, V> MapStream<K, V> of(
Stream<T> stream,
Function<? super T, ? extends K> keyFunction,
Function<? super T, ? extends V> valueFunction) {
return new MapStream<K, V>(stream.map(item -> entry(keyFunction.apply(item), valueFunction.apply(item))));
}
//-------------------------------------------------------------------------
/**
* Returns a map stream that combines two other streams, continuing until either stream ends.
* <p>
* Note that this can produce a stream with non-unique keys.
*
* @param <K> the key type
* @param <V> the value type
* @param keyStream the stream of keys
* @param valueStream the stream of values
* @return a stream of map entries derived from the stream
*/
public static <K, V> MapStream<K, V> zip(Stream<K> keyStream, Stream<V> valueStream) {
return new MapStream<K, V>(Guavate.zip(keyStream, valueStream, Guavate::entry));
}
/**
* Returns a stream of map entries where each key is the index of the value in the original stream.
*
* @param <V> the value type
* @param stream the stream of values
* @return a stream of map entries derived from the stream
*/
public static <V> MapStream<Integer, V> zipWithIndex(Stream<V> stream) {
Stream<Map.Entry<Integer, V>> zipped =
Streams.mapWithIndex(stream, (value, index) -> entry(Math.toIntExact(index), value));
return new MapStream<Integer, V>(zipped);
}
/**
* Returns an empty map stream.
*
* @param <K> the key type
* @param <V> the value type
* @return an empty map stream
*/
public static <K, V> MapStream<K, V> empty() {
return new MapStream<>(Stream.empty());
}
/**
* Creates a stream of map entries whose elements are those of the first stream followed by those of the second
* stream.
*
* @param a the first stream of entries
* @param b the second stream of entries
* @param <K> the key type
* @param <V> the value type
* @return the concatenation of the two input streams
*/
public static <K, V> MapStream<K, V> concat(
MapStream<? extends K, ? extends V> a,
MapStream<? extends K, ? extends V> b) {
@SuppressWarnings("unchecked")
MapStream<K, V> kvMapStream = new MapStream<>(Streams.concat(
(Stream<? extends Map.Entry<K, V>>) a,
(Stream<? extends Map.Entry<K, V>>) b));
return kvMapStream;
}
//-------------------------------------------------------------------------
// creates an instance
private MapStream(Stream<Map.Entry<K, V>> underlying) {
this.underlying = underlying;
}
//-------------------------------------------------------------------------
/**
* Returns the keys as a stream, dropping the values.
* <p>
* A {@link MapStream} may contain the same key more than once, so callers
* may need to call {@link Stream#distinct()} on the result.
*
* @return a stream of the keys
*/
public Stream<K> keys() {
return underlying.map(Entry::getKey);
}
/**
* Returns the values as a stream, dropping the keys.
*
* @return a stream of the values
*/
public Stream<V> values() {
return underlying.map(Entry::getValue);
}
//-------------------------------------------------------------------------
/**
* Returns a stream where the keys and values are swapped.
*
* @return a stream with swapped keys and values
*/
public MapStream<V, K> inverse() {
return wrap(underlying.map(e -> entry(e.getValue(), e.getKey())));
}
//-------------------------------------------------------------------------
/**
* Filters the stream by applying the predicate function to each key and value.
* <p>
* Entries are included in the returned stream if the predicate function returns true.
*
* @param predicate a predicate function applied to each key and value in the stream
* @return a stream including the entries for which the predicate function returned true
*/
public MapStream<K, V> filter(BiPredicate<? super K, ? super V> predicate) {
return wrap(underlying.filter(e -> predicate.test(e.getKey(), e.getValue())));
}
/**
* Filters the stream by applying the predicate function to each key.
* <p>
* Entries are included in the returned stream if the predicate function returns true.
*
* @param predicate a predicate function applied to each key in the stream
* @return a stream including the entries for which the predicate function returned true
*/
public MapStream<K, V> filterKeys(Predicate<? super K> predicate) {
return wrap(underlying.filter(e -> predicate.test(e.getKey())));
}
/**
* Filters the stream checking the type of each key.
* <p>
* Entries are included in the returned stream if the key is an instance of the specified type.
*
* @param <R> the type to filter to
* @param castToClass the class to filter the keys to
* @return a stream including only those entries where the key is an instance of the specified type
*/
public <R> MapStream<R, V> filterKeys(Class<R> castToClass) {
return wrap(underlying
.filter(e -> castToClass.isInstance(e.getKey()))
.map(e -> entry(castToClass.cast(e.getKey()), e.getValue())));
}
/**
* Filters the stream by applying the predicate function to each value.
* <p>
* Entries are included in the returned stream if the predicate function returns true.
*
* @param predicate a predicate function applied to each value in the stream
* @return a stream including the entries for which the predicate function returned true
*/
public MapStream<K, V> filterValues(Predicate<? super V> predicate) {
return wrap(underlying.filter(e -> predicate.test(e.getValue())));
}
/**
* Filters the stream checking the type of each value.
* <p>
* Entries are included in the returned stream if the value is an instance of the specified type.
*
* @param <R> the type to filter to
* @param castToClass the class to filter the values to
* @return a stream including only those entries where the value is an instance of the specified type
*/
public <R> MapStream<K, R> filterValues(Class<R> castToClass) {
return wrap(underlying
.filter(e -> castToClass.isInstance(e.getValue()))
.map(e -> entry(e.getKey(), castToClass.cast(e.getValue()))));
}
//-------------------------------------------------------------------------
/**
* Transforms the keys in the stream by applying a mapper function to each key.
* <p>
* The values are unchanged.
*
* @param mapper a mapper function whose return value is used as the new key
* @param <R> the type of the new keys
* @return a stream of entries with the keys transformed and the values unchanged
*/
public <R> MapStream<R, V> mapKeys(Function<? super K, ? extends R> mapper) {
return wrap(underlying.map(e -> entry(mapper.apply(e.getKey()), e.getValue())));
}
/**
* Transforms the keys in the stream by applying a mapper function to each key and value.
* <p>
* The values are unchanged.
*
* @param mapper a mapper function whose return value is used as the new key
* @param <R> the type of the new keys
* @return a stream of entries with the keys transformed and the values unchanged
*/
public <R> MapStream<R, V> mapKeys(BiFunction<? super K, ? super V, ? extends R> mapper) {
return wrap(underlying.map(e -> entry(mapper.apply(e.getKey(), e.getValue()), e.getValue())));
}
/**
* Transforms the values in the stream by applying a mapper function to each value.
* <p>
* The keys are unchanged.
*
* @param mapper a mapper function whose return value is used as the new value
* @param <R> the type of the new values
* @return a stream of entries with the values transformed and the keys unchanged
*/
public <R> MapStream<K, R> mapValues(Function<? super V, ? extends R> mapper) {
return wrap(underlying.map(e -> entry(e.getKey(), mapper.apply(e.getValue()))));
}
/**
* Transforms the values in the stream by applying a mapper function to each key and value.
* <p>
* The keys are unchanged.
*
* @param mapper a mapper function whose return value is used as the new value
* @param <R> the type of the new values
* @return a stream of entries with the values transformed and the keys unchanged
*/
public <R> MapStream<K, R> mapValues(BiFunction<? super K, ? super V, ? extends R> mapper) {
return wrap(underlying.map(e -> entry(e.getKey(), mapper.apply(e.getKey(), e.getValue()))));
}
/**
* Transforms the entries in the stream by applying a mapper function to each key and value.
* <p>
* The result of this method is a {@code MapStream}, unlike {@link #map(BiFunction)}.
*
* @param mapper a mapper function whose return value is the new key-value entry
* @param <RK> the type of the new keys
* @param <RV> the type of the new values
* @return a stream of entries with the keys and values transformed
*/
public <RK, RV> MapStream<RK, RV> mapBoth(BiFunction<? super K, ? super V, Entry<RK, RV>> mapper) {
return wrap(underlying.map(e -> mapper.apply(e.getKey(), e.getValue())));
}
/**
* Transforms the entries in the stream by applying a mapper function to each key and value.
*
* @param mapper a mapper function whose return values are included in the new stream
* @param <R> the type of elements in the new stream
* @return a stream containing the values returned from the mapper function
*/
public <R> Stream<R> map(BiFunction<? super K, ? super V, ? extends R> mapper) {
return underlying.map(e -> mapper.apply(e.getKey(), e.getValue()));
}
/**
* Transforms the entries in the stream to doubles by applying a mapper function to each key and value.
*
* @param mapper a mapper function whose return values are included in the new stream
* @return a stream containing the double values returned from the mapper function
*/
public DoubleStream mapToDouble(ToDoubleBiFunction<? super K, ? super V> mapper) {
return underlying.mapToDouble(e -> mapper.applyAsDouble(e.getKey(), e.getValue()));
}
/**
* Transforms the entries in the stream to integers by applying a mapper function to each key and value.
*
* @param mapper a mapper function whose return values are included in the new stream
* @return a stream containing the integer values returned from the mapper function
*/
public IntStream mapToInt(ToIntBiFunction<? super K, ? super V> mapper) {
return underlying.mapToInt(e -> mapper.applyAsInt(e.getKey(), e.getValue()));
}
//-------------------------------------------------------------------------
/**
* Transforms the keys in the stream by applying a mapper function to each key.
* <p>
* The new keys produced will be associated with the original value.
*
* @param mapper a mapper function whose return values are the keys in the new stream
* @param <R> the type of the new keys
* @return a stream of entries with new keys from the mapper function assigned to the values
*/
public <R> MapStream<R, V> flatMapKeys(Function<? super K, Stream<R>> mapper) {
return wrap(underlying.flatMap(e -> mapper.apply(e.getKey()).map(newKey -> entry(newKey, e.getValue()))));
}
/**
* Transforms the keys in the stream by applying a mapper function to each key and value.
* <p>
* The new keys produced will be associated with the original value.
* <p>
* For example this could turn a {@code MapStream<List<String>, LocalDate>} into a
* {@code MapStream<String, LocalDate>}
*
* @param mapper a mapper function whose return values are the keys in the new stream
* @param <R> the type of the new keys
* @return a stream of entries with new keys from the mapper function assigned to the values
*/
public <R> MapStream<R, V> flatMapKeys(BiFunction<? super K, ? super V, Stream<R>> mapper) {
return wrap(underlying
.flatMap(e -> mapper.apply(e.getKey(), e.getValue()).map(newKey -> entry(newKey, e.getValue()))));
}
/**
* Transforms the values in the stream by applying a mapper function to each value.
* <p>
* The new values produced will be associated with the original key.
* <p>
* For example this could turn a {@code MapStream<LocalDate, List<String>>} into a
* {@code MapStream<LocalDate, String>}
*
* @param mapper a mapper function whose return values are the values in the new stream
* @param <R> the type of the new values
* @return a stream of entries with new values from the mapper function assigned to the keys
*/
public <R> MapStream<K, R> flatMapValues(Function<? super V, Stream<R>> mapper) {
return wrap(underlying.flatMap(e -> mapper.apply(e.getValue()).map(newValue -> entry(e.getKey(), newValue))));
}
/**
* Transforms the values in the stream by applying a mapper function to each key and value.
* <p>
* The new values produced will be associated with the original key.
* <p>
* For example this could turn a {@code MapStream<LocalDate, List<String>>} into a
* {@code MapStream<LocalDate, String>}
*
* @param mapper a mapper function whose return values are the values in the new stream
* @param <R> the type of the new values
* @return a stream of entries with new values from the mapper function assigned to the keys
*/
public <R> MapStream<K, R> flatMapValues(BiFunction<? super K, ? super V, Stream<R>> mapper) {
return wrap(underlying
.flatMap(e -> mapper.apply(e.getKey(), e.getValue()).map(newValue -> entry(e.getKey(), newValue))));
}
/**
* Transforms the entries in the stream by applying a mapper function to each key and value to produce a stream of
* elements, and then flattening the resulting stream of streams.
*
* @param mapper a mapper function whose return values are included in the new stream
* @param <R> the type of the elements in the new stream
* @return a stream containing the values returned from the mapper function
*/
public <R> Stream<R> flatMap(BiFunction<? super K, ? super V, Stream<R>> mapper) {
return underlying.flatMap(e -> mapper.apply(e.getKey(), e.getValue()));
}
/**
* Transforms the entries in the stream to doubles by applying a mapper function to each key and value to produce
* a stream of doubles, and then flattening the resulting stream of streams.
*
* @param mapper a mapper function whose return values are included in the new stream
* @return a stream containing the double values returned from the mapper function
*/
public DoubleStream flatMapToDouble(BiFunction<? super K, ? super V, ? extends DoubleStream> mapper) {
return underlying.flatMapToDouble(e -> mapper.apply(e.getKey(), e.getValue()));
}
/**
* Transforms the entries in the stream to integers by applying a mapper function to each key and value to produce
* a stream of integers, and then flattening the resulting stream of streams.
*
* @param mapper a mapper function whose return values are included in the new stream
* @return a stream containing the integer values returned from the mapper function
*/
public IntStream flatMapToInt(BiFunction<? super K, ? super V, ? extends IntStream> mapper) {
return underlying.flatMapToInt(e -> mapper.apply(e.getKey(), e.getValue()));
}
//-----------------------------------------------------------------------
/**
* Sorts the entries in the stream by comparing the keys using their natural ordering.
* <p>
* If the keys in this map stream are not {@code Comparable} a {@code java.lang.ClassCastException} may be thrown.
* In this case use {@link #sortedKeys(Comparator)} instead.
*
* @return the sorted stream
*/
@SuppressWarnings("unchecked")
public MapStream<K, V> sortedKeys() {
Comparator<K> comparator = (Comparator<K>) Comparator.naturalOrder();
return wrap(underlying.sorted((e1, e2) -> comparator.compare(e1.getKey(), e2.getKey())));
}
/**
* Sorts the entries in the stream by comparing the keys using the supplied comparator.
*
* @param comparator a comparator of keys
* @return the sorted stream
*/
public MapStream<K, V> sortedKeys(Comparator<? super K> comparator) {
return wrap(underlying.sorted((e1, e2) -> comparator.compare(e1.getKey(), e2.getKey())));
}
/**
* Sorts the entries in the stream by comparing the values using their natural ordering.
* <p>
* If the values in this map stream are not {@code Comparable} a {@code java.lang.ClassCastException} may be thrown.
* In this case use {@link #sortedValues(Comparator)} instead.
*
* @return the sorted stream
*/
@SuppressWarnings("unchecked")
public MapStream<K, V> sortedValues() {
Comparator<V> comparator = (Comparator<V>) Comparator.naturalOrder();
return wrap(underlying.sorted((e1, e2) -> comparator.compare(e1.getValue(), e2.getValue())));
}
/**
* Sorts the entries in the stream by comparing the values using the supplied comparator.
*
* @param comparator a comparator of values
* @return the sorted stream
*/
public MapStream<K, V> sortedValues(Comparator<? super V> comparator) {
return wrap(underlying.sorted((e1, e2) -> comparator.compare(e1.getValue(), e2.getValue())));
}
//-----------------------------------------------------------------------
/**
* Finds the minimum entry in the stream by comparing the keys using the supplied comparator.
* <p>
* This is a terminal operation.
*
* @param comparator a comparator of keys
* @return the minimum entry
*/
public Optional<Map.Entry<K, V>> minKeys(Comparator<? super K> comparator) {
return underlying.min((e1, e2) -> comparator.compare(e1.getKey(), e2.getKey()));
}
/**
* Finds the minimum entry in the stream by comparing the values using the supplied comparator.
* <p>
* This is a terminal operation.
*
* @param comparator a comparator of values
* @return the minimum entry
*/
public Optional<Map.Entry<K, V>> minValues(Comparator<? super V> comparator) {
return underlying.min((e1, e2) -> comparator.compare(e1.getValue(), e2.getValue()));
}
/**
* Finds the maximum entry in the stream by comparing the keys using the supplied comparator.
* <p>
* This is a terminal operation.
*
* @param comparator a comparator of keys
* @return the maximum entry
*/
public Optional<Map.Entry<K, V>> maxKeys(Comparator<? super K> comparator) {
return underlying.max((e1, e2) -> comparator.compare(e1.getKey(), e2.getKey()));
}
/**
* Finds the maximum entry in the stream by comparing the values using the supplied comparator.
* <p>
* This is a terminal operation.
*
* @param comparator a comparator of values
* @return the maximum entry
*/
public Optional<Map.Entry<K, V>> maxValues(Comparator<? super V> comparator) {
return underlying.max((e1, e2) -> comparator.compare(e1.getValue(), e2.getValue()));
}
//-----------------------------------------------------------------------
/**
* Returns whether any elements of this stream match the provided predicate.
* <p>
* This is a short-circuiting terminal operation.
*
* @param predicate the predicate to apply to the entries
* @return whether any of the entries matched the predicate
*/
public boolean anyMatch(BiPredicate<? super K, ? super V> predicate) {
return underlying.anyMatch(e -> predicate.test(e.getKey(), e.getValue()));
}
/**
* Returns whether all elements of this stream match the provided predicate.
* <p>
* This is a short-circuiting terminal operation.
*
* @param predicate the predicate to apply to the entries
* @return whether all of the entries matched the predicate
*/
public boolean allMatch(BiPredicate<? super K, ? super V> predicate) {
return underlying.allMatch(e -> predicate.test(e.getKey(), e.getValue()));
}
/**
* Returns whether no elements of this stream match the provided predicate.
* <p>
* This is a short-circuiting terminal operation.
*
* @param predicate the predicate to apply to the entries
* @return whether none of the entries matched the predicate
*/
public boolean noneMatch(BiPredicate<? super K, ? super V> predicate) {
return underlying.noneMatch(e -> predicate.test(e.getKey(), e.getValue()));
}
//-------------------------------------------------------------------------
/**
* Returns an immutable map built from the entries in the stream.
* <p>
* The keys must be unique or an exception will be thrown.
* Duplicate keys can be handled using {@link #toMap(BiFunction)}.
* <p>
* This is a terminal operation.
*
* @return an immutable map built from the entries in the stream
* @throws IllegalArgumentException if the same key occurs more than once
*/
public ImmutableMap<K, V> toMap() {
return underlying.collect(Guavate.toImmutableMap(Entry::getKey, Entry::getValue));
}
/**
* Returns an immutable map built from the entries in the stream.
* <p>
* If the same key maps to multiple values the merge function is invoked with both values and the return
* value is used in the map.
* <p>
* Can be used with {@link #concat(MapStream, MapStream)} to merge immutable
* maps with duplicate keys.
* <p>
* For example, to merge immutable maps with duplicate keys preferring values in the first map:
* <pre>
* MapStream.concat(mapStreamA, mapStreamB).toMap((a,b) -> a);
* </pre>
* <p>
* This is a terminal operation.
*
* @param mergeFn function used to merge values when the same key appears multiple times in the stream
* @return an immutable map built from the entries in the stream
*/
public ImmutableMap<K, V> toMap(BiFunction<? super V, ? super V, ? extends V> mergeFn) {
return underlying.collect(Guavate.toImmutableMap(Entry::getKey, Entry::getValue, mergeFn));
}
//-------------------------------------------------------------------------
/**
* Returns an immutable map built from the entries in the stream, grouping by key.
* <p>
* Entries are grouped based on the equality of the key.
* <p>
* This is a terminal operation.
*
* @return an immutable map built from the entries in the stream
*/
public ImmutableMap<K, List<V>> toMapGrouping() {
return toMapGrouping(toList());
}
/**
* Returns a stream built from a map of the entries in the stream, grouped by key.
* <p>
* Entries are grouped based on the equality of the key.
*
* @return a stream where the values have been grouped
*/
public MapStream<K, List<V>> groupingAndThen() {
return MapStream.of(toMapGrouping());
}
/**
* Returns an immutable map built from the entries in the stream, grouping by key.
* <p>
* Entries are grouped based on the equality of the key.
* The collector allows the values to be flexibly combined.
* <p>
* This is a terminal operation.
*
* @param <A> the internal collector type
* @param <R> the type of the combined values
* @param valueCollector the collector used to combined the values
* @return an immutable map built from the entries in the stream
*/
public <A, R> ImmutableMap<K, R> toMapGrouping(Collector<? super V, A, R> valueCollector) {
return underlying.collect(collectingAndThen(
groupingBy(Entry::getKey, LinkedHashMap::new, mapping(Entry::getValue, valueCollector)),
ImmutableMap::copyOf));
}
/**
* Returns a stream built from a map of the entries in the stream, grouped by key.
* <p>
* Entries are grouped based on the equality of the key.
* The collector allows the values to be flexibly combined.
*
* @param <A> the internal collector type
* @param <R> the type of the combined values
* @param valueCollector the collector used to combined the values
* @return a stream where the values have been grouped
*/
public <A, R> MapStream<K, R> groupingAndThen(Collector<? super V, A, R> valueCollector) {
return MapStream.of(toMapGrouping(valueCollector));
}
//-------------------------------------------------------------------------
/**
* Returns an immutable list multimap built from the entries in the stream.
* <p>
* This is a terminal operation.
*
* @return an immutable list multimap built from the entries in the stream
*/
public ImmutableListMultimap<K, V> toListMultimap() {
return underlying.collect(Guavate.toImmutableListMultimap(Entry::getKey, Entry::getValue));
}
/**
* Returns an immutable set multimap built from the entries in the stream.
* <p>
* This is a terminal operation.
*
* @return an immutable set multimap built from the entries in the stream
*/
public ImmutableSetMultimap<K, V> toSetMultimap() {
return underlying.collect(Guavate.toImmutableSetMultimap(Entry::getKey, Entry::getValue));
}
/**
* Performs an action for each entry in the stream, passing the key and value to the action.
* <p>
* This is a terminal operation.
*
* @param action an action performed for each entry in the stream
*/
public void forEach(BiConsumer<? super K, ? super V> action) {
underlying.forEach(e -> action.accept(e.getKey(), e.getValue()));
}
//-------------------------------------------------------------------------
@Override
public MapStream<K, V> filter(Predicate<? super Map.Entry<K, V>> predicate) {
return wrap(underlying.filter(predicate));
}
@Override
public <R> Stream<R> map(Function<? super Map.Entry<K, V>, ? extends R> mapper) {
return underlying.map(mapper);
}
@Override
public IntStream mapToInt(ToIntFunction<? super Map.Entry<K, V>> mapper) {
return underlying.mapToInt(mapper);
}
@Override
public LongStream mapToLong(ToLongFunction<? super Map.Entry<K, V>> mapper) {
return underlying.mapToLong(mapper);
}
@Override
public DoubleStream mapToDouble(ToDoubleFunction<? super Map.Entry<K, V>> mapper) {
return underlying.mapToDouble(mapper);
}
@Override
public <R> Stream<R> flatMap(Function<? super Map.Entry<K, V>, ? extends Stream<? extends R>> mapper) {
return underlying.flatMap(mapper);
}
@Override
public IntStream flatMapToInt(Function<? super Map.Entry<K, V>, ? extends IntStream> mapper) {
return underlying.flatMapToInt(mapper);
}
@Override
public LongStream flatMapToLong(Function<? super Map.Entry<K, V>, ? extends LongStream> mapper) {
return underlying.flatMapToLong(mapper);
}
@Override
public DoubleStream flatMapToDouble(Function<? super Map.Entry<K, V>, ? extends DoubleStream> mapper) {
return underlying.flatMapToDouble(mapper);
}
@Override
public MapStream<K, V> distinct() {
return wrap(underlying.distinct());
}
@Override
public MapStream<K, V> sorted() {
return wrap(underlying.sorted());
}
@Override
public MapStream<K, V> sorted(Comparator<? super Map.Entry<K, V>> comparator) {
return wrap(underlying.sorted(comparator));
}
@Override
public MapStream<K, V> peek(Consumer<? super Map.Entry<K, V>> action) {
return wrap(underlying.peek(action));
}
@Override
public MapStream<K, V> limit(long maxSize) {
return wrap(underlying.limit(maxSize));
}
@Override
public MapStream<K, V> skip(long n) {
return wrap(underlying.skip(n));
}
@Override
public void forEach(Consumer<? super Map.Entry<K, V>> action) {
underlying.forEach(action);
}
@Override
public void forEachOrdered(Consumer<? super Map.Entry<K, V>> action) {
underlying.forEachOrdered(action);
}
@Override
public Object[] toArray() {
return underlying.toArray();
}
@Override
public <A> A[] toArray(IntFunction<A[]> generator) {
return underlying.toArray(generator);
}
@Override
public Map.Entry<K, V> reduce(Map.Entry<K, V> identity, BinaryOperator<Map.Entry<K, V>> accumulator) {
return underlying.reduce(identity, accumulator);
}
@Override
public Optional<Map.Entry<K, V>> reduce(BinaryOperator<Map.Entry<K, V>> accumulator) {
return underlying.reduce(accumulator);
}
@Override
public <U> U reduce(U identity, BiFunction<U, ? super Map.Entry<K, V>, U> accumulator, BinaryOperator<U> combiner) {
return underlying.reduce(identity, accumulator, combiner);
}
@Override
public <R> R collect(
Supplier<R> supplier,
BiConsumer<R, ? super Map.Entry<K, V>> accumulator,
BiConsumer<R, R> combiner) {
return underlying.collect(supplier, accumulator, combiner);
}
@Override
public <R, A> R collect(Collector<? super Map.Entry<K, V>, A, R> collector) {
return underlying.collect(collector);
}
@Override
public Optional<Map.Entry<K, V>> min(Comparator<? super Map.Entry<K, V>> comparator) {
return underlying.min(comparator);
}
@Override
public Optional<Map.Entry<K, V>> max(Comparator<? super Map.Entry<K, V>> comparator) {
return underlying.max(comparator);
}
@Override
public long count() {
return underlying.count();
}
@Override
public boolean anyMatch(Predicate<? super Map.Entry<K, V>> predicate) {
return underlying.anyMatch(predicate);
}
@Override
public boolean allMatch(Predicate<? super Map.Entry<K, V>> predicate) {
return underlying.allMatch(predicate);
}
@Override
public boolean noneMatch(Predicate<? super Map.Entry<K, V>> predicate) {
return underlying.noneMatch(predicate);
}
@Override
public Optional<Map.Entry<K, V>> findFirst() {
return underlying.findFirst();
}
@Override
public Optional<Map.Entry<K, V>> findAny() {
return underlying.findAny();
}
@Override
public Iterator<Map.Entry<K, V>> iterator() {
return underlying.iterator();
}
@Override
public Spliterator<Map.Entry<K, V>> spliterator() {
return underlying.spliterator();
}
@Override
public boolean isParallel() {
return underlying.isParallel();
}
@Override
public MapStream<K, V> sequential() {
return wrap(underlying.sequential());
}
@Override
public MapStream<K, V> parallel() {
return wrap(underlying.parallel());
}