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 886819
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T13:07:58+00:00 2026-05-15T13:07:58+00:00

Using scala, 2.8: import scala.collection.mutable import mutable.MultiMap val m = new mutable.HashMap[String, mutable.Set[String]] with

  • 0

Using scala, 2.8:

import scala.collection.mutable
import mutable.MultiMap
val m = new mutable.HashMap[String, mutable.Set[String]] with MultiMap[String, String]
m.addBinding("key", null)
m exists { _._2 contains null }

prints false

m exists { _._2 isEmpty }

prints false

m("key").size

prints 1

How do I find the first key (or any) key that was added via an addBinding call with a value of null?

  • 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-15T13:07:59+00:00Added an answer on May 15, 2026 at 1:07 pm

    In Scala 2.7.7, you get a NullPointer straight away when adding null:

    
    scala> val s = new scala.collection.mutable.HashSet[String]
    s: scala.collection.mutable.HashSet[String] = Set()
    
    scala> s += null
    java.lang.NullPointerException
        at scala.collection.mutable.FlatHashTable$class.elemHashCode(FlatHashTable.scala:144)
        at scala.collection.mutable.HashSet.elemHashCode(HashSet.scala:31)
        at scala.collection.mutable.FlatHashTable$class.addEntry(FlatHashTable.scala:66)
        at scala.collection.mutable.HashSet.addEntry(HashSet.scala:31)
        at scala.collection.mutable.HashSet.$plus$eq(HashSet.s...
    

    Using Scala 2.8.0.RC7, that is no longer the case. However, as Randall has observed, the behaviour is not entirely consistent either:

    
    scala> import scala.collection.mutable.HashSet
    import scala.collection.mutable.HashSet
    
    scala> val s = new HashSet[String]
    s: scala.collection.mutable.HashSet[String] = Set()
    
    scala> s += null
    res0: s.type = Set()
    
    scala> s += null
    res1: s.type = Set()
    
    scala> s.size
    res2: Int = 2
    
    scala> s.head
    java.util.NoSuchElementException: next on empty iterator
        at scala.collection.Iterator$$anon$3.next(Iterator.scala:29)
        at scala.collection.Iterator$$anon$3.next(Iterator.scala:27)
        at scala.collection.mutable.FlatHashTable$$anon$1.next(FlatHashTable.scala:176)
        at scala.collection.IterableLike$class.head(IterableLike.scala:102)
        at scala.collection.mutable.HashSet.head(HashSet.scala:38)
        at .(:8)
    

    Most of the work when adding an entry is done in
    FlatHashTable
    When you add an entery, addEntry is invoked, which looks like this:

    
     /** The actual hash table.
     */
     @transient protected var table: Array[AnyRef] = new Array(initialCapacity)
    
    def addEntry(elem: A) : Boolean = {
      var h = index(elemHashCode(elem))
      var entry = table(h)
      while (null != entry) {
      if (entry == elem) return false
          h = (h + 1) % table.length
          entry = table(h)
     }
     table(h) = elem.asInstanceOf[AnyRef]
     tableSize = tableSize + 1
     if (tableSize >= threshold) growTable()
        true
     }
    

    null.asInstanceOf[AnyRef] just gives you back null, and the elemHashCode(elem) returns zero if the elem is null. So in this case, the element with index zero is 'initialized' with null (which is the value it already had), but the tableSize is increased. This explains the results that are seen in the REPL.

    If you call head on the Set, ultimately the iterator in FlatHashTable is called, as can be seen from the stacktrace. This is implemented as follows:

    
    iterator = new Iterator[A] {
      private var i = 0
      def hasNext: Boolean = {
        while (i < table.length && (null == table(i))) i += 1
        i < table.length
      }
      def next(): A =
        if (hasNext) { i += 1; table(i - 1).asInstanceOf[A] }
        else Iterator.empty.next
    }
    

    hasNext will return false, since the element that we stored in the Array is null, therefore, the call to next will call Iterator.empty.next, which throws an exception.
    Since adding null to a HashSet is not handled consistently in Scala 2.8, this indeed looks like a bug. Also, as already has been remarked, it provides another good reason to stay away from using null where possible.

    Edit Since I couldn't find a ticket for this, I added it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 476k
  • Answers 476k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You'd have to update the top-most parent frame's favicon to… May 16, 2026 at 4:56 am
  • Editorial Team
    Editorial Team added an answer array most likely refers to a numpy.array myarray = array([0,… May 16, 2026 at 4:56 am
  • Editorial Team
    Editorial Team added an answer You should try using a buffer, to cache the data,… May 16, 2026 at 4:56 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.