Scala Queue equals()方法及示例
equals() 方法是用来检查两个队列是否由相同的元素组成,而且顺序也相同。
方法定义: def equals(o: Any):Boolean
返回类型。如果两个队列是相同的,则返回真,否则返回假。
例子 #1:
// Scala program of equals()
// method
// Import Queue
import scala.collection.mutable._
// Creating object
object GfG
{
// Main method
def main(args:Array[String])
{
// Creating queues
val q1 = Queue(1, 3, 2, 7, 6, 5)
val q2 = Queue(1, 3, 2, 7, 6, 5)
// Print the queue
println("Queue_1: " + q1)
println("Queue_2: " + q2)
// Applying equals method
val result = q1.equals(q2)
// Displays output
print("Queue_1 = Queue_2: " + result)
}
}
输出。
Queue_1: Queue(1, 3, 2, 7, 6, 5)
Queue_2: Queue(1, 3, 2, 7, 6, 5)
Queue_1 = Queue_2: true
例子#2。
// Scala program of equals()
// method
// Import Queue
import scala.collection.mutable._
// Creating object
object GfG
{
// Main method
def main(args:Array[String])
{
// Creating queues
val q1 = Queue(1, 3, 2, 7, 6, 5)
val q2 = Queue(5, 3, 2, 7, 6, 1)
// Print the queue
println("Queue_1: " + q1)
println("Queue_2: " + q2)
// Applying equals method
val result = q1.equals(q2)
// Displays output
print("Queue_1 = Queue_2: " + result)
}
}
输出。
Queue_1: Queue(1, 3, 2, 7, 6, 5)
Queue_2: Queue(5, 3, 2, 7, 6, 1)
Queue_1 = Queue_2: false
极客教程