Correct use of Serializable with lambda functions I'm practicing saving and reading serializable objects when a friend told me serializable objects have no use in real life, that I'd better focus on JSON or XML to serialize my objects when I came across this situation:
I want to preload some FileFilters to use them in a JFileChooser dialog and I want these Filefilters to have more complicated behaviour than just file extension.
The code works fine but I've read there could be some security issues saving executable code and reading it later. Is it true? Can they be avoided?
I had to implement my own interface of Predicate due to the fact that Predicate is not Serializable.
import java.io.File;
import java.io.Serializable;

public interface MyPredicate extends Serializable {
  boolean isValid(File file);
}

This is the class that defines how the filters are going to be set (It's called List despite the fact that uses internally a Map just in case implementation changes).
import java.io.Serializable;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;


public class FileFilterList implements Serializable {
  private Map&lt;String, MyPredicate&gt; filters;

  public FileFilterList() {
    this.filters = new HashMap&lt;&gt;();
  }

  public FileFilterList addFilter(String string, MyPredicate predicate) {
    this.filters.put(string, predicate);
    return this;
  }

  public Iterator&lt;Map.Entry&lt;String, MyPredicate&gt;&gt; getIterator() {
    return this.filters.entrySet().iterator();
  }
}

In this little program I save a list of filters in a file
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;

public class SaveFileFilter {
  public static void main(String[] args) throws IOException {
    FileFilterList filterList = new FileFilterList();
    filterList
            .addFilter(&quot;txt, Text files&quot;, f -&gt; f.isDirectory() || f.getName().endsWith(&quot;.txt&quot;))
            .addFilter(&quot;Big txt, Big txt files (&gt;1Kb)&quot;, f -&gt; f.isDirectory() || (f.getName().endsWith(&quot;.txt&quot;) || f.length() &gt; 1024))
            .addFilter(&quot;Small txt, Small txt file (&lt;=1Kb)&quot;, f -&gt; f.isDirectory() || (f.getName().endsWith(&quot;.txt&quot;) || f.length() &lt;= 1024));
    try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(&quot;filters.obj&quot;))) {
      oos.writeObject(filterList);
    } catch (IOException ioe) {
      System.err.println(&quot;Sorry, there wa a problem saving filters&quot;);
      ioe.printStackTrace();
      throw ioe;
    }
  }
}

And this would be the main program that get the preset file filter list:
import javax.swing.*;
import javax.swing.filechooser.FileFilter;
import java.io.*;

public class FileFilterUse {
  public static void main(String[] args) throws IOException, ClassNotFoundException {
    JFileChooser fileChooser = new JFileChooser();
    try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream(&quot;filters.obj&quot;))) {
      FileFilterList filterList = (FileFilterList) ois.readObject();
      filterList.getIterator().forEachRemaining(
              (entry) -&gt; fileChooser.addChoosableFileFilter(new FileFilter() {
                @Override
                public boolean accept(File f) {
                  return entry.getValue().isValid(f);
                }

                @Override
                public String getDescription() {
                  return entry.getKey();
                }
              }));
      fileChooser.showOpenDialog(null);
    } catch (IOException ioe) {
      System.err.println(&quot;There was a problem reading filters file&quot;);
      ioe.printStackTrace();
      throw ioe;
    } catch (ClassNotFoundException cnf) {
      System.err.println(&quot;File format is not correct&quot;);
      cnf.printStackTrace();
      throw cnf;
    }
  }
}

Every java file has a package line.
I guess it is not possible to achive this serializing objects to JSON or XML, is it?
By the way, any comment about the code would be very appreciated.
I'm  going to answer my own question. Never do this!. I have just tried saving a new Filefilter object with this filter:
.addFilter(&quot;Delete files, Delete all files&quot;,f-&gt;f.delete());

And when selected, it deletes all the files in the current directory.