Skip to content

Instantly share code, notes, and snippets.

@shelajev

shelajev/Main.java

Created Jan 19, 2021
Embed
What would you like to do?
import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.util.List; import java.util.stream.Collectors; import java.util.stream.Stream;
public class Main {
private static String filename = "fib1000";
static Stream<Long> fibonacciStream() {
return Stream.iterate(new long[]{1, 0}, (f) -> new long[]{f[0] + f[1], f[0]}).map(f -> f[0]);
}
public static void main(String[] args) throws Exception {
List<Long> fib1000 = fibonacciStream().limit(1000).collect(Collectors.toList());
try (ObjectOutputStream oss = new ObjectOutputStream(new FileOutputStream(filename))) {
oss.writeObject(fib1000);
}
Object deserializedFib1000;
try (ObjectInputStream oss = new ObjectInputStream(new FileInputStream(filename))) {
deserializedFib1000 = oss.readObject();
}
System.out.println(fib1000.equals(deserializedFib1000));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment