Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
58
Task/Collections/Scala/collections-1.scala
Normal file
58
Task/Collections/Scala/collections-1.scala
Normal file
|
|
@ -0,0 +1,58 @@
|
|||
Windows PowerShell
|
||||
Copyright (C) 2012 Microsoft Corporation. All rights reserved.
|
||||
|
||||
PS C:\Users\FransAdm> scala
|
||||
Welcome to Scala version 2.10.1 (Java HotSpot(TM) 64-Bit Server VM, Java 1.7.0_25).
|
||||
Type in expressions to have them evaluated.
|
||||
Type :help for more information.
|
||||
|
||||
scala> // Immutable collections do not and cannot change the instantiated object
|
||||
|
||||
scala> // Lets start with Lists
|
||||
|
||||
scala> val list = Nil // Empty List
|
||||
list: scala.collection.immutable.Nil.type = List()
|
||||
|
||||
scala> val list2 = List("one", "two") // List with two elements (Strings)
|
||||
list2: List[String] = List(one, two)
|
||||
|
||||
scala> val list3 = 3 :: list2 // prepend 3 to list2, using a special operator
|
||||
list3: List[Any] = List(3, one, two)
|
||||
|
||||
scala> // The result was a mixture with a Int and Strings, so the common superclass Any is used.
|
||||
|
||||
scala> // Let test the Set collection
|
||||
|
||||
scala> val set = Set.empty[Char] // Empty Set of Char type
|
||||
set: scala.collection.immutable.Set[Char] = Set()
|
||||
|
||||
scala> val set1 = set + 'c' // add an element
|
||||
set1: scala.collection.immutable.Set[Char] = Set(c)
|
||||
|
||||
scala> val set2 = set + 'a' + 'c' + 'c' // try to add another and the same element twice
|
||||
set2: scala.collection.immutable.Set[Char] = Set(a, c)
|
||||
|
||||
scala> // Let's look at the most universal map: TrieMap (Cache-aware lock-free concurrent hash trie)
|
||||
|
||||
scala> val capital = collection.concurrent.TrieMap("US" -> "Washington", "France" -> "Paris") // This map is mutable
|
||||
capital: scala.collection.concurrent.TrieMap[String,String] = TrieMap(US -> Washington, France -> Paris)
|
||||
|
||||
scala> capital - "France" // This is only an expression, does not modify the map itself
|
||||
res0: scala.collection.concurrent.TrieMap[String,String] = TrieMap(US -> Washington)
|
||||
|
||||
scala> capital += ("Tokio" -> "Japan") // Adding an element, object is changed - not the val capital
|
||||
res1: capital.type = TrieMap(US -> Washington, Tokio -> Japan, France -> Paris)
|
||||
|
||||
scala> capital // Check what we have sofar
|
||||
res2: scala.collection.concurrent.TrieMap[String,String] = TrieMap(US -> Washington, Tokio -> Japan, France -> Paris)
|
||||
|
||||
scala> val queue = new scala.collection.mutable.Queue[String]
|
||||
queue: scala.collection.mutable.Queue[String] = Queue()
|
||||
|
||||
scala> queue += "first"
|
||||
res17: queue.type = Queue("first")
|
||||
|
||||
scala> queue += "second"
|
||||
res19: queue.type = Queue("first", "second")
|
||||
|
||||
scala>
|
||||
30
Task/Collections/Scala/collections-2.scala
Normal file
30
Task/Collections/Scala/collections-2.scala
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
import collection.concurrent.TrieMap
|
||||
|
||||
// super concurrent mutable hashmap
|
||||
val map = TrieMap("Amsterdam" -> "Netherlands",
|
||||
"New York" -> "USA",
|
||||
"Heemstede" -> "Netherlands")
|
||||
|
||||
map("Laussanne") = "Switzerland" // 2 Ways of updating
|
||||
map += ("Tokio" -> "Japan")
|
||||
|
||||
assert(map("New York") == "USA")
|
||||
assert(!map.isDefinedAt("Gent")) // isDefinedAt is false
|
||||
assert(map.isDefinedAt("Laussanne")) // true
|
||||
|
||||
val hash = new TrieMap[Int, Int]
|
||||
hash(1) = 2
|
||||
hash += (1 -> 2) // same as hash(1) = 2
|
||||
hash += (3 -> 4, 5 -> 6, 44 -> 99)
|
||||
hash(44) // 99
|
||||
hash.contains(33) // false
|
||||
hash.isDefinedAt(33) // same as contains
|
||||
hash.contains(44) // true
|
||||
// iterate over key/value
|
||||
// hash.foreach { case (key, val) => println( "key " + e._1 + " value " + e._2) } // e is a 2 element Tuple
|
||||
// same with for syntax
|
||||
for ((k, v) <- hash) println("key " + k + " value " + v)
|
||||
// // items in map where the key is greater than 3
|
||||
map.filter { k => k._1 > 3 } // Map(5 -> 6, 44 -> 99)
|
||||
// // same with for syntax
|
||||
for ((k, v) <- map; if k > 3) yield (k, v)
|
||||
Loading…
Add table
Add a link
Reference in a new issue