Different behavior when declaration type is different(Set vs TreeSet)

前端 未结 4 1040
囚心锁ツ
囚心锁ツ 2021-01-21 05:15
    var set = TreeSet(5,4,3,2,1)
    println(set)

    val diffSet: TreeSet[Int] = set
    // if I change above code to val diffSet: Set[Int] = set
    // the result is          


        
4条回答
  •  别那么骄傲
    2021-01-21 05:30

    You can do:

    scala> for (i <-genSet.view; x = i + 1) println(x)
    2
    3
    4
    5
    6
    

    Although, it's the type of trick that when you look at it after a few months, you may wonder why you added .view ...

提交回复
热议问题