Scala List FAQ: How do I add elements to a Scala List?

This is actually a trick question, because you can't add elements to a ScalaList; it's an immutable data structure, like a Java String.

Prepending elements to Scala Lists

One thing you can do when working with a Scala List is to create a newList from an existing List. This sort of thing is done often in functional programming, and the general approach looks like this:

scala> val p1 = List("Kim")
p1: List[String] = List(Kim) scala> val p2 = "Julia" :: p1
p2: List[String] = List(Julia, Kim) scala> val p3 = "Judi" :: p2
p3: List[String] = List(Judi, Julia, Kim)

Those examples show how to create a series of lists. The initial list namedp1 contains one string, then p2 contains two strings, and finally p3 contains three strings.

While that approach looks cumbersome in a small example, it makes sense in larger, real-world code. You can see more/better examples of this approach in my tutorial titled, Scala List class examples.

Use a ListBuffer when you want a "List" you can modify

If you want to use a Scala sequence that has many characteristics of a Listand is also mutable (you can add and remove elements in it), use theListBuffer class instead, like this:

import scala.collection.mutable.ListBuffer

var fruits = new ListBuffer[String]()
fruits += "Apple"
fruits += "Banana"
fruits += "Orange"

Then convert it to a List if/when you need to:

val fruitsList = fruits.toList

Scala REPL example

Here's what this List and ListBuffer example looks like using the Scala command line (REPL):

scala> import scala.collection.mutable.ListBuffer
import scala.collection.mutable.ListBuffer scala> var fruits = new ListBuffer[String]()
fruits: scala.collection.mutable.ListBuffer[String] = ListBuffer() scala> fruits += "Apple"
res0: scala.collection.mutable.ListBuffer[String] = ListBuffer(Apple) scala> fruits += "Banana"
res1: scala.collection.mutable.ListBuffer[String] = ListBuffer(Apple, Banana) scala> fruits += "Orange"
res2: scala.collection.mutable.ListBuffer[String] = ListBuffer(Apple, Banana, Orange) scala> val fruitsList = fruits.toList
fruitsList: List[String] = List(Apple, Banana, Orange)
 

More functional ways to work with Scala lists

Depending on your needs, there are other, "more functional" ways to work with Scala lists, and I work through some of those in my Scala List examples. But for my needs today, I just wanted to work with a Scala Listlike I'd work with a Java List (ArrayListLinkedList), and this approach suits me.

How do I add elements to a Scala List?的更多相关文章

  1. How to add elements to a List in Scala

    Scala List FAQ: How do I add elements to a Scala List? This is actually a trick question, because yo ...

  2. openmesh - src - trimesh delete and add elements

    openmesh - src - trimesh delete and add elements openmesh 版本 8.1 About 本文主要介绍openmesh的如下接口 add_verte ...

  3. select 下拉菜单Option对象使用add(elements,index)方法动态添加

    原生js 的add函数为下拉菜单增加选项 1.object.add(oElement [, iIndex]) index 可选参数:指定元素放置所在的索引号,整形值.如果没有指定值,将添加到集合的最后 ...

  4. Beginning Scala study note(6) Scala Collections

    Scala's object-oriented collections support mutable and immutable type hierarchies. Also support fun ...

  5. Spark记录-Scala类和对象

    本章将介绍如何在Scala编程中使用类和对象.类是对象的蓝图(或叫模板).定义一个类后,可以使用关键字new来创建一个类的对象. 通过对象可以使用定义的类的所有功能. 下面的图通过一个包含成员变量(n ...

  6. Scala教程之:可变和不变集合

    文章目录 mutable HashMap immutable HashMap 集合在程序中是非常有用的,只有用好集合才能真正感受到该语言的魅力.在scala中集合主要在三个包里面:scala.coll ...

  7. Beginning Scala study note(9) Scala and Java Interoperability

    1. Translating Java Classes to Scala Classes Example 1: # a class declaration in Java public class B ...

  8. Scala中Zip相关的函数

    在Scala中存在好几个Zip相关的函数,比如zip,zipAll,zipped 以及zipWithIndex等等.我们在代码中也经常看到这样的函数,这篇文章主要介绍一下这些函数的区别以及使用. 1. ...

  9. Scala之List,Set及Map基本操作

    package big.data.analyse.dataSet import scala.collection.immutable.{TreeMap, TreeSet} import scala.c ...

随机推荐

  1. tomcat启用压缩的方式

    <Connector port="7070" protocol="HTTP/1.1"connectionTimeout="20000" ...

  2. fis3 scss 版本报错

    fis3 scss编译需要安装的node版本为4.x,node版本高了fis会报错.如下图所示:

  3. hosts.allow hosts.deny

    简单控制访问ip,hosts.allow/hsots.deny  a simple access control language that is based on client (host name ...

  4. 商业规则引擎IBM WebSphere ILog JRules概述,开发基础教程

    Ilog Jrules开发基础教程有7篇,地址规则引擎Ilog Jrules开发基础教程[连载1]-- 概述篇 概述篇 规则引擎是一种嵌套在应用程序中的组件,它实现了将业务规则从应用程序代码中分离出来 ...

  5. 解决IIS动态内容压缩的问题

    转:http://www.cnblogs.com/cmt/archive/2013/03/10/iis-dynamic-dompression-mime.html

  6. 腾讯云HTTPS设置管理

    腾讯云HTTPS解决方案:腾讯云针对现有用户,提供HTTPS的安全加密方案.腾讯云HTTPS有两种解决方法:客户自带证书和腾讯提供域名和证书. 腾讯云HTTPS解决方法客户提供证书和私钥,托管至腾讯云 ...

  7. Redis下载和安装

    原文地址:https://redis.io/download Download Redis uses a standard practice for its versioning: major.min ...

  8. Android广播BroadcastReceiver

    Android 系统里定义了各种各样的广播,如电池的使用状态,电话的接收和短信的接收,开机启动都会产生一个广播.当然用户也可以自定义自己的广播. 既然说到广播,那么必定有一个广播发送者,以及广播接收器 ...

  9. ubuntu14中创建python虚拟环境

    一.安装python-virtualenv包 sudo apt-get install python-virtualenv 安装完成后,创建一个虚拟环境文件夹. mkdir VENVcd VENV 创 ...

  10. MATLAB(1)——基本调试方法(Debug)

    作者:桂. 时间:2017-02-28  07:06:30 链接:http://www.cnblogs.com/xingshansi/articles/6477185.html 声明:转载请注明出处, ...