Skip to content

Instantly share code, notes, and snippets.

@lu911
Last active December 20, 2015 20:59
Show Gist options
  • Save lu911/6194002 to your computer and use it in GitHub Desktop.
Save lu911/6194002 to your computer and use it in GitHub Desktop.
Scala Collection
import scala.collection.mutable.ArrayBuffer
import scala.collection.mutable.LinkedList
import scala.collection.mutable.BitSet
import java.util.LinkedHashSet
object Main {
def main(args: Array[String]): Unit = {
new Collection()
}
}
class Collection {
val methods = List(immutableSeq, mutableSeq, mutableIendexedSeq, immutableIendexedSeq, range, linkedList, set, linkedHashSet, sortedSet, mutableBitSet, immutableBitSet)
// List
def immutableSeq(): Unit = {
val seq = scala.collection.immutable.Seq(0,1,2,3)
val iter = seq.iterator
println("-"*30 + " immutableSeq")
println("seq : " + seq)
println("value-0 : " + seq(0))
printIterator(iter)
}
// ArrayBuffer
def mutableSeq(): Unit = {
val seq = scala.collection.mutable.Seq(0,1,2,3)
val iter = seq.iterator
println("-"*30 + " mutableSeq")
println("seq : " + seq)
println("value-0 : " + seq(0))
println("-- value-0 change --")
seq(0) = 1
println("value-0 : " + seq(0))
printIterator(iter)
}
// Vector
def immutableIendexedSeq(): Unit = {
val indexedSeq = scala.collection.immutable.IndexedSeq(0,1,2,3)
val iter = indexedSeq.iterator
println("-"*30 + " immutableIendexedSeq")
println("indexedSeq : " + indexedSeq)
println("value-0 : " + indexedSeq(0))
printIterator(iter)
}
// ArrayBuffer
def mutableIendexedSeq(): Unit = {
val indexedSeq = scala.collection.mutable.IndexedSeq(0,1,2,3)
val iter = indexedSeq.iterator
println("-"*30 + " mutableIendexedSeq")
println("indexedSeq : " + indexedSeq)
println("value-0 : " + indexedSeq(0))
println("-- value-0 change --")
indexedSeq(0) = 1
println("value-0 : " + indexedSeq(0))
printIterator(iter)
}
// immutable
def range(): Unit = {
val range = Range(0, 4)
val iter = range.iterator
println("-"*30 + " range")
println("range : " + range)
println("value-0 : " + range(0))
printIterator(iter)
}
// mutable
def linkedList(): Unit = {
val linkedList = LinkedList(0,1,2,3)
val iter = linkedList.iterator
println("-"*30 + " linkedList")
println("linkedList : " + linkedList)
println("value-0 : " + linkedList(0))
println("linkedList elem : " + linkedList.elem)
println("linkedList next : " + linkedList.next)
println("-- value-0 change --")
linkedList(0) = 1
println("value-0 : " + linkedList(0))
printIterator(iter)
}
// immutable
def set(): Unit = {
val set = Set(1,2,3,4,5,6)
val iter = set.iterator
println("-"*30 + " set")
println("set : " + set)
println("value-0 : " + set(0))
println("value-1 : " + set(1))
printIterator(iter)
}
// mutable
def linkedHashSet(): Unit = {
val linkedHashSet = scala.collection.mutable.LinkedHashSet(1,2,3,4,5,6)
val iter = linkedHashSet.iterator
println("-"*30 + " linkedHashSet")
println("linkedHashSet : " + linkedHashSet)
println("value-0 : " + linkedHashSet(0))
println("value-1 : " + linkedHashSet(1))
printIterator(iter)
}
// immutable
def sortedSet(): Unit = {
val sortedSet = scala.collection.immutable.SortedSet(6,5,4,3,2,1)
val iter = sortedSet.iterator
println("-"*30 + " sortedSet")
println("sortedSet : " + sortedSet)
println("value-0 : " + sortedSet(0))
println("value-1 : " + sortedSet(1))
printIterator(iter)
}
def mutableBitSet(): Unit = {
val bitSet = scala.collection.mutable.BitSet(1,2,3,4,5,6)
val iter = bitSet.iterator
println("-"*30 + " bitSet")
println("bitSet : " + bitSet)
println("value-0 : " + bitSet(0))
println("value-1 : " + bitSet(1))
println("-- value add --")
bitSet.add(7)
println("bitSet : " + bitSet)
printIterator(iter)
}
def immutableBitSet(): Unit = {
val bitSet = scala.collection.immutable.BitSet(1,2,3,4,5,6)
val iter = bitSet.iterator
println("-"*30 + " bitSet")
println("bitSet : " + bitSet)
println("value-0 : " + bitSet(0))
println("value-1 : " + bitSet(1))
printIterator(iter)
}
def printIterator(iter: Iterator[Int]): Unit = {
while(iter.hasNext) println(iter.next)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment