A.S*_*iei 2 java serialization
我有一个类,它扩展了一个实现serializable本身的超类.我的类包含一个treeSet字段和一个连接的比较器.我想使类可序列化,因为我将在集群上运行它.我得到一个java.io.NotSerializableException错误,而访问treeSet场.有谁知道我应该如何解决它?
public static class bounderyRecordsFilter implements FilterFunction {
public ArrayList<String> sortingkeyStart;
public ArrayList<String> sortingkeyEnd;
public TreeSet<boundery> intervals ;
public int pass;
public Comparator<boundery> Interval_order = new Comparator<boundery>() {
public int compare(boundery e1, boundery e2) {
int comp_res=0;
comp_res= e1.getStartInterval() .compareToIgnoreCase(e2.getStartInterval());
return comp_res;
}
};
public bounderyRecordsFilter(ArrayList<String> sortingkeyStart,ArrayList<String> sortingkeyEnd, int pass){
super();
intervals = new TreeSet<boundery>(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<Integer, String> 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;
}
Run Code Online (Sandbox Code Playgroud)
}
你必须同时制作boundery和Comparator你正在使用的Serializable.你没有提供太多关于细节的方式boundery,但是一种流行的方法来制作可Comparator序列化的方法就是这样写:
enum IntervalOrder implements Comparator<boundery> {
INSTANCE;
public int compare(boundery e1, boundery e2) {
return e1.getStartInterval() .compareToIgnoreCase(e2.getStartInterval());
}
};
Run Code Online (Sandbox Code Playgroud)
然后写new TreeSet<boundery>(IntervalOrder.INSTANCE).枚举可自动序列化和单例.
| 归档时间: |
|
| 查看次数: |
1259 次 |
| 最近记录: |