equal

How to define an `equals` method in a Scala class (object equality)

Table of Contents1 - Solution2 - A Scala `equals` method example3 - Discussion4 - Example 2: A Scala `equals` method with inheritance5 - Implementing hashCode6 - See Also

Scala problem: You want to define an equals method for your class so you can compare object instances to each other.

Back to top

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:

Being lightheaded and passing out because of Stevia Truvia

I have been “sick” the last few weeks with a mysterious illness that has caused me to be lightheaded and even pass out. Whenever I get sick like this I think, “What has changed?” Well, one thing that changed is that when I returned home in late January I started using Truvia (Stevia) instead of Equal or Splenda. Sure enough, I stopped taking Truvia and I was fine, and then I tried it again yesterday and became lightheaded in a few hours, and nearly passed out again. I was able to check my blood pressure and heart rate during this latest episode, and my BP was fine (120/72), but my heart rate was 87 or higher. After reading other accounts of people saying that Truvia causes lightheadness and fainting, I hope I have found my culprit.

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.

Perl ‘equals’ FAQ: What is true and false in Perl?

Perl true/false FAQ: What is true in Perl? What is false in Perl?

The Perl programming language is a little unusual in not having true and false boolean operators. Because of this, I can never seem to remember what equates to true and false in Perl, so I decided to create this page.

What is true/false in Perl

In short, the following elements evalue to false in Perl:

Java String comparison FAQ: Why doesn't == work when comparing two String objects?

The == operator doesn't work when comparing two Java String objects, even if the Strings store the same content, because the == operator compares the two object references to each other. Since they aren't the same reference, they aren't equal.

In short, when comparing two Java String objects, compare them like this:

if (s1.equals(s2))
{
  // your code ...
}

and specifically do not compare two String objects like this: