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.

时间: 2024-10-16 04:10:13

How to merge Scala Lists的相关文章

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 commonly used data structure in Scala applications. Therefore, it's very helpful to know how create lists, merge lists, select items from lists, operat

leetCode 23. Merge k Sorted Lists (合并k个排序链表) 解题思路和方法

Merge k Sorted Lists Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 思路:此题是由合并两个排序链表演化而来,刚开始,想法比较简单,像求最大公共前缀一样,逐一求解:但是最后超时,所以马上意识到出题方是为了使用归并和分治的方法,故重新写了代码. 代码一(超时未过): /** * Definition for singly-link

【leetcode】Merge k Sorted Lists (归并排序)

题目: Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 解析:合并k个有序链表,最后返回一个总的有序链表,分析并描述其复杂度.该题的实质为归并排序,平均时间复杂度为O(NlogN). Java AC代码: public class Solution { public ListNode mergeKLists(List<ListNode> list

LeetCode: Merge k Sorted Lists 解题报告

Merge k Sorted Lists Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. Show Tags 参考资料: http://blog.csdn.net/linhuanmars/article/details/19899259. SOLUTION 1: 使用分治法.左右分别递归调用Merge K sorted List,然后再使用merg

LeetCode23 Merge k Sorted Lists 把K个有序链表连接成一个

题目: Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 翻译: 把K个有序的链表合成一个,返回. 思路: 这道题和昨天的Merge 2 Lists有些类似.但是k不确定,如果用每个都去遍历的话,肯定是不会通过的. So.可以想到的是归并方法. 还记得大一刚开始组长布置的归并作业,那时候刚入门,看的云里雾里. 正好借此机会把归并好好的复习一下. 先说

Beginning Scala study note(6) Scala Collections

Scala's object-oriented collections support mutable and immutable type hierarchies. Also support functional higher-order operations such as map, filter, and reduce that let you use expression-oriented programming in collections. Higher-order operatio

How do I create a List in Scala?

Scala List class FAQ: How do I create a List in Scala? You can create a Scala List in several different ways, including these approaches: Lisp style Java style Using the List class rangemethod Using the List class fillmethod Using the List classtabul

How do I iterate over a Scala List (or more generally, a sequence) using theforeach method or for loop?

Scala List/sequence FAQ: How do I iterate over a Scala List (or more generally, a sequence) using theforeach method or for loop? There are a number of ways to iterate over a Scala List using theforeach method (which is available to Scala sequences li

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 whe