scala -Collection

scala> val array = Array(1,2,3,4,5,6)
array: Array[Int] = Array(1, 2, 3, 4, 5, 6)

scala> array map { _.toString }
res0: Array[java.lang.String] = Array(1, 2, 3, 4, 5, 6)

scala> val list = List(1,2,3,4,5,6)
list: List[Int] = List(1, 2, 3, 4, 5, 6)

scala> list map { _.toString }
res1: List[java.lang.String] = List(1, 2, 3, 4, 5, 6)
 
 
 
创建容器

使用scala.collection.Seq, scala.collection.immutable.Set, and scala.collection.immutable.Map

scala> val seq = Seq(1,2,3,4,1)
seq: Seq[Int] = List(1, 2, 3, 4, 1)

scala> val set = Set(1,2,3,4,1)
set: scala.collection.immutable.Set[Int] = Set(1, 2, 3, 4)

scala> val map = Map(1 -> "one",2 -> "two", 3 -> "three",2 -> "too")
map: scala.collection.immutable.Map[Int,java.lang.String] = Map((1,one), (2,too), (3,three))
Converting between Java Collections

与Java容器互转

The JavaConversions object provides implicit defs that will allow mostly seamless integration between Java Collections-based APIs and the Scala collections library.

Source

package object collection {
   import scala.collection.generic.CanBuildFrom

   /** Provides a CanBuildFrom instance that builds a specific target collection (`To')
* irrespective of the original collection (`From').
*/
   def breakOut [ From, T, To ]( implicit b : CanBuildFrom [ Nothing, T, To ]) : CanBuildFrom [ From, T, To ] =
     // can't just return b because the argument to apply could be cast to From in b
     new CanBuildFrom [ From, T, To ] {
       def apply ( from : From ) = b . apply ()
       def apply () = b . apply ()
     }
}

package collection {
   /** Collection internal utility functions.
*/
   private [ collection ] object DebugUtils {
     def unsupported ( msg : String ) = throw new UnsupportedOperationException ( msg )
     def noSuchElement ( msg : String ) = throw new NoSuchElementException ( msg )
     def indexOutOfBounds ( index : Int ) = throw new IndexOutOfBoundsException ( index . toString )
     def illegalArgument ( msg : String ) = throw new IllegalArgumentException ( msg )

     def buildString ( closure : ( Any => Unit ) => Unit ) : String = {
       var output = ""
       closure ( output += _ + "\n" )

       output
     }

     def arrayString [ T ]( array : Array [ T ], from : Int , until : Int ) : String = {
       array . slice ( from , until ) map {
         case null => "n/a"
         case x => "" + x
       } mkString " | "
     }
   }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值