Re: Can arrays be parameters to generics
Tom Anderson schrieb:
... And how exactly do you search or insert into this array?
Searching with binary search ... as implemented in Arrays class..
inserting creating a new array using data of the binarys search
shifting all entrys afterwards..
Yes it is slower. Bit if the arrays/sets come en masse and are rather
small , may be less than 500 entrys one won't notice it that much.
You justify your arguments by using small lists (array or otherwise), where
the difference would be insignificant. Others have already pointed out that
the memory overhead of an ArrayList over that of an array is quite small, more
than enough to justify the additional power.
This example I made is really only important to show that even if
Collections are superior usually. My experience till now was that the
reason to choose arrays over Collection (no matter if set or list or
map) were always due to storage. There has never ever been a problem
with speed for me due to collections.
And the storage difference is insignificant. Meanwhile you are sacrificing
the algorithmic and expressive power of collections. Also, it is pointless to
compare Set or Map to array, as mentioned upthread. They just don't behave
similarly. They impose algorithmic differences. You plug in an array for a
Set and you have to do all kinds of gyrations to make it "look" like a Set.
those gyrations will sacrifice even more benefit for the unimportant
difference in memory usage.
As others pointed out ArrayList has less overhead than map or set.
Yes thats true .. but sometimes you need a Set or Map and usually I use
HashSet and HashMap for this. But sometimes an array can replace those.
Wacky reasoning. An array is just not the same as a Set or a Map.
Especially if the key for the map is part of the Object mapped it can
easier be put into an array as one doesn't need any special Entry
objects holding key and mapped object.
So instead of nearly instantaneous response to "give me the address that
corresponds to this name", you have to sort, search, re-arrange and otherwise
mess up an array that you're using as a map. You introduce complicated
algorithms with increased risk of bugs, you probably have to maintain
secondary structures to attempt to hack out the performance loss, and when you
realize that the whole thing is not thread-safe you really get buried in code
That is a false economy.
Again this is optimisation that should not be done prematurely.
Especially because it isn't an optimization, it's a de-optimization.
It should never be done.
Though sometimes one knows that there will be more than x times 100k
Collections then you might be sure beforehand that such optimisation is
As long as you know that "sometimes" in this context means "never".