Effective Java


// BROKEN difference-based comparator - violates transitivity!



Download 2,19 Mb.
Pdf ko'rish
bet72/341
Sana11.07.2022
Hajmi2,19 Mb.
#776765
1   ...   68   69   70   71   72   73   74   75   ...   341
Bog'liq
Effective Java

// BROKEN difference-based comparator - violates transitivity!
static Comparator hashCodeOrder = new Comparator<>() {
public int compare(Object o1, Object o2) {
return o1.hashCode() - o2.hashCode();
}
};
Do not use this technique. It is fraught with danger from integer overflow and
IEEE 754 floating point arithmetic artifacts [JLS 15.20.1, 15.21.1]. Furthermore,
the resulting methods are unlikely to be significantly faster than those written
using the techniques described in this item. Use either a static 
compare
method:
// Comparator based on static compare method
static Comparator hashCodeOrder = new Comparator<>() {
public int compare(Object o1, Object o2) {
return Integer.compare(o1.hashCode(), o2.hashCode());
}
};
or a comparator construction method:
// Comparator based on Comparator construction method
static Comparator hashCodeOrder =
Comparator.comparingInt(o -> o.hashCode());


CHAPTER 3
METHODS COMMON TO ALL OBJECTS
72
In summary, whenever you implement a value class that has a sensible order-
ing, you should have the class implement the 
Comparable
interface so that its
instances can be easily sorted, searched, and used in comparison-based collec-
tions. When comparing field values in the implementations of the 
compareTo
methods, avoid the use of the 
<
and 
>
operators. Instead, use the static 
compare
methods in the boxed primitive classes or the comparator construction methods in
the 
Comparator
interface.


73
C H A P T E R
4
Classes and Interfaces
C
LASSES
and interfaces lie at the heart of the Java programming language.
They are its basic units of abstraction. The language provides many powerful
elements that you can use to design classes and interfaces. This chapter contains
guidelines to help you make the best use of these elements so that your classes and
interfaces are usable, robust, and flexible.

Download 2,19 Mb.

Do'stlaringiz bilan baham:
1   ...   68   69   70   71   72   73   74   75   ...   341




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©hozir.org 2024
ma'muriyatiga murojaat qiling

kiriting | ro'yxatdan o'tish
    Bosh sahifa
юртда тантана
Боғда битган
Бугун юртда
Эшитганлар жилманглар
Эшитмадим деманглар
битган бодомлар
Yangiariq tumani
qitish marakazi
Raqamli texnologiyalar
ilishida muhokamadan
tasdiqqa tavsiya
tavsiya etilgan
iqtisodiyot kafedrasi
steiermarkischen landesregierung
asarlaringizni yuboring
o'zingizning asarlaringizni
Iltimos faqat
faqat o'zingizning
steierm rkischen
landesregierung fachabteilung
rkischen landesregierung
hamshira loyihasi
loyihasi mavsum
faolyatining oqibatlari
asosiy adabiyotlar
fakulteti ahborot
ahborot havfsizligi
havfsizligi kafedrasi
fanidan bo’yicha
fakulteti iqtisodiyot
boshqaruv fakulteti
chiqarishda boshqaruv
ishlab chiqarishda
iqtisodiyot fakultet
multiservis tarmoqlari
fanidan asosiy
Uzbek fanidan
mavzulari potok
asosidagi multiservis
'aliyyil a'ziym
billahil 'aliyyil
illaa billahil
quvvata illaa
falah' deganida
Kompyuter savodxonligi
bo’yicha mustaqil
'alal falah'
Hayya 'alal
'alas soloh
Hayya 'alas
mavsum boyicha


yuklab olish