[JAVA] I sincerely hope for Stream # reject

Well, in reality, it's not as much as I really hope for,

List.of("a", "", "b").stream()
    .reject(s -> s.isEmpty())
    .collect(Collectors.toList());
// => ["a", "b"]

I want to write like this.

But in reality, there is no such thing ...

It is written using Stream # filter.

When I write various things ...

Returns negation in the function to implement

List.of("a", "", "b").stream()
    .filter(s -> !s.isEmpty())
    .collect(Collectors.toList());

Well, a simple implementation is not bad, but complicated conditions can get confusing.

Even under simple conditions, I'm afraid that you may miss ! At a glance.

Use Predicate # negate

List.of("a", "", "b").stream()
    .filter(((Predicate<String>) (s -> s.isEmpty())).negate())
    .collect(Collectors.toList());

//Hmmm cast annoying!
Predicate<String> emptyText = s -> s.isEmpty();
List.of("a", "", "b").stream()
    .filter(emptyText.negate())
    .collect(Collectors.toList());

The Predicate # negate itself has achieved what I really want to do, but the cast is ...: scream:

Create a method that returns a negative condition

//Returns the predicate of the negative condition of the original condition
private <E> Predicate<? super E> not(Predicate<? super E> p) {
    return p.negate();
}

Making something like this

List.of("a", "", "b").stream()
    .filter(not(s -> s.isEmpty()))
    .collect(Collectors.toList());

Yup! not bad!

However, if you use not as a utility, it will look like StreamUtils.not and the description will be long.

Wrap it!

public class StreamWrapper<T> {

    private final Stream<T> stream;

    // private constructor
    private StreamWrapper(Stream<T> src) {
        this.stream = src;
    }

    // make instance
    public static <T> StreamWrapper<T> of(Stream<T> src) {
        return new StreamWrapper(src);
    }

    // Stream#Wrap filter
    public StreamWrapper<T> filter(Predicate<T> p) {
        return this.of(stream.filter(p));
    }

    // Stream#Wrap collect
    public <R, A> R collect(Collector<T, A, R> c) {
        return stream.collect(c);
    }

    // ...A lot of other rap

    /**
     *Matches the specified predicate among the elements of this stream<b>do not do</b>Returns a stream composed of things.
     * <p>
     *This is an intermediate operation.
     *
     * @param predicate
     *A non-interfering, stateless predicate that applies to each element to determine if it should be included
     * @return new stream
     */
    public StreamWrapper<T> reject(Predicate<T> predicate) {
        return this.of(stream.filter(predicate.negate()));
    }

}

Making something like this

StreamWrapper.of(List.of("a", "", "b").stream())
    .reject(s -> s.isEmpty())
    .collect(Collectors.toList());

You did it!

(~~ Well, it doesn't take so much time for reject, but ... ~~)

Create an implementation class!

~~ JMT (Implementation / Seriously / Effort: innocent :) ~~

A rather time-consuming child Stream API

Even if you say "I want this kind of intermediate operation!", It's hard to expand.

Java9 has added takeWhile`` dropWhile ʻofNullable to make it more convenient, but it doesn't implement major features such as zip(alsoreject`), so try expanding it as needed. How is it? : yum:

Recommended Posts

I sincerely hope for Stream # reject
I searched for URL encoding
I tried node-jt400 (SQL stream)
I searched for PHP variables
Java application for beginners: stream