Skip to content

Instantly share code, notes, and snippets.

@mloza
Last active April 15, 2020 09:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mloza/42345bd0b2d7bac0423bc65e7767f76c to your computer and use it in GitHub Desktop.
Save mloza/42345bd0b2d7bac0423bc65e7767f76c to your computer and use it in GitHub Desktop.
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/
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);
}
}
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