I want to take the hash of my mail addresses and write it to the log file.
logger.info(s"[RestService] Get permission request for $email")
this is my case but How can I get the hash of the email.
Helper or method?
I suggest you to create an object and overwrite the hashCode
method. Then use the scala.util.hashing.MurmurHash3
to create your hash code.
import scala.util.hashing.MurmurHash3
object TestMurmurHash {
def main(args: Array[String]): Unit = {
val email = MyObject("my_email@google.com")
println(s"This is my email hash: $email and this is my hash: ${email.hashCode()}")
}
case class MyObject(val email: String) {
override def equals(o: Any): Boolean = {
this.hashCode() == o.hashCode()
}
override def hashCode(): Int = {
MurmurHash3.stringHash(email)
}
def mailToHash(): Int = {
MurmurHash3.stringHash(email)
}
}
}
output:
This is my email hash: MyObject(my_email@google.com) and this is my hash: -585889836
good but how to write own methot and I want use everywhere . Ex: def mailToHash
What is the point of using
App
if you are going to write themain
yourself? You are just having the worst of all words. Second,hashCode
has to be consistent withequals
, the best on those cases is always have a custom method.@KemalDurmus just create a method with this signature. I updated the answer. I case you need to have a Util class use this method inside a Util and any other object can use it.
thank you your solutions is good but I use diffrent solutions. I write hash helper and add project other package
def hashEmail(email: String): String = { HASH_FUNCTION.hashString(email, StandardCharsets.UTF_8).toString }