How to merge Scala Lists
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的更多相关文章
- 【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 ...
- LeetCode第[21][23]题(Java):Merge Sorted Lists
题目:合并两个已排序链表 难度:Easy 题目内容: Merge two sorted linked lists and return it as a new list. The new list s ...
- 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 ...
- LeetCode: Merge k Sorted Lists 解题报告
Merge k Sorted Lists Merge k sorted linked lists and return it as one sorted list. Analyze and descr ...
- Merge k Sorted Lists——分治与堆排序(需要好好看)
Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 1 ...
- Beginning Scala study note(6) Scala Collections
Scala's object-oriented collections support mutable and immutable type hierarchies. Also support fun ...
- Scala详解---------数组、元组、映射
一.数组 1.定长数组 声明数组的两种形式: 声明指定长度的数组 val 数组名= new Array[类型](数组长度) 提供数组初始值的数组,无需new关键字 Scala声明数组时,需要带有Arr ...
- Leetcode: Merge k Sorted List
Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 参 ...
- 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 ...
随机推荐
- Linux库的创建和使用
Linux库的概念 库是一种软件组建技术,里面封装了数据和函数,提供给用户程序调用.使用库能够使程序模块化,提高编译速度,实现代码重用,易于升级. Windows系统提供了大量静态链接库(.lib)和 ...
- 监测CentOS下TCP断线
TCP正常的断开,通信双方(服务端和客户端)都是能知道的.但是非正常的断开,比如直接拔掉了网线,就只能靠如下两种方法,实现短时间内的检测. 一.心跳包机制 心跳包机制,是网游设计中的常用机制.从用户层 ...
- 为什么要有GDT
逻辑地址-------------->线性地址------------> 物理地址 分段 分页 GDT是[gobal (segment) descriptor table]的缩写,它保 ...
- EXCEPTION-javaBean
CreateTime--2016年11月24日14:29:43Author:Marydon 声明:异常类文章主要是记录了我遇到的异常信息及解决方案,解决方案大部分都是百度解决的,(这里只是针对我遇 ...
- centos 6.4 调整home和root分区大小
调整过程中可以随时查看硬盘分区情况,命令: lsblk df -h 压缩home分区到5G: [root@fscp-dev /]# df -h 文件系统 容量 已用 可用 已用%% 挂载点 /dev/ ...
- 微信小程序开发动感十足的加载动画--都在这里!
代码地址如下:http://www.demodashi.com/demo/14242.html 一.前期准备工作 软件环境:微信开发者工具 官方下载地址:https://mp.weixin.qq.co ...
- eclipse中英文版转换(前提:有中文包)
均为命令行启动(一次就可以) 中文版启动:eclipse.exe -nl zh 英文版启动:eclipse.exe -nl en
- nginx反向代理proxy_set_header自定义header头无效
公司使用nginx作为负载均衡,有时候需要自定义header头发送给后端的真实服务器. 想过去应该是非常的简单的事情. 例子如下: 设置代理服务器ip头 1 proxy_set_header X- ...
- JedisClient(示例)
拷贝 import java.io.IOException; import java.util.HashSet; import java.util.Map; import java.util.Set; ...
- java.sql.SQLException: 索引中丢失 IN 或 OUT 参数:: 1
String sql1 = "insert into TEST_RELEVANCEEXPORT" + " (ID, YYCSDM, YYCSMC, ...