scala.collection 集合

一、immutable 不可变集合

package main

import scala.collection.immutable;


object Test {

  def main(args: Array[String]): Unit = {

    val t = immutable.Traversable(1, 3, 5, 7, 9, 2, 4, 6, 8, 10, 9, 8);
    println(t.getClass.getName); //scala.collection.immutable.$colon$colon
    println(t);
    //List(1, 3, 5, 7, 9, 2, 4, 6, 8, 10, 9, 8)


    val seq = immutable.Seq(1, 3, 5, 7, 9, 2, 4, 6, 8, 10, 9, 8);
    println(seq.getClass.getName); //scala.collection.immutable.$colon$colon
    println(seq);
    //List(1, 3, 5, 7, 9, 2, 4, 6, 8, 10, 9, 8)


    val indexedSeq = immutable.IndexedSeq(1, 3, 5, 7, 9, 2, 4, 6, 8, 10, 9, 8);
    println(indexedSeq.getClass.getName); //scala.collection.immutable.Vector
    println(indexedSeq);
    //Vector(1, 3, 5, 7, 9, 2, 4, 6, 8, 10, 9, 8)


    val linearSeq = immutable.LinearSeq(1, 3, 5, 7, 9, 2, 4, 6, 8, 10, 9, 8);
    println(linearSeq.getClass.getName); //scala.collection.immutable.$colon$colon
    println(linearSeq);
    //List(1, 3, 5, 7, 9, 2, 4, 6, 8, 10, 9, 8)

    val vector = immutable.Vector(1, 3, 5, 7, 9, 2, 4, 6, 8, 10, 9, 8);
    println(vector.getClass.getName); //scala.collection.immutable.Vector
    println(vector);
    //Vector(1, 3, 5, 7, 9, 2, 4, 6, 8, 10, 9, 8)

    val list = immutable.List(1, 3, 5, 7, 9, 2, 4, 6, 8, 10, 9, 8);
    println(list.getClass.getName); //scala.collection.immutable.$colon$colon
    println(list);
    //List(1, 3, 5, 7, 9, 2, 4, 6, 8, 10, 9, 8)


   println();
   println(); val sortedSet = immutable.SortedSet(1, 3, 5, 7, 9, 2, 4, 6, 8, 10, 9, 8); println(sortedSet.getClass.getName); //scala.collection.immutable.TreeSet println(sortedSet); //TreeSet(1, 2, 3, 4, 5, 6, 7, 8, 9, 10) val treeSet = immutable.TreeSet(1, 3, 5, 7, 9, 2, 4, 6, 8, 10, 9, 8); println(treeSet.getClass.getName); //scala.collection.immutable.TreeSet println(treeSet); //TreeSet(1, 2, 3, 4, 5, 6, 7, 8, 9, 10) val hashSet = immutable.HashSet(1, 3, 5, 7, 9, 2, 4, 6, 8, 10, 9, 8); println(hashSet.getClass.getName); //scala.collection.immutable.HashSet$HashTrieSet println(hashSet); //Set(5, 10, 1, 6, 9, 2, 7, 3, 8, 4) val listSet = immutable.ListSet(1, 3, 5, 7, 9, 2, 4, 6, 8, 10, 9, 8); println(listSet.getClass.getName); //scala.collection.immutable.ListSet$Node println(listSet); //ListSet(1, 3, 5, 7, 9, 2, 4, 6, 8, 10) } }

 

二、mutable 可变集合 

package main

import scala.collection.mutable;


object Test {

  def main(args: Array[String]): Unit = {

    val t = mutable.Traversable(1, 3, 5, 7, 9, 2, 4, 6, 8, 10, 9, 8);
    println(t.getClass.getName); //scala.collection.mutable.ArrayBuffer
    println(t);
    //ArrayBuffer(1, 3, 5, 7, 9, 2, 4, 6, 8, 10, 9, 8)


    val seq = mutable.Seq(1, 3, 5, 7, 9, 2, 4, 6, 8, 10, 9, 8);
    println(seq.getClass.getName); //scala.collection.mutable.ArrayBuffer
    println(seq);
    //ArrayBuffer(1, 3, 5, 7, 9, 2, 4, 6, 8, 10, 9, 8)


    val indexedSeq = mutable.IndexedSeq(1, 3, 5, 7, 9, 2, 4, 6, 8, 10, 9, 8);
    println(indexedSeq.getClass.getName); //scala.collection.mutable.ArrayBuffer
    println(indexedSeq);
    //ArrayBuffer(1, 3, 5, 7, 9, 2, 4, 6, 8, 10, 9, 8)


    val linearSeq = mutable.LinearSeq(1, 3, 5, 7, 9, 2, 4, 6, 8, 10, 9, 8);
    println(linearSeq.getClass.getName); //scala.collection.mutable.MutableList
    println(linearSeq);
    //MutableList(1, 3, 5, 7, 9, 2, 4, 6, 8, 10, 9, 8)


    val listBuffer = mutable.ListBuffer(1, 3, 5, 7, 9, 2, 4, 6, 8, 10, 9, 8);
    println(listBuffer.getClass.getName); //scala.collection.mutable.ListBuffer
    println(listBuffer);
    //ListBuffer(1, 3, 5, 7, 9, 2, 4, 6, 8, 10, 9, 8)


    val arrayBuffer = mutable.ArrayBuffer(1, 3, 5, 7, 9, 2, 4, 6, 8, 10, 9, 8);
    println(arrayBuffer.getClass.getName); //scala.collection.mutable.ArrayBuffer
    println(arrayBuffer);
    //ArrayBuffer(1, 3, 5, 7, 9, 2, 4, 6, 8, 10, 9, 8)


    println()
    println()


    val linkedHashSet = mutable.LinkedHashSet(1, 3, 5, 7, 9, 2, 4, 6, 8, 10, 9, 8);
    println(linkedHashSet.getClass.getName); //scala.collection.mutable.LinkedHashSet
    println(linkedHashSet);
    //Set(1, 3, 5, 7, 9, 2, 4, 6, 8, 10)


    val sortedSet = mutable.SortedSet(1, 3, 5, 7, 9, 2, 4, 6, 8, 10, 9, 8);
    println(sortedSet.getClass.getName); //scala.collection.mutable.TreeSet
    println(sortedSet);
    //TreeSet(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)


    val treeSet = mutable.TreeSet(1, 3, 5, 7, 9, 2, 4, 6, 8, 10, 9, 8);
    println(treeSet.getClass.getName); //scala.collection.mutable.TreeSet
    println(treeSet);
    //TreeSet(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)


    val hashSet = mutable.HashSet(1, 3, 5, 7, 9, 2, 4, 6, 8, 10, 9, 8);
    println(hashSet.getClass.getName); //scala.collection.mutable.HashSet
    println(hashSet);
    //Set(9, 1, 5, 2, 6, 3, 10, 7, 4, 8)
  }
}

 

posted @ 2019-07-23 01:14  茗::流  阅读(401)  评论(0)    收藏  举报
如有雷同,纯属参考。如有侵犯你的版权,请联系我。