How do I compare two arrays in scala?

Related searches
val a: Array[Int] = Array(1,2,4,5)
val b: Array[Int] = Array(1,2,4,5)
a==b // false

Is there a pattern-matching way to see if two arrays (or sequences) are equivalent?

You need to change your last line to

a.deep == b.deep

to do a deep comparison of the arrays.

Scala Best Practices, == does not compare arrays for value equality, but for reference equality. This is because Array is essentially an alias for Java's array, which implements equals as reference equality - only returning true if two variables point to the same array instance. Solution: Use the ++ method to join two arrays into one new array: scala> val a = Array (1,2,3) a: Array [Int] = Array (1, 2, 3) scala> val b = Array (4,5,6) b: Array [Int] = Array (4, 5, 6) scala> val c = a ++ b c: Array [Int] = Array (1, 2, 3, 4, 5, 6)

From Programming Scala:

Array(1,2,4,5).sameElements(Array(1,2,4,5))

Scala array comparison (without a PhD), Scala collections follow a simple set of general equality rules[1]. Two collections are equal if: they are in the same overall category ( Seq , Set , or� Array is a special kind of collection in Scala. On the one hand, Scala arrays correspond one-to-one to Java arrays. That is, a Scala array Array[Int] is represented as a Java int[], an Array[Double] is represented as a Java double[] and a Array[String] is represented as a Java String[]. But at the same time, Scala arrays offer much more than

  a.corresponds(b){_ == _}

Scaladoc: true if both sequences have the same length and p(x, y) is true for all corresponding elements x of this wrapped array and y of that, otherwise false

Equality, scala> map(buf) res13: Int = 3 scala> buf(0) += 1 scala> map(buf) java.util. fail because the hash-code of the array xs has changed in the second-to-last line. Scala Best Practices - Do not compare arrays with == Do not compare arrays with == When comparing two arrays for equality, use sameElements rather than ==.

For best performance you should use:

java.util.Arrays.equals(a, b)

This is very fast and does not require extra object allocation. Array[T] in scala is the same as Object[] in java. Same story for primitive values like Int which is java int.

Array equality, Am I missing something totally obvious, or are arrays never equal? deep comparison function which will work with all case classes as well: Since Object #equals is defined to return false when two different instances are� 4. How to Process an Array in Scala? Since we know the type of elements and the size of the Scala array, we can use loop control structures to process an array. Let’s take an example of processing Scala Array. To iterate over the array: scala> var a=Array(1,2,3) a: Array[Int] = Array(1, 2, 3) scala> for(i<-a){ | println(i) | } 1 2 3

As of Scala 2.13, the deep equality approach doesn't work and errors out:

val a: Array[Int] = Array(1,2,4,5)
val b: Array[Int] = Array(1,2,4,5)
a.deep == b.deep // error: value deep is not a member of Array[Int]

sameElements still works in Scala 2.13:

a sameElements b // true

How do I compare two arrays in scala?, val a: Array[Int] = Array(1,2,4,5) val b: Array[Int] = Array(1,2,4,5) a==b // false. Is there a pattern-matching way to see if two arrays (or sequences)� Scala arrays are compatible with Scala sequences – we can pass an Array[T] where a Seq[T] is required. Scala arrays also support all sequence operations. The following figure shows how values can be stored in array sequentially : Scala supports both one as well as multi-dimension arrays. A single dimension array is one having only one row and n columns whereas two dimension array is actually a matrix of dimension (n * m).

import Array._ object Demo { def main(args: Array[String]) { var myList1 = Array(1.9, 2.9, 3.4, 3.5) var myList2 = Array(8.9, 7.9, 0.4, 1.5) var myList3 = concat( myList1, myList2) // Print all the array elements for ( x <- myList3 ) { println( x ) } } }

A cool thing about Scala sets — and some other Scala collections — is that you can easily determine the union, intersection, and difference between two sets. The following examples demonstrate how the methods work.

val array = Array((for(i <- 0 to 10) yield scala.util.Random.nextInt): _*) scala.util.Sorting.quickSort(array) Scala's "default" array is a mutable data structure, very close to Java's Array. Generally speaking, that means an "array" is not very Scala-ish, even as mutable data structures go. It serves a purpose, though.

Comments
  • duplicate of stackoverflow.com/questions/2481149/… and stackoverflow.com/questions/3737711/… answer from those is to use sameElements
  • This is a duplicate, but sameElements doesn't do the trick for nested arrays, because it's not recursive. Moritz' answer below is the appropriate one now (which should probably be added to the older answers).
  • This is the canonical way to do it. But just a warning to the performance-hungry: this does create an entire new collection on both sides, so it's not the most efficient possible way to do it.
  • @Rex yes, it does create a new collection, but this does not mean, that it is inefficient. Look at the implementation of the method deep. It creates a collection, that forwards all calls of the apply method to the original array.
  • @E. Verda - Hm, the implementation is not what I'd expected. But it does a pattern match for every element of the array, which is expensive if it's an array of primitives, and for nested arrays it re-wraps the array on every access. If the arrays are almost entirely different it's inexpensive; for matching close arrays, it's going to be expensive compared to a recursive non-constructive solution.
  • @LucaMolteni:do you mean Array.equals? That doesn't seem to provide a deep comparison.
  • @matanster deepEquals is deprecated in the latest versions.
  • I think this is the correct solution, even though the other one is the accepted.
  • This was the one that helped my FreeSpec test to pass. :-)
  • Array is not a sequence scala-lang.org/api/current/index.html#scala.Array, so this will require some (probably implicit) redirections.
  • I ran val t0 = System.nanoTime(); val r = (java.util.Arrays.equals(a,b)) ; val t1 = System.nanoTime(); t1 - t0 on this sample code and very similar code for the other examples ... This option was way faster than the other examples.