How to create sorted map in scala? - scala

How to create sorted map in scala (mutable/immutable)?

Use a TreeMap.

There's always java.util.TreeMap which, although not as nice as Scala's maps, is probably exactly the behavior for which you're looking.

Related

Scala equivalent of java.util.Collection?

What is the equivalent of java.util.Collection? I first thought Seq is the way to go, but actually it's an ordered set of elements. I mean we have a numeration of elements.
Collection Hierarcy
Traversable is on top above iterable
The picture above is very good looking, but it is not helpful at all in answering the question. (It may be helpful as general reference for other purposes, but not for this purpose.) It is even missing some very important interfaces. So, here is some good old text to try and clear out the confusion:
The Scala equivalent to java.util.Collection is scala.collection.Iterable, despite the mismatch in the name.
You see, scala.collection.Iterable offers the basic functionality of a Java Collection: it has an iterator and a size.
Apparently, what Java calls Iterable Scala calls it Traversable, and then what Java calls Collection Scala calls it Iterable.

Create SortedMap from Iterator in scala

I have an val it:Iterator[(A,B)] and I want to create a SortedMap[A,B] with the elements I get out of the Iterator. The way I do it now is:
val map = SortedMap[A,B]() ++ it
It works fine but feels a little bit awkward to use. I checked the SortedMap doc but couldn't find anything more elegant. Is there something like:
it.toSortedMap
or
SortedMap.from(it)
in the standard Scala library that maybe I've missed?
Edit: mixing both ideas from #Rex's answer I came up with this:
SortedMap(it.to:_*)
Which works just fine and avoids having to specify the type signature of SortedMap. Still looks funny though, so further answers are welcome.
The feature you are looking for does exist for other combinations, but not the one you want. If your collection requires just a single parameter, you can use .to[NewColl]. So, for example,
import collection.immutable._
Iterator(1,2,3).to[SortedSet]
Also, the SortedMap companion object has a varargs apply that can be used to create sorted maps like so:
SortedMap( List((1,"salmon"), (2,"herring")): _* )
(note the : _* which means use the contents as the arguments). Unfortunately this requires a Seq, not an Iterator.
So your best bet is the way you're doing it already.

Scala HashMap: iterate in insertion order?

I have been looking around, but most of them point to a java TreeMap. The only issue with that is I do not want to convert any Scala into java and back. If there really is no way, then I am ok with that, but I would like to hear it from some professionals just to be 100% sure and to have this question on here for others in the future to stumble upon when they have a similar issue. Thanks in advance.
EDIT:
Type: scala.collection.mutable.HashMap[String, String]
In general, a Scala HashMap does not guarantee the original order.
However, there is the LinkedHashMap, which states: "The iterator and all traversal methods of this class visit elements in the order they were inserted."
What is the exact type you are dealing with? If you can decide which implementation to use, then you can choose one that maintains order. If you are just given something of type HashMap, then you're out of luck.

No Scala mutable list

Scala has both a mutable and an immutable Map ,
but it has only an immutable List.
If you want a mutable List you need a ListBuffer.
I don't understand why this is so.
Any one knows?.
You can choose between these:
scala.collection.mutable.DoubleLinkedList
scala.collection.mutable.LinkedList
scala.collection.mutable.ListBuffer
scala.collection.mutable.MutableList
So, yes, Scala has mutable lists :-)
I hope that this article may be of some use to you. The diagram at the bottom of the page is particularly useful in providing the mutable and immutable classes.
http://www.scala-lang.org/docu/files/collections-api/collections_1.html
There is a mutable List, but it is called Buffer. The article linked by Graham goes into more depth, but I thought there should be a specific answer to the question as well.
Map is a trait -- like Java's interface --, while List is a class, a concrete implementation of a Seq. There are mutable and immutable Seq, just like for Map.
This may be confusing to Java programmers because, in Java, List is an interface, whose (main) implementations are ArrayList and LinkedList. Alas, Java naming is atrocious. First, ArrayList is not a List by any stretch of imagination. Also, the interface has methods that are not really related to any traditional list.
So, if you want mutable/immutable equivalence, look to concrete subclass implementations of Seq.

Why do Scala immutable HashMap methods return a Map?

I am having problems using the update method of scala.collection.immutable.HashMap.I don't see the reason it returns a Map instead of a HashMap. How do I get a new HashMap with a new key-value pair added?
That's the expected behavior. HashMap is most useful as a specific implementation of Map involving using a hash table for lookups.
Usually, you'd say var values: Map[String, Any] = new HashMap, and then sit back and use it as if it's a plain ol' immutable Map.
Do you have a reason for your code to know that it's a HashMap after you've new'd it as above?
If you're using 2.7 this is because over time the collections library has become inconsistent with various implementation class not specializing the return types of some methods. This is one of the things that is fixed in the collections library redesign for 2.8.
If you're using 2.8, this is because the update method is deprecated and you should use updated instead. This correctly specializes the return value.
scala> HashMap(1->1, 2->2, 3->3, 4->4).updated(1,3)
res4: scala.collection.immutable.HashMap[Int,Int] = Map((2,2), (4,4), (1,3), (3,3))
In 2.7.x, it returns a Map because it might be a ListMap or TreeMap or whatever, and it was deemed to be too much work to redefine the methods each time.
In 2.8.x, it should return a HashMap--but you have to use updated (update is deprecated).