Created
January 19, 2021 04:19
-
-
Save shelajev/a87adc12fcc22fee0c2195f68aeb3039 to your computer and use it in GitHub Desktop.
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
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