Kod do wpisu o ciekawych zastosowaniach streamów w Javie. Post znajduje się pod adresem https://blog.mloza.pl/ciekawe-przyklady-zastosowania-streamow-w-javie/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Fibonacci { | |
private static class FibonacciGenerator { | |
private BigInteger nMinusTwo = BigInteger.ONE; | |
private BigInteger nMinusOne = BigInteger.ZERO; | |
public BigInteger next() { | |
final BigInteger n = nMinusTwo.add(nMinusOne); | |
nMinusTwo = nMinusOne; | |
nMinusOne = n; | |
return n; | |
} | |
} | |
public static void main(String[] args) { | |
BigInteger n = new BigInteger("10"); | |
BigInteger result = Stream.generate(new FibonacciGenerator()::next) | |
.limit(n.longValueExact()) | |
.reduce(BigInteger::add) | |
.get(); | |
System.out.println(result); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
String s1 = "przykładowy ciąg znaków"; | |
Map<String, Long> map = Arrays.stream(s1.split("")) | |
.collect(Collectors.groupingBy(Function.identity(), Collectors.counting())); | |
System.out.println(map); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment