Comments (4)
You gave me an idea to implement findIndexed(Predicate<T>)
operator. It will return Optional<IntPair<T>>
with an element and its index.
Predicate<String> predicate = "sam"::equalsIgnoreCase;
int index = Stream.of("Peter", "John", "Sam", "Kate")
.findIndexed(predicate)
.ifPresent(p -> System.out.println(p.getFirst()));
from lightweight-stream-api.
I think it is helpful only for lists and arrays. If you use any intermediate operator before this function, the index will be different and useless.
List<Integer> list = Arrays.asList(0, 1, 2, 3, 4, 5, 6, 7, 8);
int index = Stream.of(list)
.filter(i -> i % 2 == 0)
.contains(6);
// index = 3
In this example it is more reasonable to collect stream to list first, and then call contains
on the result. Then the index can be used to access list items.
However, you can use reduceIndexed
or custom operator.
Predicate<String> p = "sam"::equalsIgnoreCase;
int index = Stream.of("Peter", "John", "Sam", "Kate")
.reduceIndexed(-1, (i, acc, value) ->
(acc >= 0) ? acc : (p.test(value) ? i : acc));
System.out.println(index); // 2
int index2 = Stream.of("Peter", "John", "Sam", "Kate")
.custom(new ContainsOperator<>(p));
private static class ContainsOperator<T> implements Function<Stream<T>, Integer> {
private final Predicate<? super T> predicate;
public ContainsOperator(Predicate<? super T> predicate) {
this.predicate = predicate;
}
@Override
public Integer apply(Stream<T> stream) {
IndexedIterator<? extends T> iterator = new IndexedIterator<T>(stream.iterator());
while (iterator.hasNext()) {
int index = iterator.getIndex();
if (predicate.test(iterator.next())) {
return index;
}
}
return -1;
}
}
from lightweight-stream-api.
Thanks for the explanation. I forgot that intermediate operations can reduce Stream
length and break indices
You can close this issue if it is not actual feature for implementation in your library
from lightweight-stream-api.
findIndexed
implemented with #114
from lightweight-stream-api.
Related Issues (20)
- Make Stream.toList() as ArrayList instead of List HOT 7
- Samples using Stream on Android SDK 21+ HOT 1
- Sorting by date as descending HOT 2
- Add sonarcloud integration HOT 7
- Why does Optional.of(List<String).stream() return a stream with a single element, the List<String> instead of a Stream of individual elements? HOT 2
- Not maintaining the order while groupBy HOT 2
- Add nullability annotations for better Kotlin support HOT 2
- Add teeing collector HOT 1
- 0.0% Coverage on New Code is less than 80.0% HOT 6
- How to get map return HOT 2
- may supply a new method Stream.findFirstOrElse HOT 1
- may supply some method Stream.concat, Stream.append, Stream.prepend HOT 1
- BUG: function not work as java9 standard HOT 1
- ComparatorCompat strange behaviour, possible bug HOT 1
- Make java 11 as a minimum version HOT 6
- v 1.2.2 broke proguard HOT 1
- NoSuchElementException
- NoSuchElementException with generate and anyMatch in v1.2.2 HOT 1
- New mapMulti method.
- Increase code coverage for Compat class HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from lightweight-stream-api.