How do I add elements to a Scala List?
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 List
and 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 List
like I'd work with a Java List (ArrayList
, LinkedList
), and this approach suits me.
How do I add elements to a Scala List?的更多相关文章
- 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 ...
- openmesh - src - trimesh delete and add elements
openmesh - src - trimesh delete and add elements openmesh 版本 8.1 About 本文主要介绍openmesh的如下接口 add_verte ...
- select 下拉菜单Option对象使用add(elements,index)方法动态添加
原生js 的add函数为下拉菜单增加选项 1.object.add(oElement [, iIndex]) index 可选参数:指定元素放置所在的索引号,整形值.如果没有指定值,将添加到集合的最后 ...
- Beginning Scala study note(6) Scala Collections
Scala's object-oriented collections support mutable and immutable type hierarchies. Also support fun ...
- Spark记录-Scala类和对象
本章将介绍如何在Scala编程中使用类和对象.类是对象的蓝图(或叫模板).定义一个类后,可以使用关键字new来创建一个类的对象. 通过对象可以使用定义的类的所有功能. 下面的图通过一个包含成员变量(n ...
- Scala教程之:可变和不变集合
文章目录 mutable HashMap immutable HashMap 集合在程序中是非常有用的,只有用好集合才能真正感受到该语言的魅力.在scala中集合主要在三个包里面:scala.coll ...
- 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 ...
- Scala中Zip相关的函数
在Scala中存在好几个Zip相关的函数,比如zip,zipAll,zipped 以及zipWithIndex等等.我们在代码中也经常看到这样的函数,这篇文章主要介绍一下这些函数的区别以及使用. 1. ...
- Scala之List,Set及Map基本操作
package big.data.analyse.dataSet import scala.collection.immutable.{TreeMap, TreeSet} import scala.c ...
随机推荐
- java 验证码生成
import java.awt.Color; import java.awt.Font; import java.io.IOException; import java.io.OutputStream ...
- vue inheritAttrs、$attrs和$listeners使用
inheritAttrs.$attrs和$listeners使用场景: 组件传值,尤其是祖孙组件有跨度的传值. (1)inheritAttrs 属性说明:https://cn.vuejs.org/v2 ...
- openerp发送给群组信息
发送给群组 self.pool.get('mail.group').message_post(cr, uid, [1], body=_('Welcome to ! Please ...
- 一致性 hash 算法( consistent hashing )(转)
consistent hashing 算法早在 1997 年就在论文 Consistent hashing and random trees 中被提出,目前在 cache系统中应用越来越广泛: 1 基 ...
- 学习hibernate笔记
曾经学习java的时候,一開始就学习了hibernate,那时候总认为ssh很高大上,所以就急忙看了下相关视频.只是由于实际须要不高,所以后来一直没有使用上hibernate组件.如今一年过去了,也疯 ...
- Spring MVC Beginner’s Guide勘误表
- 17 submitted: last submission 09 Dec 2016 Page number: 213 Qauntity should be: Quantity Page numbe ...
- define() vs const 该如何选择?
使用 define(),除非考虑到可读性.类常量.或关注微优化 1.在 PHP 中是使用 define() 函数来定义常量,PHP 5.3.0 以后,PHP 中也能够使用 const 关键字来声明常量 ...
- Hibernate学习备忘
1.关于Hibernate异常: org.hibernate.service.jndi.JndiException: Error parsing JNDI name 刚接触Hibernate,调试 ...
- 为Github 托管项目的访问添加SSH keys
为了便于访问远程仓库,各个协作者将自己的本地的项目内容推送到远程仓库中,使用 SSH keys 验证github的好处:不用每次提交代码时都输入用户名和密码. 如果SSH key没有添加到github ...
- 作业调度框架_Quartz
什么是Quartz Quartz是一个完全由Java编写的开源作业调度框架,为在Java应用程序中进行作业调度提供了简单却强大的机制.Quartz允许开发人员根据时间间隔来调度作业.它实现了作业和触发 ...