09.10.2014 Views

Download Scala Tutorial (PDF Version) - Tutorials Point

Download Scala Tutorial (PDF Version) - Tutorials Point

Download Scala Tutorial (PDF Version) - Tutorials Point

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Displays all elements of the list in a string using a separator string.<br />

34<br />

35<br />

36<br />

37<br />

38<br />

39<br />

40<br />

41<br />

42<br />

43<br />

44<br />

45<br />

def reverse: List[A]<br />

Returns new list with elements in reversed order.<br />

def sorted[B >: A]: List[A]<br />

Sorts the list according to an Ordering.<br />

def startsWith[B](that: Seq[B], offset: Int): Boolean<br />

Tests whether the list contains the given sequence at a given index.<br />

def sum: A<br />

Sums up the elements of this collection.<br />

def tail: List[A]<br />

Returns all elements except the first.<br />

def take(n: Int): List[A]<br />

Returns first n elements.<br />

def takeRight(n: Int): List[A]<br />

Returns last n elements.<br />

def toArray: Array[A]<br />

Converts the list to an array.<br />

def toBuffer[B >: A]: Buffer[B]<br />

Converts the list to a mutable buffer.<br />

def toMap[T, U]: Map[T, U]<br />

Converts this list to a map.<br />

def toSeq: Seq[A]<br />

Converts the list to a sequence.<br />

def toSet[B >: A]: Set[B]<br />

Converts the list to a set.<br />

46<br />

def toString(): String<br />

Converts the list to a string.<br />

<strong>Scala</strong> Sets<br />

<strong>Scala</strong> Set is a collection of pairwise different elements of the same type. In other words, a Set is a collection that<br />

contains no duplicate elements. There are two kinds of Sets, the immutable and themutable. The difference<br />

between mutable and immutable objects is that when an object is immutable, the object itself can't be changed.<br />

By default, <strong>Scala</strong> uses the immutable Set. If you want to use the mutable Set, you'll have to import<br />

scala.collection.mutable.Set class explicitly. If you want to use both mutable and immutable sets in the same,<br />

then you can continue to refer to the immutable Set as Set but you can refer to the mutable Set as mutable.Set.<br />

Following is the example to declare immutable Sets as follows:<br />

// Empty set of integer type<br />

var s : Set[Int] = Set()<br />

// Set of integer type<br />

var s : Set[Int] = Set(1,3,5,7)<br />

or<br />

TUTORIALS POINT<br />

Simply Easy Learning

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!