Scala Array [Byte] diff

I'm using the diff method to compare two arrays of string bytes, but it gives unexpected results (or is this expected behavior?).

diff()

returns true

for (subString diff originalString)

, but returns false

for (originalString diff subString)

. It would be great if someone can explain this. Sample code and output are shown below.

Snippet of code

val subString : Array[Byte] = "cde".getBytes("UTF-8")
val originalString : Array[Byte] = "abcdefg".getBytes("UTF-8")

println(s"compare subString with originalString - ${((subString diff originalString).isEmpty).toString}")
println(s"compare originalString with subString - ${((originalString diff subString).isEmpty).toString}")

      

Output

compare subString with originalString - true

compare originalString with subString - false

      

+3


source to share


1 answer


diff

is not compared; it is like a difference of tasks, "what are the characters that are in the first line, but not in the second?"

"abcd" diff "a" = "bcd"
"a" diff "abcd" = ""

      



so they are almost equivalent:

"abc".toSet diff "cb".toSet = Set(a)
"abc" diff "cb" = "a"

      

+5


source







All Articles