当前位置:网站首页>Scala equality
Scala equality
2022-06-22 16:56:00 【ZH519080】
stay scala Everything in is the object !!!
java Compare two objects in / Whether the attributes are equal :
/** stay java in ,== Only to java Object references , Object references the same address ( Same location in memory ) Then return to true ; * and equals Is to compare whether the values of two fields are equal , If the values are equal, return true * * But when comparing Array perhaps Seq when , Use sameElements Method */
class EqualEq {
case class Person(firstName: String,lastName: String,age: Int)
val p1 = Person("Dean","Wampler",29)
val p2 = Person("Dean","Wampler",29)
val p3 = Person("Buck","Trends",30)
p1 equals p2 // =true
p1 equals(p3) //=false
p1 equals null //=false
null equals p1 // Throw out java.lang.NullPointerException abnormal
null equals null // Throw out java.lang.NullPointerException abnormal
p1 == p2 //=true
p1 == p3 //=false
/** stay scala in ,equals And == Exactly the same as , They just test whether the values are equal */
p1 == null //=false
null == p1 //=false
null == null //=true, There are compilation warnings
p1 eq(p2) //=true
p1 eq p3 //=false
p1 eq null //=false
null eq(p2) //=false
null eq(null) //true , There are compilation warnings
/**eq Is to compare whether references are equal , When two objects point to the same location in memory, it returns true
* among ne Methods and eq contrary ,ne Equivalent to !(object1 eq object2)*/
}But when scala in Array perhaps Seq It is best to use... To compare equality sameElements idea
/**
* @author E-mail:[email protected] Zhu Haichuan
* @date Creation time :2016/11/25 21:54
* @declare Related instructions : Array 、 Sequence equality comparison
*/
object ArraySeqsameElements {
def main(args: Array[String]): Unit = {
val arraySeqsameElements = new ArraySeqsameElements
arraySeqsameElements.arrayEquals
}
}
class ArraySeqsameElements {
def arrayEquals: Unit ={
/** Compare Array Using an array sameElements Method */
val array1 = Array(1,2,3)
val array2 = Array(1,2,3)
val bool1 = array1 == array2 // = false
println(" Use == Compare array equality :"+bool1)
val bool2 = array1 sameElements(array2)
println(" Use sameElements Compare equality :"+bool2)
/** If comparing arrays , It is better to compare with sequence */
val list1 = List(1,2,3)
val list2 = List(1,2,3)
val bool3 = list1 == list2 //=true
val bool4 = list1 sameElements(list2) //=true
}
}
边栏推荐
- 华为云招募工业智能领域合作伙伴,强力扶持+商业变现
- Spark Streaming checkpoint的问题与恢复
- Add a millennial sign to a number (amount in millennia)
- 毕业季·本科毕业感想——机械er的自救之路
- Spark and mysql:did not find registered driver with class com mysql. jdbc. Driver
- 为数字添加千分位符号(金额千分位)
- MYSQL_ERRNO : 1292 Truncated incorrect date value At add_num :1
- 2022年中国重卡智能化升级专题研究
- STM32通过DMA进行ADC采集(HAL库)
- [C language] deeply analyze the relationship between pointer and array
猜你喜欢

每秒处理10万高并发订单的乐视集团支付系统架构分享

视频爆炸时代,谁在支撑视频生态网高速运行?
![[C language] use of library function qsort](/img/b0/6e86e31243164479b0f3d960d039ef.png)
[C language] use of library function qsort

【C语言】深度剖析指针和数组的关系

Safari兼容性问题总结

华为云招募工业智能领域合作伙伴,强力扶持+商业变现

图计算Hama-BSP模型的运行流程

Machine learning notes - Hagrid - Introduction to gesture recognition image data set
![Web technology sharing | [Gaode map] to realize customized track playback](/img/0b/25fc8967f5cc2cea626e0b3f2b7594.png)
Web technology sharing | [Gaode map] to realize customized track playback
![[wechat applet custom bottom tabbar]](/img/04/2ea4ab3fd8571499190a9b3c9990b2.png)
[wechat applet custom bottom tabbar]
随机推荐
如何为政企移动办公加上一道“安全锁”?
[C language] use of library function qsort
为什么要买增额终身寿险?增额终身寿险安全可靠吗?
What is restful and what rules should be followed when designing rest APIs?
Web technology sharing | [Gaode map] to realize customized track playback
In the era of video explosion, who is supporting the high-speed operation of video ecological network?
Call CMD process communication
[wechat applet custom bottom tabbar]
NiO uses writable events to handle the situation of one-time write incompleteness
What should I do if I can't hear a sound during a video conference?
MYSQL_ERRNO : 1292 Truncated incorrect date value At add_num :1
每秒处理10万高并发订单的乐视集团支付系统架构分享
SAP value process & help request process-011
华为云招募工业智能领域合作伙伴,强力扶持+商业变现
Interview knowledge points
数据库mysql 主从方案
社会担当 广汽本田“梦想童行”倡导儿童道路交通安全
Spark and mysql:did not find registered driver with class com mysql. jdbc. Driver
Consumption monitoring of Prometheus monitoring [consult exporter]
【C语言】深度剖析整型和浮点型在内存中的存储