hello-scala | Scala on Android starter template
kandi X-RAY | hello-scala Summary
kandi X-RAY | hello-scala Summary
Scala on Android starter template for
Support
Quality
Security
License
Reuse
Top functions reviewed by kandi - BETA
Currently covering the most popular Java, JavaScript and Python libraries. See a Sample of hello-scala
hello-scala Key Features
hello-scala Examples and Code Snippets
Community Discussions
Trending Discussions on hello-scala
QUESTION
With reference to the tutorial:
https://hello-scala.com/240-collections-maps.html
Please locate:
"Note that the last example probably only makes sense for a sorted Map."
"the last example" in the above statement:
ANSWER
Answered 2020-May-22 at 16:52The comment is referring to the literal SortedMap
class. In a SortedMap
, the entries are sorted by their keys. A SortedMap
initialized with your data will always have the order SortedMap(1 -> "a", 2 -> "b", 3 -> "c", 4 -> "d")
, because that's the only way for the keys to be sorted. For a plain Map
, the order is not guaranteed. In particular, for SortedMap
, take
respects ==
: if xs == ys
then xs.take(n) == ys.take(n)
. But for Map
, your example shows that's not true. Since a Map
is "supposed" to be an unordered collection of mappings, you're not supposed to depend on its "order". Notice that if you extend your maps to 6 elements, their order completely changes, because of implementation details of the library. It only makes sense to use take
on a generic Map
if you truly don't care which mappings you get out. E.g. it would be acceptable if you were "splitting" the map so you could operate on its pieces in parallel, or if you're "chunking" it so you can serialize it, but you can't use take
for "logical" operations, like adding a bunch of mappings to a Map
and expecting them to come back out in the order you put them in.
Community Discussions, Code Snippets contain sources that include Stack Exchange Network
Vulnerabilities
No vulnerabilities reported
Install hello-scala
Support
Reuse Trending Solutions
Find, review, and download reusable Libraries, Code Snippets, Cloud APIs from over 650 million Knowledge Items
Find more librariesStay Updated
Subscribe to our newsletter for trending solutions and developer bootcamps
Share this Page