@Ezekiel4
Охотник на пиратов и сборщик монолитов

Что значит число 31 в определении функции hashCode?

Часто встречаю его в коде.

Пример 1 : PersonInfo из fastjson

ссылка: https://github.com/alibaba/fastjson/blob/14a95cc5b...
public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((department == null) ? 0 : department.hashCode());
        result = prime * result + ((fax == null) ? 0 : fax.hashCode());
        result = prime * result + (female ? 1231 : 1237);
        result = prime * result + ((fullAddress == null) ? 0 : fullAddress.hashCode());
        result = prime * result + ((homepageUrl == null) ? 0 : homepageUrl.hashCode());
        result = prime * result + ((jobTitle == null) ? 0 : jobTitle.hashCode());
        result = prime * result + (male ? 1231 : 1237);
        result = prime * result + ((mobileNo == null) ? 0 : mobileNo.hashCode());
        result = prime * result + ((name == null) ? 0 : name.hashCode());
        result = prime * result + ((phones == null) ? 0 : phones.hashCode());
        return result;
    }

Пример 1 : RecordsTest из moshi

Ссылка: https://github.com/square/moshi/blob/5cf5f9664cd34...
public int hashCode() {
      int result =
          Objects.hash(
              firstName,
              lastName,
              age,
              nationalities,
              weight,
              tattoos,
              hasChildren,
              superWildcard,
              extendsWildcard,
              unboundedWildcard,
              objectList,
              foodPreferences,
              setListMapArrayInt);
      result = 31 * result + Arrays.hashCode(favoriteThreeNumbers);
      result = 31 * result + Arrays.hashCode(favoriteArrayValues);
      result = 31 * result + Arrays.hashCode(nestedArray);
      return result;
    }
  }


Иногда вместо 31 ставят другие числа. Например, в библиотеке libgdx (https://github.com/libgdx/libgdx) почти всегда разные цифры ставят
Примеры: https://github.com/libgdx/libgdx/search?p=1&q=publ...
  • Вопрос задан
  • 313 просмотров
Пригласить эксперта
Ответы на вопрос 1
ambisinistrone
@ambisinistrone
sic transit Gloria mundi
61e13cff49815610728548.png
Ответ написан
Комментировать
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы
Bell Integrator Ульяновск
До 400 000 ₽
Bell Integrator Хабаровск
До 400 000 ₽
Bell Integrator Ижевск
До 400 000 ₽
25 апр. 2024, в 12:23
2500 руб./за проект
25 апр. 2024, в 12:21
10000 руб./за проект