Reputation: 3294
I'm trying to get grips on the new java8 streams.
I need to generate an infinite stream of tuples with certain characteristics:
The tuples will have three int
values, a
, b
and c
.
where b
and c
are simply counters, that could be made in a for
loop with:
for (int c = 1;; c++) {
for (int b = 1; b < c; b++) {
...
}
}
I would then use a filter to calculate a
I created a TupleFactory
:
class TupleFactory {
private static int b = 1;
private static int c = 1;
public static Tuple next(){
if (b >= c - 1)
c++;
else
b++;
return new MyTuple (1,b,c);
}
}
and then I would like to generate the Stream<Tuple>
with:
Supplier<Tuple> anyTuple = s -> TupleFactory.next();
Stream<Tuple> result = Stream.generate(anyTuple);
I get a:
Lambda expression's signature does not match the signature of the functional interface method
get()
in the line where I define the supplier...
any clues?
Upvotes: 1
Views: 602
Reputation: 124265
Your lambda must provide body for
T get();
method in Supplier<T>
interface and handle all this method arguments. But in this case get
method doesn't have any arguments so there is no need for s
in s -> ...
. Instead of
Supplier<Tuple> anyTuple = s -> TupleFactory.next();
use
Supplier<Tuple> anyTuple = () -> TupleFactory.next();
// ^^ - means no arguments
or maybe simpler use method references
Supplier<Tuple> anyTuple = TupleFactory::next;
Upvotes: 0
Reputation: 62054
First, get rid of every instance of static
in your TupleFactory. That use of static
is nonsense.
Then, try this:
TupleFactory myFactory = new TupleFactory();
Supplier<Tuple> anyTuple = myFactory::next;
If you don't want to fix your static
s, then this should work:
Supplier<Tuple> anyTuple = TupleFactory::next;
Upvotes: 2