public class Test {
private static void trySerialize (Object o) {
try {
ObjectOutputStream stream = new ObjectOutputStream(new ByteArrayOutputStream());
System.out.println("Trying to serialize " + o + "...");
stream.writeObject(o);
stream.flush();
}
catch (Exception e) {
System.out.println("failed: ");
e.printStackTrace();
}
}
public static void main (String[] args) {
Map map = new HashMap();
map.put("one", "one");
map.put("two", "two");
map.put("three", "three");
System.out.println("keySet():");
trySerialize(map.keySet());
System.out.println("entrySet():");
trySerialize(map.entrySet());
System.out.println("values():");
trySerialize(map.values());
//如果需要被序列化,代码需要
System.out.println("new values():");
trySerialize(new HashSet(map.values()));
}
}