what is the best Serializable replacement for "Set" and "Collection" interfaces?

Some common ones:

--HashSet:

All Implemented Interfaces:
    Serializable, Cloneable, Iterable<E>, Collection<E>, Set<E>

--TreeSet:

All Implemented Interfaces:
    Serializable, Cloneable, Iterable<E>, Collection<E>, NavigableSet<E>, 
    Set<E>, SortedSet<E> 

--ArrayList:

All Implemented Interfaces:
    Serializable, Cloneable, Iterable<E>, Collection<E>, List<E>, RandomAccess 

--LinkedList:

All Implemented Interfaces:
    Serializable, Cloneable, Iterable<E>, Collection<E>, List<E>, Queue<E> 

The type checking knows that set is not serializable, but a subtype of set can be serializable.

Set is a Interface. But implementation of set is HashSet. This is serializable.

You would not expect to serialize a Set because a Set can not be instantiated.

Set st = new Set(); // illegal 

So set doesn't really need to implement Serializable.

Anyway, you can use LinkedHashSet and TreeSet. Those are also Serializable.


Set is an Interface.
Use HashSet that are implementing Set and HashSets are serializable.
Just be sure that all the objects in the Set is serializable.

For more info Why java.util.Set is not Serializable?

PS. It doesn't have to be a HashSet; use any concrete class that is serializable and implementing Set or Collection.

Tags:

Java