我有Scala类,它将生成一个Option[StructType]将在Java函数中使用的值.在那个java函数中,我需要检查它是否Option[StructType]是Scala None.我怎么做?
class Person(columns : String) {
val recordStruct : Option[StructType] = {
if ( columns != null && !columns.isEmpty()) {
Some(new StructType(fields.map(field =>
StructField(field, StringType, true)).toArray))
} else {
None
}
}
}
Run Code Online (Sandbox Code Playgroud)
StructType structure = person.recordStruct().get();
// how to check if structure is None (in scala) ????
if (structure is None) {
// ...
}
Run Code Online (Sandbox Code Playgroud)
Option<StructType> maybeStructure = person.recordStruct();
if (maybeStructure.isEmpty()) {
// do something if None
} else {
StructType structure = person.recordStruct().get();
// now you can use structure...
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2882 次 |
| 最近记录: |