0

I have a requirement ,where i have multiple tuples coming in.

For eg: (a1, b1)
        (a2, b2)
        (a3, b3)

I need to store these values in datastructure and create a collection of these. Next I will get another tuple as input (an, bn) and i need to check if it is present in the collection.

I used a class with two variables to store it and create collection of object and then use as follows

collection.exists(object => object.a == an && object.b == bn)

Can i use any other better way to achieve this or any other datastructure to use rather than creating class for variable a and b

Alok
  • 1,374
  • 3
  • 18
  • 44
  • Do you need to store the tuples as class in your collection? Can you not store them as they are (as tuples) ? – ccheneson Feb 18 '16 at 10:00
  • Yes , I can store them as tuples . But I think both will have same performance , right? Should i use Set or Map somehow to have better performance – Alok Feb 18 '16 at 10:03

1 Answers1

0

If you can store them as tuple, you can check if the collection contains (an, bn). contains is defined in the trait SeqLike.

You don't need a key-value collection, so a Set is enough

The following should work

val collections = Set(("a1","b1"),("a2","b2"),("a3","b3"))
val test = ("a2","b2")
val test2 = ("a4","b4")

println(collection.contains( test))
println(collection.contains( test2))
ccheneson
  • 49,072
  • 8
  • 63
  • 68