Skip to content

Yadro_gaffarov_av_HomeWork_02#6

Open
gaffarovAV wants to merge 1 commit intoKotlin-Yadro:masterfrom
gaffarovAV:master
Open

Yadro_gaffarov_av_HomeWork_02#6
gaffarovAV wants to merge 1 commit intoKotlin-Yadro:masterfrom
gaffarovAV:master

Conversation

@gaffarovAV
Copy link

  • 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

 -  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
override fun equals(other: Any?): Boolean = false
override fun equals(other: Any?): Boolean {
var i : Int = 1;
return List<Int>(size, { i++ } ).equals(other)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@gaffarovAV , КМК, создание нового списка при вызове equals не очень оптимально.

  1. Алоцируется память под новый список, который заполнен последовательностью чисел
  2. Первый цикл - заполняем список для сравнения
  3. Второй цикл - сравнение нового цикла с other.
    Я бы сделал вариант попроще:
  4. Проверил бы, что other не тот же самый объект (===), если равно - ранний выход
  5. Проверил бы, что other - это список. Если нет - ранний выход
  6. Проверил бы равенство длины.
  7. В цикле перебрал бы все элементы. Если какой-то из них не равен - ранний выход
    Так мы не создаем лишних коллекций в памяти и оптимизируем скорость, обходясь только одним проходом

override fun hashCode(): Int = -1
override fun hashCode(): Int {
var i : Int = 1;
return List<Int>(size, { i++ } ).hashCode()
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@gaffarovAV, тут, примерно, та же история, что и с предыдущим комментарием. Посмотрите реализацию в каком-нибудь библиотечном списке. Там будет что-нибудь такое:

override fun hashCode(): Int {
        var hashCode = 1 
        for (e in a) hashCode = 31 * hashCode + e
        return hashCode
    }

КМК, не очень здорово создавать список только для того, чтобы потом посчитать его хешкод

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants