comparable - Scala - TreeSet of tuples -


i trying instantiate treeset in scala, passing specific comparator on tuples this:

  var heads: java.util.treeset[(t, int)] = new java.util.treeset[(t, int)](new comparator[(t,int)] {     def compare(o1: (t, int), o2: (t, int)): int = ordering[(t, int)].compare(o1, o2)   }) 

however, implicit ordering on t cannot found. should specify t <: comparable[t] in type hierarchy or there simple wayto achieve tuple comparison?

since comparing tuples, need specify element use comparisons, e.g. if want order first element of type t:

object main extends app {   import java.util.comparator   def heads[t: ordering] = new java.util.treeset[(t, int)](new comparator[(t,int)] {     def compare(o1: (t, int), o2: (t, int)): int = ordering.by[(t, int), t](_._1).compare(o1, o2)   })   val test = heads[string]   test.add(("foo", 42))   test.add(("foo", 42))   test.add(("bar", 17))   println(test) } 

this output [(bar,17), (foo,42)].


Comments

Popular posts from this blog

jquery - How can I dynamically add a browser tab? -

keyboard - C++ GetAsyncKeyState alternative -

android - java.net.UnknownHostException(Unable to resolve host “URL”: No address associated with hostname) -