컬렉션 처리 단계 수를 제한하라
컬렉션의 비용
모든 컬렉션 처리 메서드는 비용이 많이 듭니다.
내부적으로 요소들을 활용해 반복을 돌며, 추가적인 컬렉션을 만들어 사용하기도 합니다.
시퀀스도 시퀀스 전체를 랩 하는 객체가 만들어지며, 조작을 위해 또 다른 추가적인 객체를 만들어냅니다.
컬렉션의 처리의 단계 수
컬렉션과 시퀀스의 처리 수가 많다면 꽤 큰 비용이 들어갑니다.
어떤 메서드를 사용하는지에 따라서 컬렉션 처리의 단계 수가 달라집니다.
따라서 적절한 메서드를 활용해서 컬렉션 처리 단계 수를 적절하게 제한하는 것이 좋습니다.
.filter { it != null } .map { it }
.filterNotNull()
.map { Transformation } .filterNotNull()
.mapNotNull { Transformation }
.map { Transformation } .joinToString()
.joinToString { Transformation }
.filter { Predicate1 } .filter{ Predicate2 }
.filter { Predicate1 && Predicate2 }
.filter { it is Type } .map { it as Type }
.filterIsInstance()
.sortedBy { Key2 } .sortedBy { Key1 }
.sortedWith( compareBy({ Key1 }, { Key2 }) )
.listOf(…) .filterNotNull()
.listOfNotNull(…)
.withIndex() .filter { (index, element) -> Predicate using index } .map { it.value }
.filterIndexed { index, element -> Predicate using index }
Last updated
Was this helpful?