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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T06:51:46+00:00 2026-06-18T06:51:46+00:00

Below Scala class parses a file using JDOM and populates the values from the

  • 0

Below Scala class parses a file using JDOM and populates the values from the file into a Scala immutable Map. Using the + operator on the Map does not seem to have any effect as the Map is always zero.

import java.io.File
import org.jsoup.nodes.Document
import org.jsoup.Jsoup
import org.jsoup.select.Elements
import org.jsoup.nodes.Element
import scala.collection.immutable.TreeMap

class JdkElementDetail() {

  var fileLocation: String = _

  def this(fileLocation: String) = {
      this()
      this.fileLocation = fileLocation;
    }


  def parseFile : Map[String , String] = {

    val jdkElementsMap: Map[String, String] = new TreeMap[String , String];
    val input: File = new File(fileLocation);
    val doc: Document = Jsoup.parse(input, "UTF-8", "http://example.com/");
    val e: Elements = doc.getElementsByAttribute("href");

    val href: java.util.Iterator[Element] = e.iterator();
    while (href.hasNext()) {
      var objectName = href.next();
      var hrefValue = objectName.attr("href");
      var name = objectName.text();

      jdkElementsMap + name -> hrefValue
            println("size is "+jdkElementsMap.size)
    }

    jdkElementsMap
  }

}

println("size is "+jdkElementsMap.size) always prints "size is 0"

Why is the size always zero, am I not adding to the Map correctly?

Is the only fix for this to convert jdkElementsMap to a var and then use the following?

jdkElementsMap += name -> hrefValue

Removing the while loop here is my updated object:

package com.parse

import java.io.File
import org.jsoup.nodes.Document
import org.jsoup.Jsoup
import org.jsoup.select.Elements
import org.jsoup.nodes.Element
import scala.collection.immutable.TreeMap
import scala.collection.JavaConverters._

class JdkElementDetail() {

  var fileLocation: String = _

  def this(fileLocation: String) = {
      this()
      this.fileLocation = fileLocation;
    }


  def parseFile : Map[String , String] = {

    var jdkElementsMap: Map[String, String] = new TreeMap[String , String];
    val input: File = new File(fileLocation);
    val doc: Document = Jsoup.parse(input, "UTF-8", "http://example.com/");
    val elements: Elements = doc.getElementsByAttribute("href");

    val elementsScalaIterator = elements.iterator().asScala

    elementsScalaIterator.foreach {
      keyVal => {
          var hrefValue = keyVal.attr("href");
          var name = keyVal.text();
          println("size is "+jdkElementsMap.size)
          jdkElementsMap += name -> hrefValue
       }
    }
    jdkElementsMap
  }

}
  • 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-06-18T06:51:47+00:00Added an answer on June 18, 2026 at 6:51 am

    Immutable data structures — be they lists or maps — are just that: immutable. You don’t ever change them, you create new data structures based on changes to the old ones.

    If you do val x = jdkElementsMap + (name -> hrefValue), then you’ll get the new map on x, while jdkElementsMap continues to be the same.

    If you change jdkElementsMap into a var, then you could do jdkEleemntsMap = jdkElementsMap + (name -> hrefValue), or just jdkElementsMap += (name -> hrefValue). The latter will also work for mutable maps.

    Is that the only way? No, but you have to let go of while loops to achieve the same thing. You could replace these lines:

    val href: java.util.Iterator[Element] = e.iterator();
    while (href.hasNext()) {
      var objectName = href.next();
      var hrefValue = objectName.attr("href");
      var name = objectName.text();
    
      jdkElementsMap + name -> hrefValue
            println("size is "+jdkElementsMap.size)
    }
    
    jdkElementsMap
    

    With a fold, such as in:

    import scala.collection.JavaConverters.asScalaIteratorConverter
    
    e.iterator().asScala.foldLeft(jdkElementsMap) {
      case (accumulator, href) =>  // href here is not an iterator
        val objectName = href
        val hrefValue = objectName.attr("href")
        val name = objectName.text()
    
        val newAccumulator = accumulator + (name -> hrefValue)
    
        println("size is "+newAccumulator.size)
    
        newAccumulator
    }
    

    Or with recursion:

    def createMap(hrefIterator: java.util.Iterator[Element],
                  jdkElementsMap: Map[String, String]): Map[String, String] = {
      if (hrefIterator.hasNext()) {
        val objectName = hrefIterator.next()
        val hrefValue = objectName.attr("href")
        val name = objectName.text()
    
        val newMap = jdkElementsMap + name -> hrefValue
    
        println("size is "+newMap.size)
    
        createMap(hrefIterator, newMap)
      } else {
         jdkElementsMap
      }
    }
    
    createMap(e.iterator(), new TreeMap[String, String])
    

    Performance-wise, the fold will be rather slower, and the recursion should be very slightly faster.

    Mind you, Scala does provide mutable maps, and not just to be able to say it has them: if they fit better you problem, then go ahead and use them! If you want to learn how to use the immutable ones, then the two approaches above are the ones you should learn.

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

Sidebar

Related Questions

Why does scala complains for below code? scala> class Http(var status: Int) defined class
Hi I'm passing path string to Groovy Script from Scala like below but when
Below is the code from internalRegister method of GCMRegistrar class static void internalRegister(Context context,
below is the code to download a txt file from internet approx 9000 lines
Scala's Option class has an orNull method, whose signature is shown below. orNull [A1
Below class does not compile, if I declare Functions as an object instead of
I'm trying to understand the add method in below class taken from book 'Programming
I'm attempting to learn Scala coming from a Java background. Should the below program
Let me explain ;-) both classes below are in package com.company.foo RoleGroup.scala abstract class
Scala superimposes a very elegant class hierarchy over Java's type system, balooning out from

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.