Serializing a lambda

0 votes

I have the below code, which throws a NotSerializableException when I try to serialize the lambda.

public static void main(String[] args) throws Exception {
    File myFile = Files.createTempFile("lambda", "ser").toFile();
    try (ObjectOutput oo = new ObjectOutputStream(new FileOutputStream(myFile))) {
        Runnable r = () -> System.out.println("Can I be serialized?");

    try (ObjectInput oi = new ObjectInputStream(new FileInputStream(myFile))) {
        Runnable  r = (Runnable) oi.readObject();;

I don't want to create a SerializableRunnable "dummy" interface for this. Please help!

Nov 16, 2018 in Java by 93.lynn
• 1,610 points

1 answer to this question.

0 votes

With the release of Java 8, Oracle introduced the lambda expressions. It lets you cast an object to an intersection of types by adding multiple bounds. In the case of serialization, it is therefore possible to write:

Runnable r = (Runnable & Serializable)() -> System.out.println("Serializable!");

And the lambda expression automatically becomes serializable.

answered Nov 16, 2018 by code.reaper12
• 3,500 points

Related Questions In Java

0 votes
1 answer

How to automatically deploy a jar from s3 to aws lambda?

You could probably create a lambda function ...READ MORE

answered Apr 2, 2020 in Java by Jake
0 votes
2 answers

How can I convert a String variable to a primitive int in Java

 Here are two ways illustrating this: Integer x ...READ MORE

answered Aug 20, 2019 in Java by Sirajul
• 59,090 points
0 votes
1 answer

How to divide a string in two parts

String s="yourstring"; boolean flag = true; for(int i=0;i<s.length();i++) { ...READ MORE

answered Apr 13, 2018 in Java by Rishabh
• 3,620 points
0 votes
2 answers

How to create a 2-D array in java?

int[][] multi = new int[5][]; multi[0] = new ...READ MORE

answered Jul 16, 2018 in Java by Daisy
• 8,110 points
0 votes
1 answer

How are Java lambda functions compiled?

For Lambda expressions, the compiler doesn’t translate ...READ MORE

answered Mar 6, 2019 in Cloud Computing by EdurekaUser
• 1,500 points
+5 votes
4 answers

How to execute a python file with few arguments in java?

You can use Java Runtime.exec() to run python script, ...READ MORE

answered Mar 27, 2018 in Java by DragonLord999
• 8,450 points

edited Nov 7, 2018 by Omkar 49,975 views
+1 vote
1 answer

How to handle drop downs using Selenium WebDriver in Java

First, find an XPath which will return ...READ MORE

answered Mar 27, 2018 in Selenium by nsv999
• 5,500 points
0 votes
1 answer

What are the differences between getText() and getAttribute() functions in Selenium WebDriver?

See, both are used to retrieve something ...READ MORE

answered Apr 5, 2018 in Selenium by nsv999
• 5,500 points
0 votes
1 answer

How to remove ‘char’ from a String? you can try using the overloaded ...READ MORE

answered May 22, 2018 in Java by code.reaper12
• 3,500 points
0 votes
1 answer

How to download a file from spring controllers?

@RequestMapping(value = "/files/{file_name}", method = RequestMethod.GET) public void ...READ MORE

answered Sep 4, 2018 in Java by code.reaper12
• 3,500 points