1. The zipper (zip)
And a set of two, may be used in combination fastener element dual
object Demo_031 { def main(args: Array[String]): Unit = { val list1 = List(1, 2 ,3) val list2 = List(4, 5, 6) val list3 = list1.zip(list2) // (1,4),(2,5),(3,6) println("list3=" + list3) } }
Export
Precautions
- The combined operation of the fastener is essentially two sets, each of the combined element is a dual-tuple.
- FIG operation rules:
- If the two do not correspond to the number of collections can cause data loss.
- Any set to List, such as may be other sets Array
- Dual respective data tuple to be taken if the combined, can traverse
for (Item <-list3) { Print (item._1 + "" + item._2) // when removed, can be removed in the manner tuple }
2. iterator (Iterator)
Obtaining an iterator set by the iterator method, while traversing through the collection cycle and for expression
Example:
{Demo_032 Object DEF main (args: the Array [String]): Unit = { Val Iterator List = (. 1, 2,. 3,. 4,. 5) to give .iterator // iterator println ( "-------- ----------------- traversal. 1 ") the while (iterator.hasNext) { the println (Iterator.next ()) } the println (" traversal -------- ----------------- embodiment for 2 ") for (enum <- Iterator) { the println (enum) // } } }
3. The flow (stream)
stream is a collection. This set can be used to store an infinite number of elements, but it does not produce an infinite number of elements in a one-time out, but need to use much of the range, it will be dynamic production, the last element loaded lazy to follow the rules (when using )
Use tail, to dynamically stream a set of rules generated by new elements
{Demo_033 Object DEF main (args: the Array [String]): Unit = { DEF numsForm (n-: the BigInt): Stream [the BigInt] :: = # n-numsForm (n-+. 1) Val numsForm STREAM1 = (. 1) the println (STREAM1 ) // // take the first element the println ( "head =" + stream1.head) // the println (stream1.tail) // Note unavailable stream.last, go into an infinite loop or the println (STREAM1) // } }
operation result
Example: Use map mapping element stream and some calculations
object Demo_034 { def main(args: Array[String]): Unit = { def numsForm(n: BigInt) : Stream[BigInt] = n #:: numsForm(n + 1) def multi(x:BigInt) : BigInt = { x * x } println(numsForm(5).map(multi)) //? (25,?) } }
Export
4. view (view)
Stream lazy loading characteristics, but also can be obtained similar effects to the other set of applications view method, it has the following characteristics:
- view the process yielded a set of always being lazy execution.
- When the view is not cached data is recalculated every time, such as traversing View.
Examples: Please find 1-100, numbers in reverse order and the number itself all the same?. (12, 11, 22, 33 ...)
object Demo_035 { def main(args: Array[String]): Unit = { def eq(i: Int): Boolean = { i.toString.equals(i.toString.reverse) } //说明: 没有使用view val viewSquares1 = (1 to 100).filter(eq) println(viewSquares1) //使用view val viewSquares2 = (1 to 100).view.filter(eq) println(viewSquares2) } }
Output
The set of parallel
Applications parallel
- Printing 1-5
object Demo_036 {
def main(args: Array[String]): Unit = {
def method(i:Int): Unit ={
print("\t"+i)
}
(1 to 5).foreach(method)
println()
(1 to 5).par.foreach(method)//并行方式输出
}
}
Export
- View the collection element access parallel threads
val result1 = (0 to 100).map{case _ => Thread.currentThread.getName} val result2 = (0 to 100).par.map{case _ => Thread.currentThread.getName} println(result1) println(result2)