Java Collection Serialize serializeSet(Set set)

Here you can find the source of serializeSet(Set set)

Description

serialize Set

License

Apache License

Declaration

public static byte[] serializeSet(Set<?> set) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.io.ByteArrayOutputStream;
import java.io.Closeable;

import java.io.ObjectOutputStream;

import java.util.Set;

public class Main {

    public static byte[] serializeSet(Set<?> set) {
        if (set.isEmpty()) {
            return null;
        }//  w w  w.j a v  a 2 s . co  m
        ObjectOutputStream oos = null;
        ByteArrayOutputStream baos = null;
        byte[] bytes = null;
        try {
            baos = new ByteArrayOutputStream();
            oos = new ObjectOutputStream(baos);
            for (Object obj : set) {
                oos.writeObject(obj);
            }
            bytes = baos.toByteArray();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            close(oos);
            close(baos);
        }
        return bytes;
    }

    public static void close(Closeable closeable) {
        if (closeable != null) {
            try {
                closeable.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}

Related

  1. serialize(List features)
  2. serialize(Map> in, OutputStream out)
  3. serializeMap(Map map, File file)
  4. SerializeVectorOfInts(Vector m)