在 Scala Stack class
,则使用diff()方法来查找两个堆栈之间的差异。它从另一个堆栈中删除一个堆栈中存在的元素。
函数定义: def diff[B >: A](that: collection.Seq[B]): Stack[A]
返回类型: It returns a new stack which consists of elements after the difference between the two stacks.
范例1:
// Scala program of diff()
// method
// Import Stack
import scala.collection.mutable._
// Creating object
object GfG
{
// Main method
def main(args:Array[String])
{
// Creating stacks
val s1 = Stack(1, 2, 3, 4, 5)
val s2 = Stack(3, 4, 5)
// Print the stack
println("Stack_1: " + s1)
println("Stack_2: " + s2)
// Applying diff method
val result = s1.diff(s2)
// Displays output
print("(Stack_1 - Stack_2): " + result)
}
}
输出:
Stack_1: Stack(1, 2, 3, 4, 5) Stack_2: Stack(3, 4, 5) (Stack_1 - Stack_2): Stack(1, 2)
范例2:
// Scala program of diff()
// method
// Import Stack
import scala.collection.mutable._
// Creating object
object GfG
{
// Main method
def main(args:Array[String])
{
// Creating stacks
val s1 = Stack(1, 2, 3, 4, 5)
val s2 = Stack(3, 4, 5, 6, 7, 8)
// Print the stack
println("Stack_1: " + s1)
println("Stack_2: " + s2)
// Applying diff method
val result = s2.diff(s1)
// Displays output
print("(Stack_2 - Stack_1): " + result)
}
}
输出:
Stack_1: Stack(1, 2, 3, 4, 5) Stack_2: Stack(3, 4, 5, 6, 7, 8) (Stack_2 - Stack_1): Stack(6, 7, 8)
相关用法
- Scala Set diff()用法及代码示例
- Scala Queue diff()用法及代码示例
- Scala TreeSet diff()用法及代码示例
- Scala SortedSet diff()用法及代码示例
- Scala Stack top()用法及代码示例
- Scala Stack min()用法及代码示例
- Scala Stack max()用法及代码示例
- Scala Stack last()用法及代码示例
- Scala Stack last()用法及代码示例
- Scala Stack contains()用法及代码示例
- Scala Stack pop()用法及代码示例
- Scala Stack map()用法及代码示例
- Scala Stack take()用法及代码示例
- Scala Stack +:()用法及代码示例
注:本文由纯净天空筛选整理自rupesh_rao大神的英文原创作品 Scala Stack diff() method with example。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。