1

If I have a case class like below:

case class Student(name: String, activities: Seq[String], grade: String)

And I have a List like this:

val students = List(
      Student("John",List("soccer","Video Games"),"9th"),
      Student("Jane",List("sword fighting","debate"),"10th")
     )

How can I convert the entire thing into a string like below:

johnsoccervideogames9thjaneswordfightingdebate10th

I'd like to take an MD5 has of this string.

3
  • 1
    Override toString method of the case class Commented Aug 5, 2019 at 23:21
  • @texasbruce overriding toString on a case class just for a one time use seems like a really bad idea... Commented Aug 5, 2019 at 23:35
  • @LuisMiguelMejíaSuárez He's not quite clear about his requirement. Don't think he mentioned this is a one time use. Commented Aug 6, 2019 at 0:45

1 Answer 1

1

This seems to do what you are expecting.

students.map( student => student.name + student.activities.mkString + student.grade).mkString.toLowerCase.replaceAll("\\s", "")
Sign up to request clarification or add additional context in comments.

2 Comments

When concatenating large Strings, always use String Interpolator: student => s"${student.name}${student.activities.mkString}${student.grade}".
@LuisMiguelMejíaSuárez Is this possible to do in a sorting as well? stackoverflow.com/questions/57367311/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.