Commander
Commander

Reputation: 1322

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

My questions:

  1. What is the proper Scala object to use for this situation?
  2. What is the best way to add or remove an entity from said Scala object?

I am fairly new to Scala so please forgive the elementary question I might be asking. 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.

Upvotes: 1

Views: 397

Answers (3)

boycod3
boycod3

Reputation: 5313

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

Upvotes: 1

Juliano Alves
Juliano Alves

Reputation: 2016

You can use the immutable implementation of HashSet which operations of adding and removing take effectively constant time.

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. Maybe you'll need to change the way you're working the collections, but this way you won't need to worry about concurrency.

Upvotes: 1

Andriy Plokhotnyuk
Andriy Plokhotnyuk

Reputation: 7989

You can use java.util.concurrent.ConcurrentHashMap - it has best performance with guarantied thread safety.

Upvotes: 2

Related Questions