What are views for collections and when would you want to use them?

前端 未结 4 1669
死守一世寂寞
死守一世寂寞 2020-12-07 22:09

In Scala, for many (all?) types of collections you can create views.

What exactly is a view and for which purposes are views useful?

4条回答
  •  臣服心动
    2020-12-07 22:54

    view is used for lazy computation,but not for saving memory.

    When you create a view against a collection, the memory has already been allocated forthe collection.

    When creating the view with val view = Range(1,9).view., the collection has already been allocated the memory, if it is too large,say,Range(1,1000000000), OOM can't be avoid

提交回复
热议问题