Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6618771
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T20:52:47+00:00 2026-05-25T20:52:47+00:00

I’ve discovered a strange behavior for mutable sets which I cannot understand: I have

  • 0

I’ve discovered a strange behavior for mutable sets which I cannot understand:

I have a object which I want to add to a set. The equals method for the class is overridden. When I add two different objects to the set, which produces the same output for equals method, I get a different behavior between mutable and immutable sets for the contains method.

Here is the code snippet:

class Test(text:String){
  override def equals(obj:Any) = obj match {
    case t: Test => if (t.text == this.text) true else false
    case _ => false
  }
  override def toString = text
}

val mutableSet:scala.collection.mutable.Set[Test] = scala.collection.mutable.Set.empty
mutableSet += new Test("test")
println(mutableSet)
println(mutableSet.contains(new Test("test")))

val immutableSet:scala.collection.immutable.Set[Test] = scala.collection.immutable.Set.empty
immutableSet += new Test("test")
println(immutableSet)
println(immutableSet.contains(new Test("test")))

This produces as output:

Set(test)
false
Set(test)
true

In my opinion both calls of contains should produce the same output (true).

Could anybody help me to understand the difference here or is this a bug in the scala immutable set implementation? By the way, I use scala 2.8.1.final

Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T20:52:48+00:00Added an answer on May 25, 2026 at 8:52 pm

    Rule 1 when implementing equals(): Implement hashCode() at the same time. See Overriding equals and hashCode in Java

    In the first example, you’re creating a mutable set, which calls hashCode to set up the hash table.

    In the second, you’re using an immutable set with one entry, so Scala actually uses an optimised version of Set called Set1. Set1.contains() just compares the one entry with the passed element using equals() directly. This looks like:

    /** An optimized representation for immutable sets of size 1 */
    @SerialVersionUID(1233385750652442003L)
    class Set1[A] private[collection] (elem1: A) extends Set[A] with Serializable {
      override def size: Int = 1
      def contains(elem: A): Boolean = 
        elem == elem1
      def + (elem: A): Set[A] = 
        if (contains(elem)) this
        else new Set2(elem1, elem)
      def - (elem: A): Set[A] = 
        if (elem == elem1) Set.empty
        else this
      def iterator: Iterator[A] = 
        Iterator(elem1)
      override def foreach[U](f: A =>  U): Unit = {
        f(elem1)
      }
    }
    

    No hashCode is called. There is also a Set2, Set3 and Set4.

    So if we change your code to be:

    class Test(val text:String){
      override def equals(obj:Any) = {
      println("equals=" + obj)
      obj match {
        case t: Test => if (t.text == this.text) true else false
        case _ => false
      }}
    
      override def hashCode(): Int = {
        println("hashCode=" + super.hashCode())
        super.hashCode()
      }
      override def toString = text
    }
    
    println("mutable")
    val mutableSet:scala.collection.mutable.Set[Test] = scala.collection.mutable.Set.empty
    mutableSet += new Test("test")
    println("mutableSet=" + mutableSet + " contains=" + mutableSet.contains(new Test("test")))
    
    println("immutable")
    var immutableSet:scala.collection.immutable.Set[Test] = scala.collection.immutable.Set.empty
    immutableSet += new Test("test")
    println("immutableSet=" + immutableSet + " contains=" + immutableSet.contains(new Test("test")))
    

    adding a hashCode and a println in the equals, and the output is:

    mutable
    hashCode=30936685
    hashCode=26956691
    mutableSet=Set(test) contains=false
    immutable
    equals=test
    immutableSet=Set(test) contains=true
    

    which explains why the mutable.contains() isn’t working correctly. It is looking up the object in the wrong hash table entry, equals() doesn’t even get called. And, unsurprisingly, it doesn’t find it.

    You can implement hashCode using text.hashCode:

    override def hashCode: Int = text.hashCode
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
I have a French site that I want to parse, but am running into
I have a text area in my form which accepts all possible characters from
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
I used javascript for loading a picture on my website depending on which small
I have a jquery bug and I've been looking for hours now, I can't
this is what i have right now Drawing an RSS feed into the php,
Seemingly simple, but I cannot find anything relevant on the web. What is the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.