case class 生成字符串的乘积迭代器
case class product iterator to make string
我想用字符串表示大小写 class
case class Person(s:Student)
case class Student(name:String,value:String){
def toMyString(flag:Boolean):String= if(flag)s"${name} =${value}" else s"Hello${name}+${value}"
}
Person(Student("abc","def")).productIterator
我想从 productIterator
打电话给 toMyString
。
我的实际用例有很多元素 class .
只要询问每个迭代器元素是否是目标类型即可。
Person(Student("abc","def")).productIterator.collect{
case x:Student => x.toMyString(true)
}
// res0: Iterator[String] = non-empty iterator (String: "abc=def")
为元素生成 String
有 2 种解决方案:
1.Create a trait
for elements
需要实现 toMyString
并且需要生成字符串的元素从它扩展,并实现它,如:
trait MyString {
def toMyString(flag: Boolean): String
}
case class Student(name: String, value: String) extends MyString{
def toMyString(flag: Boolean): String = if (flag) s"${name} =${value}" else s"Hello${name}+${value}"
}
val result: List[String] = Person(Student("abc","def")).productIterator.collect{
case x: MyString => x.toMyString(true)
}
2。通过 方法名称 获取 toMyString
方法为此使用 reflection
,这是一种棘手的方法并且类型不安全,应该多考虑一下,例如:
val student = Student("abc", "def")
val res: Iterator[String] = Person(student).productIterator.map(i => {
val method = i.getClass.getDeclaredMethods.filter(i => i.getName.equals("toMyString")).headOption
method match {
case Some(m) =>
m.invoke(i, true.asInstanceOf[AnyRef])
case None =>
null
}
})
我们需要 Iterator[Student]
而不是 Iterator[Any]
Person(Student("abc","def")).productIterator.asInstanceOf[Iterator[Student]]
我想用字符串表示大小写 class
case class Person(s:Student)
case class Student(name:String,value:String){
def toMyString(flag:Boolean):String= if(flag)s"${name} =${value}" else s"Hello${name}+${value}"
}
Person(Student("abc","def")).productIterator
我想从 productIterator
打电话给 toMyString
。
我的实际用例有很多元素 class .
只要询问每个迭代器元素是否是目标类型即可。
Person(Student("abc","def")).productIterator.collect{
case x:Student => x.toMyString(true)
}
// res0: Iterator[String] = non-empty iterator (String: "abc=def")
为元素生成 String
有 2 种解决方案:
1.Create a trait
for elements
需要实现 toMyString
并且需要生成字符串的元素从它扩展,并实现它,如:
trait MyString {
def toMyString(flag: Boolean): String
}
case class Student(name: String, value: String) extends MyString{
def toMyString(flag: Boolean): String = if (flag) s"${name} =${value}" else s"Hello${name}+${value}"
}
val result: List[String] = Person(Student("abc","def")).productIterator.collect{
case x: MyString => x.toMyString(true)
}
2。通过 方法名称 获取 toMyString
方法为此使用 reflection
,这是一种棘手的方法并且类型不安全,应该多考虑一下,例如:
val student = Student("abc", "def")
val res: Iterator[String] = Person(student).productIterator.map(i => {
val method = i.getClass.getDeclaredMethods.filter(i => i.getName.equals("toMyString")).headOption
method match {
case Some(m) =>
m.invoke(i, true.asInstanceOf[AnyRef])
case None =>
null
}
})
我们需要 Iterator[Student]
而不是 Iterator[Any]
Person(Student("abc","def")).productIterator.asInstanceOf[Iterator[Student]]