Operations between Java threads

I am trying to find the fastest way and less complicated way to perform some union / exclude / intersection operations on Java SE 8 threads.

I'm doing it:

Stream<String> fruitStream = Stream.of("apple", "banana", "pear", "kiwi", "orange");
Stream<String> fruitStream2 = Stream.of("orange", "kiwi", "melon", "apple", "watermelon");

//Trying to create exclude operation
fruitStream.filter(
    item -> !fruitStream2.anyMatch(item2 -> item2.equals(item)))
.forEach(System.out::println);
// Expected result: fruitStream - fruitStream2: ["banana","pear"]

I get the following exception:

java.lang.IllegalStateException: thread is already running or closed

If I could perform this operation, I could develop everything else, union, intersection, etc.

So, 2 points:

1) What am I doing wrong in this decision to get this exception?

2) Is there a less complicated way to perform operations between two threads?

Note

I want to use streams to find out about them. Do not want to convert them to arrays or lists.

+4
source share
4 answers

item -> !fruitStream2.anyMatch(item2 -> item2.equals(item)) fruitStream2. fruitStream2 fruitStream1.

, , Set contains?

Set<String> otherFruits = new HashSet<>(); // Add the fruits.
fruitStream.filter(f -> !otherFruits.contains(f)).forEach(System.out::println);
+6

, ?

Stream, fruitStream2 , fruitStream

?

Stream Set:

Stream<String> fruitStream = Stream.of("apple", "banana", "pear", "kiwi", "orange");
Set<String> fruitSet = Stream.of("orange", "kiwi", "melon", "apple", "watermelon")
    .collect(Collectors.toSet());

fruitStream.filter(item -> !fruitSet.contains(item)).forEach(System.out::println);

:

banana
pear
+4

- /, . .

Supplier<Stream<String>> fruitStream2S = () ->
        Stream.of("orange", "kiwi", "melon", "apple", "watermelon");

fruitStream.filter(item -> !fruitStream2s.get().anyMatch(item2 -> item2.equals(item)))
    .forEach(System.out::println);

.

+1

This is stupid and it won’t give you much performance, but it will fulfill your requirement of not ( explicitly ) creating arrays or lists:

public static void main(String[] args) {
    new Object() {
        Stream<String> fruitStream = Stream.of("apple", "banana", "pear", "kiwi", "orange");
        Stream<String> fruitStream2 = Stream.of("orange", "kiwi", "melon", "apple", "watermelon");
        {
            fruitStream2.forEach(f -> fruitStream = fruitStream.filter(Predicate.isEqual(f).negate()));
            fruitStream.forEach(System.out::println);
        }
    };
}

Conclusion:

banana
pear

EDIT A (slightly) simpler approach:

public static void main(String[] args) {
    Stream<String> fruitStream = Stream.of("apple", "banana", "pear", "kiwi", "orange");
    Stream.of("orange", "kiwi", "melon", "apple", "watermelon")
            .map(Predicate::isEqual)
            .reduce(Predicate::or)
            .map(Predicate::negate)
            .map(fruitStream::filter)
            .orElse(fruitStream)
            .forEach(System.out::println);
}
0
source

Source: https://habr.com/ru/post/1653323/


All Articles