Operators for Collections in Scala (v2)

Short reminder of operators for collections in the Scala, version 2. Previous version: Operators for Collections in Scala

Operators for deleting and adding elements

Operator Description Collection type
coll :+ elem Adds elem element at the end of coll collection. Returns collection of same type as coll. Seq
elem +: coll Adds elem element at the beginning of coll collection. Returns collection of same type as coll. Seq
coll + elem

coll + (e1, e2, …)

Adds elem(s) and returns collection of same type as coll Set, Map
coll elem

coll – (e1, e2, …)

Deletes elem(s) and returns collection of same type as coll Set, Map
coll ++ coll2

coll ++: coll2

Adds collection and returns collection of same type as coll Iterable
coll coll2 Deletes collection and returns collection of same type as coll Set, Map, ArrayBufffer
elem :: lst

 

Adds elem element at the beginning of lst. Returns collection of the same type as lst. List
lst2 ::: lst Adds all elements from lst2 at the beginning of lst. Returns collection of the same type as lst. List
set | set2 Joins two sets and returns collection of same type as set. Set
set & set2 Intercepts two sets and returns collection of same type as set. Set
set &~ set2 Result is the set difference and has same type as set. Set
coll += elem

coll += (e1, e2, …)

Adds element(s) to collection and updates the collection. Returns changed collection. Mutable collections
coll -= elem

coll -= (e1, e2, …)

Deletes element(s) from collection and updates the collection. Returns changed collection. Mutable collections.
coll ++= coll2 Adds elements to collection and updates the collection. Returns changed collection. Mutable collections.
coll –= coll2 Deletes elements from collection and updates the collection. Returns changed collection. Mutable collections.
elem +=: coll Adds element to collection at the beginning. Returns changed collection. ArrayBuffer
coll2 ++=: coll Adds elements of coll2 to coll collection at the beginning. Returns changed coll collection. ArrayBuffer

 

Code sample is here Code sample

Advertisements

About ysden123

We are not only for work... All my time free from work I spend on photography. Most photos are from our travels. I like make photos in still life style as well.
This entry was posted in Programming, Scala and tagged , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s