Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 546 Vote(s) - 3.47 Average
  • 1
  • 2
  • 3
  • 4
  • 5
asReversed() vs reversed() in Kotlin?

#1
I noticed that Kotlin has two built in methods [reversed()](

[To see links please register here]

) and [asReversed()](

[To see links please register here]

)

Is there any difference between these two? or are they essentially just doing the exact same thing?
Reply

#2
As per the document

**1. reversed()**

It only returns a list **(List<>)** with elements in reversed order.

There are **multiple definitions of this extension** on different Objects like Array, List, etc.

Example of extension on

**Array<>**

/**
* Returns a list with elements in reversed order.
*/
public fun <T> Array<out T>.reversed(): List<T> {
if (isEmpty()) return emptyList()
val list = toMutableList()
list.reverse()
return list
}


**List<>**

/**
* Returns a list with elements in reversed order.
*/
public fun <T> Iterable<T>.reversed(): List<T> {
if (this is Collection && size <= 1) return toList()
val list = toMutableList()
list.reverse()
return list
}

**2. asReversed()**

It is only applicable to **List<>** and returns a reversed read-only view of the original List. All changes made in the original list will be reflected in the reversed one.
Reply

#3
In Kotlin, both reversed and asReversed have their own unique functions.

The **Reverse** function returns a list with elements in reversed: order.

[Reversed Function](

[To see links please register here]

)

Whereas, the **asReversed** function returns a reversed read-only view of the original List i.e., all changes made in the original list will be reflected in the reversed one.

[asReversed Function](

[To see links please register here]

)

**The difference between the two are that once the asReversed() function has been used, any changes in the original list will be reflected in the reversed list as well.
But the same doesn't hold valid or true when the reversed() function is being used. It's merely used to reverse a list.**


Example:

```
val list = mutableListOf(0, 1, 2, 3, 4, 5)

val asReversed = list.asReversed()
val reversed = list.reversed()

println("Original list: $list")
println("asReversed: $asReversed")
println("reversed: $reversed")

list[0] = 10

println("Original list: $list")
println("asReversed: $asReversed")
println("reversed: $reversed")
```

Outputs

```
Original list: [0, 1, 2, 3, 4, 5]
asReversed: [5, 4, 3, 2, 1, 0]
reversed: [5, 4, 3, 2, 1, 0]
Original list: [10, 1, 2, 3, 4, 5]
asReversed: [5, 4, 3, 2, 1, 10]
reversed: [5, 4, 3, 2, 1, 0]
```

[Try it online!](

[To see links please register here]

"Kotlin – Try It Online")
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through