Functional programming example on a collection __scala

Source: Internet
Author: User
Tags iterable
Package Com.xing.listfile/** * Created by Dengni on 2017/2/12. * * 1, in Scala's collection system iterable is common trait,iterable requires inheritors to implement some common methods, such as the traversal of elements; * 2,array is a very basic data structure that does not belong to a Scala set; * 3,scal The set system of a is divided into variable set and immutable set; immutable set in scala.collection.immutable packet, variable * set in scala.collection.mutable; * 4, List is a collection of elements, Is immutable: * 1th: The head in the list refers to the first element, tail refers to the list of the remaining elements; * 2nd: Use:: This operator sets the list and other elements together to build a new list * 3rd: if When there is no element in the collection, for example, if there is only one element in the list, then the head is the element itself, and the tail operation returns NIL * 5,linkedlist is a mutable list of elements * 6, set is an element-nil collection, and the element is unordered The elements in the HashSet are variable and not repeatable and cannot be guaranteed in order; * 7,linkedhashset will maintain the insertion order of the elements; * 8, SortedSet will automatically sort the inserted elements;/object Functional_iterabl
    e {def main (args:array[string]) {val range = 1 to Range.foreach (println)//list is a list collection of elements and is not changeable The head in the list is the first element, and the tail refers to the list//use of the remaining elements consisting of the element other than the first element:: This operator combines the list with the other elements to form a new list set///If there are no elements in the Union
, this is nil, such as an element in the collection itself, then the head is the element itself, and the tail operation returns nil Val list = list (1,2,3,4,5)    println (List.head) println (list.tail) println (0::list)//val list = 0::list//list collection is not to be changed//prin TLN (list)//LinkedList is a variable list of elements var linkedlist = scala.collection.mutable.LinkedList (1,2,3,4,5) println (link Edlist.elem) println (Linkedlist.tail) while (LinkedList!=nil) {println (linkedlist.elem) LinkedList =

    Linkedlist.tail} println (linkedlist) LinkedList = linkedlist.+:(9)//This is a collection of what kind of child println (LinkedList) Val set = Set (1,2,3,4)//set is a set of immutable elements that must be prefixed with Val println (set) Val Setmore = set + 1//This set does not add 1 to println (
    Setmore)//hashset is a mutable collection, but the content is not repeatable, and there is no sequential var hashset = Scala.collection.mutable.HashSet (1,2,3) HashSet + 4  HashSet = 4 HashSet + + println (hashset)//linkedhashset maintains the insertion order of elements, and set does not allow elements to repeat Var linkedhashset
    = Scala.collection.mutable.LinkedHashSet (1,2,3,4,4) linkedhashset +=12 Print (linkedhashset)//results do not allow duplicate data Val SortedSet = scala.coLlection.mutable.SortedSet (1, 2, 3,3, 5, 4) println (SortedSet) println (list[string] ("I am into the Spark so much" , "Scala is Powerful"). Flatmap {x => x.split ("")}.map {x => (x, 1)}.map (x => x._2). Reduce (_+_)) println (L Ist[string] ("I am into Spark so much", "Scala is Powerful"). Flatmap {x => x.split ("")}.map {(_, 1)}.map (_._2). reduce
    (_+_)) println (List[string] ("I am into the Spark so much", "Scala is Powerful"). Flatmap {x => x.split ("")}.map {(_, 1)}.map (_._ 2. Reduce ((x,y) =>x + y)) List (0,1,2,3,4,5). foreach {x => println (x)} list (0,1,2,3,4,5). foreach {println ( _)} list (0,1,2,3,4,5). foreach {println _} list (0,1,2,3,4,5). foreach {println} list (0,1,2,3,4,5). foreach (pr

 INTLN)}}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.