Last active
August 29, 2015 14:17
-
-
Save nenono/8b849b22a47ee424a6c2 to your computer and use it in GitHub Desktop.
Scalaでオブジェクトの比較(等値性、同一性、大小)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// オブジェクトの比較 | |
object Comparing { | |
// --------------------------------------------------------------------------------- | |
// 適当なcase class | |
case class SomeCase(key1:Int, key2:String) | |
// 適当なclass | |
class SomeClass(val key1:Int, val key2:String) | |
def equalsTest = { | |
println("equalsTest") | |
// 以下全てtrue | |
println(1 == 1) | |
println(1 != 2) | |
println(true == true) | |
println(null == null) | |
// 文字列も値比較 | |
println("aaaaa" == "aaaaa") | |
// ケースクラスも値比較 | |
println(SomeCase(1, "a") == SomeCase(1, "a")) | |
println(SomeCase(1, "a") != SomeCase(1, "b")) | |
// Tuple | |
println((1, 2) == (1, 2)) | |
// List | |
println(List(1, 2, 3) == List(1, 2, 3)) | |
// Vector | |
println(Vector(1, 2, 3) == Vector(1, 2, 3)) | |
// Map | |
println(Map("a"->1, "b"->2) == Map("a"->1, "b"->2)) | |
// Set | |
println(Set(1, 2, 3) == Set(3, 2, 1)) | |
// 通常のオブジェクトでは参照比較になります | |
println(new SomeClass(1, "a") != new SomeClass(1, "a")) | |
// ポインタ比較をしたい場合は eq/ne演算子を使います | |
val x = new SomeClass(1, "a") | |
println(x eq x) | |
println(null ne x) | |
// 配列も参照比較 | |
println(Array(1, 2, 3) != Array(1, 2, 3)) | |
println(Array(1, 2, 3).sameElements(Array(1, 2, 3))) // 要素比較の場合はsameElementsメソッドを使います | |
} | |
// --------------------------------------------------------------------------------- | |
// ==演算子でequalsメソッドが呼ばれる確認 | |
class SomeClass2(val key:Int) { | |
override def equals(other: Any) = { | |
println("equals called!") | |
other match { | |
case that: SomeClass2 => key == that.key // 型の一致でパターンマッチ | |
case _ => false | |
} | |
} | |
} | |
def equalsTest2 = { | |
println("--equalsTest2--") | |
println(null == new SomeClass2(1)) // false | |
println(null != new SomeClass2(1)) // true | |
println(new SomeClass2(1) == new SomeClass2(1)) // equalsが呼び出されて、true | |
println(new SomeClass2(1) != new SomeClass2(2)) // equalsが呼び出されて、true | |
} | |
// --------------------------------------------------------------------------------- | |
// 大小比較 | |
// 参考: [Scala - 大小比較の可能なcase class - Qiita](http://qiita.com/edvakf@github/items/01572c97bee954905a01) | |
case class Row(age:Int, name:String) extends Ordered[Row]{ | |
override def compare(other: Row): Int = Ordering.by(Row.unapply).compare(this, other) | |
} | |
def lessMoreTest = { | |
println("--lessMoreTest--") | |
val rows = Vector(Row(19, "bob"), Row(19, "alice"), Row(17, "charlie")) | |
println(rows.sorted) // charlie, alice, bob | |
} | |
def main(args: Array[String]) { | |
equalsTest | |
equalsTest2 | |
lessMoreTest | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment