简体   繁体   English

在Scala中创建List容器对象的最佳方法

[英]Best way to create a List container object in Scala

Here's the scenario. 这是场景。 I am creating a simple session handler in Scala and I need a class that can store lists. 我在Scala中创建一个简单的会话处理程序,我需要一个可以存储列表的类。 The class needs other functions associated with it to function properly. 该类需要与其相关的其他功能才能正常运行。

I will be accessing sessions by a session ID I will rarely be traversing the list I will be constantly adding and removing from the list 我将通过会话ID访问会话我将很少遍历列表,我将不断添加并从列表中删除

My questions: 我的问题:

  1. What is the proper Scala object to use for this situation? 在这种情况下使用的Scala对象是什么?
  2. What is the best way to add or remove an entity from said Scala object? 从Scala对象添加或删除实体的最佳方法是什么?

I am fairly new to Scala so please forgive the elementary question I might be asking. 我对Scala相当新,所以请原谅我可能要问的基本问题。 Any assistance would be most appreciated. 任何援助都将非常感激。

Edit: To add to it all...Thread Safty is a factor. 编辑:要添加全部...线程安全是一个因素。 The object used must be thread safe or it must be easy to allow for thread safty when adding and removing items by Session ID. 使用的对象必须是线程安全的,或者在按会话ID添加和删除项目时,必须容易允许线程安全。

您可以使用java.util.concurrent.ConcurrentHashMap - 它具有最佳性能和保证线程安全性。

You can use the immutable implementation of HashSet which operations of adding and removing take effectively constant time. 您可以使用HashSet不可变实现,其中添加和删除操作可以有效地保持时间。

Once this collection is immutable, you'll need to learn the "scala way" of working with collections, how to deal with state and so on. 一旦这个集合是不可变的,你将需要学习使用集合的“scala方式”,如何处理状态等等。 Maybe you'll need to change the way you're working the collections, but this way you won't need to worry about concurrency. 也许你需要改变你处理集合的方式,但这样你就不用担心并发了。

val list = new List(1,2,3,4,5,6,7,8,9,10)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM