Effective Java



Download 2,19 Mb.
Pdf ko'rish
bet199/341
Sana11.07.2022
Hajmi2,19 Mb.
#776765
1   ...   195   196   197   198   199   200   201   202   ...   341
Bog'liq
Effective Java



CHAPTER 7
LAMBDAS AND STREAMS
218
The 
Collection
interface is a subtype of 
Iterable
and has a 
stream
method,
so it provides for both iteration and stream access. Therefore, 
Collection
 or an
appropriate subtype is generally the best return type for a public, sequence-
returning method.
Arrays also provide for easy iteration and stream access with
the 
Arrays.asList
and 
Stream.of
methods. If the sequence you’re returning is
small enough to fit easily in memory, you’re probably best off returning one of the
standard collection implementations, such as 
ArrayList
or 
HashSet
. But
 do not
store a large sequence in memory just to return it as a collection.
If the sequence you’re returning is large but can be represented concisely, con-
sider implementing a special-purpose collection. For example, suppose you want
to return the 
power set
of a given set, which consists of all of its subsets. The
power set of {
a

b

c
} is {{}, {
a
}, {
b
}, {
c
}, {
a

b
}, {
a,
c
}, {
b

c
}, {
a

b

c
}}. If a
set has 
n
elements, its power set has 2
n
. Therefore, you shouldn’t even consider
storing the power set in a standard collection implementation. It is, however, easy
to implement a custom collection for the job with the help of 
AbstractList
.
The trick is to use the index of each element in the power set as a bit vector,
where the 
n
th bit in the index indicates the presence or absence of the 
n
th element
from the source set. In essence, there is a natural mapping between the binary
numbers from 0 to 2
n

1 and the power set of an 
n
-element set. Here’s the code:

Download 2,19 Mb.

Do'stlaringiz bilan baham:
1   ...   195   196   197   198   199   200   201   202   ...   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