Scala List FAQ: How do I merge a List in Scala?

NOTE: I wrote the solutions shown below a long time ago, and they are not optimal. I'll update this article when I have more time. The best approach is to prepend one List to the beginning of another List with the :: method.

There are at least three ways to merge/concatenate Scala List instances, as shown in the examples below.

1) The Scala List ::: method

First, you can merge two Scala lists using the ::: method of the List class, as demonstrated here at the Scala command prompt:

scala> val a = List(1,2,3)
a: List[Int] = List(1, 2, 3) scala> val b = List(4,5,6)
b: List[Int] = List(4, 5, 6) scala> val c = a ::: b
c: List[Int] = List(1, 2, 3, 4, 5, 6)

This operation is said to have O(n) speed, where n is the number of elements in the first List.

2) The Scala List concat method

You can also merge two Scala lists using the List class concat method:

scala> val a = List(1,2,3)
a: List[Int] = List(1, 2, 3) scala> val b = List(4,5,6)
b: List[Int] = List(4, 5, 6) scala> val c = List.concat(a, b)
c: List[Int] = List(1, 2, 3, 4, 5, 6)

3) The Scala List ++ method

You can also use the List ++ method to concatenate Scala Lists, as shown here:

scala> val a = List(1,2,3)
a: List[Int] = List(1, 2, 3) scala> val b = List(4,5,6)
b: List[Int] = List(4, 5, 6) scala> val c = a ++ b
c: List[Int] = List(1, 2, 3, 4, 5, 6)

I'll try to add more information on these three approaches as I learn about them, but for now, I just wanted to share these three approaches.

How to merge Scala Lists的更多相关文章

  1. 【LeetCode算法题库】Day7:Remove Nth Node From End of List & Valid Parentheses & Merge Two Lists

    [Q19] Given a linked list, remove the n-th node from the end of list and return its head. Example: G ...

  2. LeetCode第[21][23]题(Java):Merge Sorted Lists

    题目:合并两个已排序链表 难度:Easy 题目内容: Merge two sorted linked lists and return it as a new list. The new list s ...

  3. Can you share some Scala List class examples?

    Scala List FAQ: Can you share some Scala List class examples? The Scala List class may be the most c ...

  4. LeetCode: Merge k Sorted Lists 解题报告

    Merge k Sorted Lists Merge k sorted linked lists and return it as one sorted list. Analyze and descr ...

  5. Merge k Sorted Lists——分治与堆排序(需要好好看)

    Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 1 ...

  6. Beginning Scala study note(6) Scala Collections

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

  7. Scala详解---------数组、元组、映射

    一.数组 1.定长数组 声明数组的两种形式: 声明指定长度的数组 val 数组名= new Array[类型](数组长度) 提供数组初始值的数组,无需new关键字 Scala声明数组时,需要带有Arr ...

  8. Leetcode: Merge k Sorted List

    Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 参 ...

  9. 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 yo ...

随机推荐

  1. MySQL工具1:mysqladmin

    每两秒显示一下MySQL的状态,一共显示5次. # mysqladmin -uroot -p -i 2 -c 5 status 查看MySQL的运行状态: #mysqladmin -uroot -p ...

  2. webpack 处理CSS

    1.安装插件 npm i style-loader css-loader --save-dev npm i postcss-loader --save-dev npm i autoprefixer - ...

  3. 解决Unable to load component class org.sonar.scanner.report.ActiveRulesPublisher/Unable to load component interface org.sonar.api.batch.rule.ActiveRules: NullPointerException

    解决办法 Delete the directory data/es in your SonarQube installation. Restart SonarQube.

  4. Protobuf学习 - 入门(转)

    从公司的项目源码中看到了这个东西,觉得挺好用的,写篇博客做下小总结.下面的操作以C++为编程语言,protoc的版本为libprotoc 3.2.0. 一.Protobuf? 1. 是什么?  Goo ...

  5. 【Oracle】将表名与字段名连接成一行数据展示,字段名使用顿号的分隔

    select '<'||a.comments||'>:'||replace(wmsys.wm_concat(b.comments),',','.')||'.' as pjzf from u ...

  6. JAVA虚拟机内存架构

    Java在执行Java程序的过程中会把它所管理的内存划分为若干个不同的数据区域.这些区域都有各自的用途.创建和销毁的时间,有一些是随虚拟机的启动而创建,随虚拟机的退出而销毁,有些则是与线程一一对应,随 ...

  7. 通过配置CPU参数 worker_cpu_affinity 提升nginx性能

    简介 Nginx默认没有开启利用多核cpu,我们可以通过增加worker_cpu_affinity配置参数来充分利用多核cpu的性能.cpu是任务处理,计算最关键的资源,cpu核越多,性能就越好. 规 ...

  8. 如何在网上隐藏自己的IP地址(转)

    摘自:http://net.yesky.com/396/3082396.shtml 在某些场合(例如论坛发贴时)有些网友希望能隐藏自己的IP地址,以免IP被记下,为安全起见,QQ聊天时也不希望别人知道 ...

  9. java项目中classpath路径到底指的是哪里?

    本文转自:http://blog.csdn.net/javaloveiphone/article/details/51994268 1.src不是classpath, WEB-INF/classes和 ...

  10. mysql数据库的数据类型及约束

     本文转自:http://www.cnblogs.com/zbseoag/archive/2013/03/19/2970004.html 1.整型 MySQL数据类型 含义(有符号) tinyint( ...