The Artima Developer Community
Sponsored Link

Programming in Scala Forum
immutable HashMap

5 replies on 1 page. Most recent reply: Apr 8, 2008 2:54 PM by meiko rachimow

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 5 replies on 1 page
meiko rachimow

Posts: 6
Nickname: meiko1977
Registered: Mar, 2008

immutable HashMap Posted: Apr 8, 2008 12:15 PM
Reply to this message Reply
Advertisement
Hello scala-people

Here is my first question :-)
Is it possible to create a similar immutable Hashmap ?

val charDict = scala.collection.immutable.HashMap[Char, Int]()
('a' to 'z').zipWithIndex.foreach(charDict+=)


meiko rachimow

Posts: 6
Nickname: meiko1977
Registered: Mar, 2008

Re: immutable HashMap Posted: Apr 8, 2008 12:17 PM
Reply to this message Reply
sorry - this was my code...

val charDict = scala.collection.mutable.HashMap[Char, Int]()
('a' to 'z').zipWithIndex.foreach(charDict+=)

meiko rachimow

Posts: 6
Nickname: meiko1977
Registered: Mar, 2008

Re: immutable HashMap Posted: Apr 8, 2008 12:22 PM
Reply to this message Reply
similar here:

val alphabet = new java.lang.StringBuffer()
('a' to 'z').foreach(c => alphabet.append(c))

meiko rachimow

Posts: 6
Nickname: meiko1977
Registered: Mar, 2008

Re: immutable HashMap Posted: Apr 8, 2008 1:58 PM
Reply to this message Reply

//okay, this works so far
def abcIter():Iterator[Char] = ('a' to 'z')
val abc = new String(abcIter.mkString(""))

//but this is a little manic to me:
val charDict = HashMap[Char,Int](abcIter.zipWithIndex.next(),abcIter.zipWithIndex.next())


H ow to put all elements into the immutable Hashmap... ???

meiko rachimow

Posts: 6
Nickname: meiko1977
Registered: Mar, 2008

Re: immutable HashMap Posted: Apr 8, 2008 2:47 PM
Reply to this message Reply

val charDict = HashMap[Char, Int]()
charDict ++ alphabetIter.zipWithIndex

Okay - this do it !
I think a Constructo for Iterator would be nice...

meiko rachimow

Posts: 6
Nickname: meiko1977
Registered: Mar, 2008

Re: immutable HashMap Posted: Apr 8, 2008 2:54 PM
Reply to this message Reply
the solution

val charDict = scala.collection.immutable.HashMap[Char, Int]() ++ alphabetIter.zipWithIndex

Flat View: This topic has 5 replies on 1 page
Topic: Why are "by-name" parameters named as such? Previous Topic   Next Topic Topic: Pattern Matching In Scala

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use