Open
Conversation
- Implimentation of the NaturalList::subList(), containsAll() - Implimentation of the Map::swap() - Implimentation of the Person::sortByAge(), sortByName() Ps - Implimentation of the NaturalList::equals(),hashCode() without contracts
motorro
reviewed
Sep 10, 2024
| override fun equals(other: Any?): Boolean = false | ||
| override fun equals(other: Any?): Boolean { | ||
| var i : Int = 1; | ||
| return List<Int>(size, { i++ } ).equals(other) |
There was a problem hiding this comment.
@gaffarovAV , КМК, создание нового списка при вызове equals не очень оптимально.
- Алоцируется память под новый список, который заполнен последовательностью чисел
- Первый цикл - заполняем список для сравнения
- Второй цикл - сравнение нового цикла с other.
Я бы сделал вариант попроще: - Проверил бы, что
otherне тот же самый объект (===), если равно - ранний выход - Проверил бы, что
other- это список. Если нет - ранний выход - Проверил бы равенство длины.
- В цикле перебрал бы все элементы. Если какой-то из них не равен - ранний выход
Так мы не создаем лишних коллекций в памяти и оптимизируем скорость, обходясь только одним проходом
| override fun hashCode(): Int = -1 | ||
| override fun hashCode(): Int { | ||
| var i : Int = 1; | ||
| return List<Int>(size, { i++ } ).hashCode() |
There was a problem hiding this comment.
@gaffarovAV, тут, примерно, та же история, что и с предыдущим комментарием. Посмотрите реализацию в каком-нибудь библиотечном списке. Там будет что-нибудь такое:
override fun hashCode(): Int {
var hashCode = 1
for (e in a) hashCode = 31 * hashCode + e
return hashCode
}КМК, не очень здорово создавать список только для того, чтобы потом посчитать его хешкод
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Ps