1

How to Add items to Queue sitting in a Scala mutable.hashmap?

I tried this:

 val hashMapUserListeners: mutable.HashMap[UUID, mutable.Queue[UUID]]
 hashMapUserListeners.get(uuid) += uuid2

But got this error:

[error]Expression does not convert to assignment because receiver is not assignable.
[error]hashMapUserListeners.get(uuid) += uuid2

Actual Code Snippet:

  def listenUserStatus(actorRef: ActorRef, message: SocketParsedMessage)={
      (message.data \ "userId").validate[UUID] match {
        case s: JsSuccess[UUID] => {
          if(hashMapUserListeners.contains(s.get)){
            if(!hashMapUserListeners.get(s.get).contains(hashMapA2U.get(actorRef))) {
              hashMapUserListeners.get(s.get) += hashMapA2U.get(actorRef)
            }
          } else{
            hashMapUserListeners += (s.get -> new mutable.Queue[UUID]())
          }
        }
        case e: JsError => actorRef ! SocketParsedMessage(
          AllowedSocketMessageTypes.LISTEN_USER_STATUS, Json.obj(
            "success" -> false,
            "message" -> "UserId not provided with request"
          ))
4
  • Please refer to documentation: tutorialspoint.com/scala/scala_maps.htm Commented Oct 23, 2017 at 13:03
  • @Pavel I know how to add items to Hashmap, I am asking about adding more item to quee in Hashmap, without cloning and adding items to it. Commented Oct 23, 2017 at 13:06
  • Its looks like item you are added to the HasMap is immutable, please provide definition for Queue etc Commented Oct 23, 2017 at 13:10
  • @Pavel I added my actual code snippet, and no queue(mutable.Queue[UUID]) is mutable. Commented Oct 23, 2017 at 13:17

2 Answers 2

1

This doesn't work because mutable.HashMap.get returns an Option[Queue[UUID]], not a Queue[UUID]. You'll need to go under the Option using foreach to update the underlying queue:

val maybeUuids: Option[mutable.Queue[UUID]] = hashMapUserListeners.get(uuid)
maybeUuids.foreach(queue => queue += uuid2)
Sign up to request clarification or add additional context in comments.

Comments

0

Finally I altered my code to one attached below.

   hashMapUserListeners.get(s.get) match {
        case Some(currentQueue) => {
               if(!currentQueue.contains(s.get)){
                  currentQueue += userId
               }
        }

For those interested in complete code.

 def listenUserStatus(actorRef: ActorRef, message: SocketParsedMessage): Unit = {
    hashMapA2U.get(actorRef) match {
      case Some(user) => {
        (message.data \ "userId").validate[UUID] match {
          case s: JsSuccess[UUID] => {
            hashMapUserListeners.get(s.get) match {
              case Some(x) => {
        case Some(currentQueue) => {
               if(!currentQueue.contains(s.get)){
                  currentQueue += userId
               }
              }
              case None => hashMapUserListeners += (s.get -> new mutable.Queue[UUID]())
            }
          }
          case e: JsError => actorRef ! SocketParsedMessage(
            AllowedSocketMessageTypes.LISTEN_USER_STATUS, Json.obj(
              "success" -> false,
              "message" -> "UserId not provided with request"
            ))
        }
      }
      case None => {
        actorRef ! SocketParsedMessage(
          AllowedSocketMessageTypes.AUTHENTICATE, Json.obj(
            "success" -> false,
            "message" -> "User not authenticated"
          ))
      }
    }
  }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.