On the scala collection

Always do not understand what the hell is functional programming (this is in their future use scala slowly It is important to understand, and feel almost a little js), it just feels strange syntax, probably more important to write about feeling something.

3 Ways 1. List the brief statement

1> common declarative way: var list

2>: colon :( 2) + Nil manner: val data2 = 10: :( 20 :: 30 :: 15 :: Nil)

3> and binding mode

a commonly used list order function:

 tail: List collection collection of other elements other than the first element

init: List collection collection of other elements other than the last element

::: meaning: a plurality List collection together, the synthesis of a new collection

take (n): obtaining a set of first n elements

drop (n): the n elements removed before the remaining elements of the set of List

splitAt (n): The List collection split into several sets

head: returns the first element set List

last: Returns the last element in the set list

The number of elements in a set List: length

reverse: List the set of the elements in reverse

indices: Index List Remove all elements corresponding to the set

zip: the fastener may be a plurality of collection operation, pairing is

zipWithIndex: List the elements and the index of a new set of paired constitution

mkString: The List elements of the collection according to a specified output format, when mkString method in the formatted output data is useful in spark

toIterator: Returns the iterator can traverse the collection of all elements of the current

toList method is usually carried out in conjunction with the map and flatMap, toListz role in particular string above list may be set into a string of characters from the set list

map is the result of performing a specific function of the set of all the elements, will act on the value of the function, and returns a new set.

flatMap: First base member for each map operation inside a List, and then produces results, are combined, after generating an integrated set.

foreach: performing a specific function is set for each element, acting on the value of a function, the map is different, the foreach no concrete results, the result is a type of Unit.

filter: the filter element eligible list, filter parameters passed in the structure is a structure of type Boolean function.

List common operation set

partition: to partition the set of elements in accordance with certain conditions

span: spand operation is similar partition, of a collection into different areas

find: identify a set of the first element satisfies the condition, the return value Some or None

takeWhile: Gets a collection of all the elements which meet the conditions

dropWhile: obtaining a set of elements which satisfy the condition other than

forall: Returns true only if all the elements of the collection which satisfies the condition, false otherwise

exists: As long as there is an element in the set satisfy the condition returns true

Brief List of foldLeft foldRight sort function specific use

foldLeft function body performs two elements from left to right, foldRight: performing a two-element body of the function operation from right to left

sortWith efficiency of sorting algorithms in scala is still very high, linear

Objects commonly used method in the Associated List:

List itself is an abstract class, an abstract class instance object can not be defined at the abstract class, only the definition of the abstract class reference

List of the object (companion object) commonly used methods:

apply: a configured instance of the object List

range: Specifies a range of left and right to open and close the List object, and can specify an increment between two elements

zip: Operation zipper unzip: Results Anti collection operation before the zip fastener obtained is the inverse operation, to generate a set of two List

flatten and concat: List all the elements combine to form the internal set a new collection, which constitute a unified list

ListBuffer ArrayBuffer are the new version of an Array List

Queue (queue) and Stack (stack) are two classical data structure

scala set in a variable and non-variable set points, is immutable collection itself is immutable, immutable set for each operation will generate a new set.

By default, scala encouraged us to use an immutable object programming, so the default class objects are immutable introduced in the header file.

For Set Map, the following elements are not put in order, if desired, after the elements are put in sequential data can be stored treeMap treeSet and, usually in the class object into the internal storage structure Tree container

Corresponding class objects ascending order







Guess you like

Origin blog.csdn.net/baibaichenchen/article/details/74178155