我有一个类,它扩展了一个实现serializable本身的超类.我的类包含一个treeSet
字段和一个连接的比较器.我想使类可序列化,因为我将在集群上运行它.我得到一个java.io.NotSerializableException
错误,而访问treeSet
场.有谁知道我应该如何解决它?
public static class bounderyRecordsFilter implements FilterFunction { public ArrayListsortingkeyStart; public ArrayList sortingkeyEnd; public TreeSet intervals ; public int pass; public Comparator Interval_order = new Comparator () { public int compare(boundery e1, boundery e2) { int comp_res=0; comp_res= e1.getStartInterval() .compareToIgnoreCase(e2.getStartInterval()); return comp_res; } }; public bounderyRecordsFilter(ArrayList sortingkeyStart,ArrayList sortingkeyEnd, int pass){ super(); intervals = new TreeSet (Interval_order); for (int i=0 ; i< 4 ; i++) { boundery tempInterval = new boundery(); ... intervals.add(tempInterval) ; } this.sorkingkeyStart = sorkingkeyStart ; this.sorkingkeyEnd = sorkingkeyEnd ; this.pass = pass; } @Override public boolean filter(Tuple2 inputTuple) throws Exception { boundery tempInterval = new boundery(); boundery outputInterval = new boundery(); tempInterval .setStartInterval(inputTuple.f19); outputInterval = intervals.lower(tempInterval); if (outputInterval. getEndInterval().compareToIgnoreCase(inputTuple.f2) >0) return true; else return false; }
}
你必须同时制作boundery
和Comparator
你正在使用的Serializable
.你没有提供太多关于细节的方式boundery
,但是一种流行的方法来制作可Comparator
序列化的方法就是这样写:
enum IntervalOrder implements Comparator{ INSTANCE; public int compare(boundery e1, boundery e2) { return e1.getStartInterval() .compareToIgnoreCase(e2.getStartInterval()); } };
然后写new TreeSet
.枚举可自动序列化和单例.