How to define an `equals` method in a Scala class (object equality)
Scala problem: You want to define an equals
method for your class so you can compare object instances to each other.
Solution
If you’re new to Scala, a first thing to know is that object instances are compared with ==
:
"foo" == "foo" // true
"foo" == "bar" // false
"foo" == null // false
null == "foo" // false
1 == 1 // true
1 == 2 // false
1d == 1.0d // true
case class Person(name: String)
Person("Jess") == Person("Jessie") // false
This is different than Java, which uses ==
for primitive values and equals
for object comparisons.
How to test String equality in Scala
This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 1.1, “Testing String Equality in Scala.”
Problem
When using Scala, you want to compare two strings to see if they’re equal, i.e., whether they contain the exact same sequence of characters.
Solution
In Scala you compare two String instances with the ==
operator. Given these strings:
This is a page from my book, Functional Programming, Simplified
A Quick Review of Scala’s Case Classes
“The biggest advantage of case classes is that they support pattern matching.”
A Java “approximately equal” method (function)
As a quick note, here’s the source code for a Java “approximately equal” function that I use in an Android application:
A Java tuple class (Tuple2 or Pair, if you prefer)
After working with Scala for a long time, I had to come back to Java for a while to work on an Android app. Right away I missed a lot of things from the Scala world, including all of the built-in Scala collection methods, and other things as simple as the Scala Tuple
classes.
If you haven’t used them before, a Scala Tuple
class lets you write code like this:
Tuple<String, Integer> t = new Tuple<>("age", 41);
If you’re comfortable with generics, the Java implementation of a Tuple
class like this is simple:
How to generate boilerplate code with Scala case classes
This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 4.14, “How to generate boilerplate code with Scala case classes.”
Scala - How to find the unique items in a List, Array, Vector (sequence)
Scala FAQ: How do I find the unique items in a List
, Array
, Vector
, or other Scala sequence?
Solution: Use the distinct
method.
Here's a simple example using a List
of integers:
scala> val x = List(1,1,1,2,2,3,3) x: List[Int] = List(1, 1, 1, 2, 2, 3, 3) scala> x.distinct res0: List[Int] = List(1, 2, 3)
As you can see, res0
now contains only the unique elements in the list.
How to compare String equality in Java
Java String comparison FAQ: Can you share some examples of how to compare strings in Java?
If you’re like me, when I first started using Java, I wanted to use the ==
operator to test whether two String
instances were equal, but that’s not the correct way to do it in Java.
Scala, isInstanceOf, inheritance, getClass, and equals (equality)
I was doing a little Scala programming this morning, and because I hadn't written any code in a while, I managed to forget how isInstanceOf works with inheritance in Scala.
To refresh my memory, I wrote the following example code:
A Java instanceof array example
While working with various "Java instanceof" tests recently, my curiosity was piqued, and I thought I'd take a look at how the instanceof operator works when testing against a Java array.