On Sat, Nov 24, 2018 at 7:37 AM Oleg Kalnichevski <[email protected]> wrote:

> On Sat, 2018-11-24 at 15:04 +0100, Michael Osipov wrote:
> > Am 2018-11-23 um 09:20 schrieb Oleg Kalnichevski:> On Thu, 2018-11-22
> > at
> > 20:12 -0700, Gary Gregory wrote:
> >  >> Hi All:
> >  >>
> >  >> In the HC code base, we could avoid some boilerplate clutter by
> > using
> >  >> Java's enhanced for-loop construct. I believe this would make the
> >  >> code
> >  >> easier to read. Are there any objections to making this change?
> >  >>
> >  >> Gary
> >  >
> >  > I am _strongly_ against it. Last time you did that it had caused
> > us a
> >  > lot of grief.
> >  >
> >  > https://issues.apache.org/jira/browse/HTTPCORE-361
> > That's an interesting case because it is an antipattern to access
> > collections via index. The recommended way is to use iterators. Why
> > have
> > create the iterator directly and call next() once? Still to much
> > overhead?
> >
> > Michael
>
> This is what one unnecessary object allocation in a critical execution
> path can do to you.
>
> Strongly disagree about collection element access by index being an
> anti-pattern. Who invents this kind of stuff?
>

When you access a linked list by index (LinkedList is one example) without
random access, access for a single element is O(N), which is terrible when
you iterate through the whole array, O(N^2).

If your linked list provides random access (ArrayList in one example),
access for a single element is O(1), which is great when you iterate
through the whole array, O(N).

So, before you write a loop on any Collection that loops through all
elements, you better know which kind of collection you are dealing with...

Gary
Gary


>
> Oleg
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [email protected]
> For additional commands, e-mail: [email protected]
>
>

Reply via email to