Java Streams Api, finite stream [duplicate]











up vote
1
down vote

favorite
1













This question already has an answer here:




  • Limit a stream by a predicate

    18 answers




I'm trying to create a fibonacci sequence through the usage of Java Streams API. I've create a supplier, but I want it to stop at a specific value (e.g 1000000).



The suplier:



import java.util.function.Supplier;

public class FibonacciSupplier implements Supplier<Integer> {

private int current;
private int next;

public FibonacciSupplier() {
current = 0;
next = 1;
}

@Override
public Integer get() {
int result = current;
current = next + current;
next = result;
return result;
}
}


How I would like it to be:



Stream.generate(new FibonacciSupplier()).maxValue(1000000);


The maxValue does not exist as a function, I'm using it as a name to for context.










share|improve this question













marked as duplicate by Michael java
Users with the  java badge can single-handedly close java questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 7 at 17:45


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.















  • Have you tried using an if statement? if (value > 1000000) { dontDo(); }?
    – Frontear
    Nov 7 at 17:29






  • 1




    limit might be what you need. Is that what you need?
    – Sweeper
    Nov 7 at 17:29

















up vote
1
down vote

favorite
1













This question already has an answer here:




  • Limit a stream by a predicate

    18 answers




I'm trying to create a fibonacci sequence through the usage of Java Streams API. I've create a supplier, but I want it to stop at a specific value (e.g 1000000).



The suplier:



import java.util.function.Supplier;

public class FibonacciSupplier implements Supplier<Integer> {

private int current;
private int next;

public FibonacciSupplier() {
current = 0;
next = 1;
}

@Override
public Integer get() {
int result = current;
current = next + current;
next = result;
return result;
}
}


How I would like it to be:



Stream.generate(new FibonacciSupplier()).maxValue(1000000);


The maxValue does not exist as a function, I'm using it as a name to for context.










share|improve this question













marked as duplicate by Michael java
Users with the  java badge can single-handedly close java questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 7 at 17:45


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.















  • Have you tried using an if statement? if (value > 1000000) { dontDo(); }?
    – Frontear
    Nov 7 at 17:29






  • 1




    limit might be what you need. Is that what you need?
    – Sweeper
    Nov 7 at 17:29















up vote
1
down vote

favorite
1









up vote
1
down vote

favorite
1






1






This question already has an answer here:




  • Limit a stream by a predicate

    18 answers




I'm trying to create a fibonacci sequence through the usage of Java Streams API. I've create a supplier, but I want it to stop at a specific value (e.g 1000000).



The suplier:



import java.util.function.Supplier;

public class FibonacciSupplier implements Supplier<Integer> {

private int current;
private int next;

public FibonacciSupplier() {
current = 0;
next = 1;
}

@Override
public Integer get() {
int result = current;
current = next + current;
next = result;
return result;
}
}


How I would like it to be:



Stream.generate(new FibonacciSupplier()).maxValue(1000000);


The maxValue does not exist as a function, I'm using it as a name to for context.










share|improve this question














This question already has an answer here:




  • Limit a stream by a predicate

    18 answers




I'm trying to create a fibonacci sequence through the usage of Java Streams API. I've create a supplier, but I want it to stop at a specific value (e.g 1000000).



The suplier:



import java.util.function.Supplier;

public class FibonacciSupplier implements Supplier<Integer> {

private int current;
private int next;

public FibonacciSupplier() {
current = 0;
next = 1;
}

@Override
public Integer get() {
int result = current;
current = next + current;
next = result;
return result;
}
}


How I would like it to be:



Stream.generate(new FibonacciSupplier()).maxValue(1000000);


The maxValue does not exist as a function, I'm using it as a name to for context.





This question already has an answer here:




  • Limit a stream by a predicate

    18 answers








java java-8 java-stream






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 7 at 17:28









Niku Hysa

147110




147110




marked as duplicate by Michael java
Users with the  java badge can single-handedly close java questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 7 at 17:45


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.






marked as duplicate by Michael java
Users with the  java badge can single-handedly close java questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 7 at 17:45


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.














  • Have you tried using an if statement? if (value > 1000000) { dontDo(); }?
    – Frontear
    Nov 7 at 17:29






  • 1




    limit might be what you need. Is that what you need?
    – Sweeper
    Nov 7 at 17:29




















  • Have you tried using an if statement? if (value > 1000000) { dontDo(); }?
    – Frontear
    Nov 7 at 17:29






  • 1




    limit might be what you need. Is that what you need?
    – Sweeper
    Nov 7 at 17:29


















Have you tried using an if statement? if (value > 1000000) { dontDo(); }?
– Frontear
Nov 7 at 17:29




Have you tried using an if statement? if (value > 1000000) { dontDo(); }?
– Frontear
Nov 7 at 17:29




1




1




limit might be what you need. Is that what you need?
– Sweeper
Nov 7 at 17:29






limit might be what you need. Is that what you need?
– Sweeper
Nov 7 at 17:29














2 Answers
2






active

oldest

votes

















up vote
3
down vote



accepted










The best solution is to use takeWhile, which is available in Java 9+:



Stream.generate(new FibonacciSupplier())
.takeWhile(i -> i <= 1000000) //will stop stream when value exceeds given limit
.forEach(System.out::println);


If you're using Java 8, then you may want to look into hacks for stopping an infinite stream






share|improve this answer

















  • 3




    If you knew that question existed, why didn't you close this as a dupe?
    – Michael
    Nov 7 at 17:45


















up vote
0
down vote













You'll need to use the .limit() method on the stream as below :



Stream.generate(new FibonacciSupplier()).limit(1000).forEach(System.out::println);


From the docs,




Returns a stream consisting of the elements of this stream, truncated
to be no longer than {@code maxSize} in length.







share|improve this answer





















  • takeWhile from java9 will do the job
    – kofemann
    Nov 7 at 17:46








  • 1




    Eh... Yes and no. limit limits by the number of elements. He wants to stop once the sequence has exceeded a certain value. How do you know that a fib number of 1000000 is element 1000?
    – Michael
    Nov 7 at 17:48


















2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
3
down vote



accepted










The best solution is to use takeWhile, which is available in Java 9+:



Stream.generate(new FibonacciSupplier())
.takeWhile(i -> i <= 1000000) //will stop stream when value exceeds given limit
.forEach(System.out::println);


If you're using Java 8, then you may want to look into hacks for stopping an infinite stream






share|improve this answer

















  • 3




    If you knew that question existed, why didn't you close this as a dupe?
    – Michael
    Nov 7 at 17:45















up vote
3
down vote



accepted










The best solution is to use takeWhile, which is available in Java 9+:



Stream.generate(new FibonacciSupplier())
.takeWhile(i -> i <= 1000000) //will stop stream when value exceeds given limit
.forEach(System.out::println);


If you're using Java 8, then you may want to look into hacks for stopping an infinite stream






share|improve this answer

















  • 3




    If you knew that question existed, why didn't you close this as a dupe?
    – Michael
    Nov 7 at 17:45













up vote
3
down vote



accepted







up vote
3
down vote



accepted






The best solution is to use takeWhile, which is available in Java 9+:



Stream.generate(new FibonacciSupplier())
.takeWhile(i -> i <= 1000000) //will stop stream when value exceeds given limit
.forEach(System.out::println);


If you're using Java 8, then you may want to look into hacks for stopping an infinite stream






share|improve this answer












The best solution is to use takeWhile, which is available in Java 9+:



Stream.generate(new FibonacciSupplier())
.takeWhile(i -> i <= 1000000) //will stop stream when value exceeds given limit
.forEach(System.out::println);


If you're using Java 8, then you may want to look into hacks for stopping an infinite stream







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 7 at 17:43









ernest_k

18k41837




18k41837








  • 3




    If you knew that question existed, why didn't you close this as a dupe?
    – Michael
    Nov 7 at 17:45














  • 3




    If you knew that question existed, why didn't you close this as a dupe?
    – Michael
    Nov 7 at 17:45








3




3




If you knew that question existed, why didn't you close this as a dupe?
– Michael
Nov 7 at 17:45




If you knew that question existed, why didn't you close this as a dupe?
– Michael
Nov 7 at 17:45












up vote
0
down vote













You'll need to use the .limit() method on the stream as below :



Stream.generate(new FibonacciSupplier()).limit(1000).forEach(System.out::println);


From the docs,




Returns a stream consisting of the elements of this stream, truncated
to be no longer than {@code maxSize} in length.







share|improve this answer





















  • takeWhile from java9 will do the job
    – kofemann
    Nov 7 at 17:46








  • 1




    Eh... Yes and no. limit limits by the number of elements. He wants to stop once the sequence has exceeded a certain value. How do you know that a fib number of 1000000 is element 1000?
    – Michael
    Nov 7 at 17:48















up vote
0
down vote













You'll need to use the .limit() method on the stream as below :



Stream.generate(new FibonacciSupplier()).limit(1000).forEach(System.out::println);


From the docs,




Returns a stream consisting of the elements of this stream, truncated
to be no longer than {@code maxSize} in length.







share|improve this answer





















  • takeWhile from java9 will do the job
    – kofemann
    Nov 7 at 17:46








  • 1




    Eh... Yes and no. limit limits by the number of elements. He wants to stop once the sequence has exceeded a certain value. How do you know that a fib number of 1000000 is element 1000?
    – Michael
    Nov 7 at 17:48













up vote
0
down vote










up vote
0
down vote









You'll need to use the .limit() method on the stream as below :



Stream.generate(new FibonacciSupplier()).limit(1000).forEach(System.out::println);


From the docs,




Returns a stream consisting of the elements of this stream, truncated
to be no longer than {@code maxSize} in length.







share|improve this answer












You'll need to use the .limit() method on the stream as below :



Stream.generate(new FibonacciSupplier()).limit(1000).forEach(System.out::println);


From the docs,




Returns a stream consisting of the elements of this stream, truncated
to be no longer than {@code maxSize} in length.








share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 7 at 17:34









Nicholas K

4,04031029




4,04031029












  • takeWhile from java9 will do the job
    – kofemann
    Nov 7 at 17:46








  • 1




    Eh... Yes and no. limit limits by the number of elements. He wants to stop once the sequence has exceeded a certain value. How do you know that a fib number of 1000000 is element 1000?
    – Michael
    Nov 7 at 17:48


















  • takeWhile from java9 will do the job
    – kofemann
    Nov 7 at 17:46








  • 1




    Eh... Yes and no. limit limits by the number of elements. He wants to stop once the sequence has exceeded a certain value. How do you know that a fib number of 1000000 is element 1000?
    – Michael
    Nov 7 at 17:48
















takeWhile from java9 will do the job
– kofemann
Nov 7 at 17:46






takeWhile from java9 will do the job
– kofemann
Nov 7 at 17:46






1




1




Eh... Yes and no. limit limits by the number of elements. He wants to stop once the sequence has exceeded a certain value. How do you know that a fib number of 1000000 is element 1000?
– Michael
Nov 7 at 17:48




Eh... Yes and no. limit limits by the number of elements. He wants to stop once the sequence has exceeded a certain value. How do you know that a fib number of 1000000 is element 1000?
– Michael
Nov 7 at 17:48



這個網誌中的熱門文章

Tangent Lines Diagram Along Smooth Curve

Yusuf al-Mu'taman ibn Hud

Zucchini