You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by Sébastien Lorber <lo...@gmail.com> on 2011/08/18 14:26:52 UTC

[collections] Iterate over sublists of an original list

Hello,


It's not the first time i have to split a big list of hibernate entities
ID's to sublists of 100 items for exemple so that i could load all these
entities 100 in a single request (with a "where id in (<id sublist>")

Thus I want to iterate easily on sublists of a list, with the possibility to
give the sublist a size...
I though i would find the tool in apache collections but i didn't find it.
Perhaps i've missed the class...



If there's no tool to do that yet, i think it would be great to make one in
apache collections.

The kinda simple implementation i use at work is the following:
http://pastebin.com/CRitkWTG


And you use it like that:

        // We load vehicles 100 by 100
         for ( List<String> idSublist : new
SublistIterable<String>(allIds,100) ) {
            List<Vehicle> vehiclesSublist = vehicleDAO.findByIds(idSublist);
            // blablabla
        }

Re: [collections] Iterate over sublists of an original list

Posted by Sébastien Lorber <lo...@gmail.com>.
Hello,

Sorry but i don't know so much about open source contributions yet...

If i think a feature is missing, should i make a pull request or something
or wait for the approval on this mailing list?


It seems that it would be kinda easy to implement these functions that are
available in scala...

The groupBy Matthew Pocock talk about could also be implemented easily i
think





2011/8/18 Sébastien Lorber <lo...@gmail.com>

> That's true.
>
> The grouped() method in scala do exactly that.
>
> I agree, these functions are quite useful that's why i sent a mail: to see
> if we can integrate them in apache libs
>
>
> 2011/8/18 Matthew Pocock <tu...@gmail.com>
>
>> The scala collections library has the grouped() method. From the scaladoc:
>>
>> defgrouped (size: Int <
>> http://www.scala-lang.org/api/current/scala/Int.html>
>> ): Iterator<
>> http://www.scala-lang.org/api/current/scala/collection/Iterator.html>
>> [List<
>> http://www.scala-lang.org/api/current/scala/collection/immutable/List.html
>> >
>> [A]]
>>
>> Partitions elements in fixed size lists.
>> size
>>
>> the number of elements per group
>> returns
>>
>> An iterator producing lists of size size, except the last will be
>> truncated
>> if the elements don't divide evenly.
>>
>> I'm not suggesting you should jump to scala to use its collections
>> library,
>> or even use scala-library.jar from your Java app, but I do find this
>> method
>> together with groupBy() one of the more useful bits of functionality. Pity
>> something like it isn't supported by other 3rd party collections for Java.
>>
>> Matthew
>>
>> On 18 August 2011 13:48, Sébastien Lorber <lorber.sebastien@gmail.com
>> >wrote:
>>
>> > Hello
>> >
>> > Actually if you look at my implementation, i use that List.sublist()
>> > method.
>> >
>> > It's a little pain to use it to split because you must always take care
>> of
>> > an out of bound...
>> > IMHO, people do not really like to play with array/list indexes... If
>> they
>> > just want to split a big list of a couple of small sublists,
>> > they probably do not like to have to deal with calculating the good
>> index
>> > positions, they just want the list to be splitted :)
>> >
>> > I've looked at some guava classes but wasn't able to find anything to do
>> > this: a convenient way to split a list
>> >
>> >
>> >
>> > 2011/8/18 David Karlsen <da...@gmail.com>
>> >
>> > > Guava also has a lot of handy classes for working on collections.
>> > >
>> > > 2011/8/18 Simone Tripodi <si...@apache.org>
>> > >
>> > > > Salut Sébastien,
>> > > > wouldn't the List#subList(int, int)[1] method be helpful for your
>> > > purposes?
>> > > > HTH,
>> > > > Simo
>> > > >
>> > > > [1]
>> > > >
>> > >
>> >
>> http://download.oracle.com/javase/1.5.0/docs/api/java/util/List.html#subList(int
>> > > > ,
>> > > > int)
>> > > >
>> > > >
>> > > > http://people.apache.org/~simonetripodi/
>> > > > http://www.99soft.org/
>> > > >
>> > > >
>> > > >
>> > > > On Thu, Aug 18, 2011 at 2:26 PM, Sébastien Lorber
>> > > > <lo...@gmail.com> wrote:
>> > > > > Hello,
>> > > > >
>> > > > >
>> > > > > It's not the first time i have to split a big list of hibernate
>> > > entities
>> > > > > ID's to sublists of 100 items for exemple so that i could load all
>> > > these
>> > > > > entities 100 in a single request (with a "where id in (<id
>> sublist>")
>> > > > >
>> > > > > Thus I want to iterate easily on sublists of a list, with the
>> > > possibility
>> > > > to
>> > > > > give the sublist a size...
>> > > > > I though i would find the tool in apache collections but i didn't
>> > find
>> > > > it.
>> > > > > Perhaps i've missed the class...
>> > > > >
>> > > > >
>> > > > >
>> > > > > If there's no tool to do that yet, i think it would be great to
>> make
>> > > one
>> > > > in
>> > > > > apache collections.
>> > > > >
>> > > > > The kinda simple implementation i use at work is the following:
>> > > > > http://pastebin.com/CRitkWTG
>> > > > >
>> > > > >
>> > > > > And you use it like that:
>> > > > >
>> > > > >        // We load vehicles 100 by 100
>> > > > >         for ( List<String> idSublist : new
>> > > > > SublistIterable<String>(allIds,100) ) {
>> > > > >            List<Vehicle> vehiclesSublist =
>> > > > vehicleDAO.findByIds(idSublist);
>> > > > >            // blablabla
>> > > > >        }
>> > > > >
>> > > >
>> > > >
>> ---------------------------------------------------------------------
>> > > > To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
>> > > > For additional commands, e-mail: dev-help@commons.apache.org
>> > > >
>> > > >
>> > >
>> > >
>> > > --
>> > > --
>> > > David J. M. Karlsen - http://www.linkedin.com/in/davidkarlsen
>> > >
>> >
>>
>>
>>
>> --
>> Dr Matthew Pocock
>> Visitor, School of Computing Science, Newcastle University
>> mailto: turingatemyhamster@gmail.com
>> gchat: turingatemyhamster@gmail.com
>> msn: matthew_pocock@yahoo.co.uk
>> irc.freenode.net: drdozer
>> tel: (0191) 2566550
>> mob: +447535664143
>>
>
>

Re: [collections] Iterate over sublists of an original list

Posted by Sébastien Lorber <lo...@gmail.com>.
That's true.

The grouped() method in scala do exactly that.

I agree, these functions are quite useful that's why i sent a mail: to see
if we can integrate them in apache libs


2011/8/18 Matthew Pocock <tu...@gmail.com>

> The scala collections library has the grouped() method. From the scaladoc:
>
> defgrouped (size: Int <
> http://www.scala-lang.org/api/current/scala/Int.html>
> ): Iterator<
> http://www.scala-lang.org/api/current/scala/collection/Iterator.html>
> [List<
> http://www.scala-lang.org/api/current/scala/collection/immutable/List.html
> >
> [A]]
>
> Partitions elements in fixed size lists.
> size
>
> the number of elements per group
> returns
>
> An iterator producing lists of size size, except the last will be truncated
> if the elements don't divide evenly.
>
> I'm not suggesting you should jump to scala to use its collections library,
> or even use scala-library.jar from your Java app, but I do find this method
> together with groupBy() one of the more useful bits of functionality. Pity
> something like it isn't supported by other 3rd party collections for Java.
>
> Matthew
>
> On 18 August 2011 13:48, Sébastien Lorber <lorber.sebastien@gmail.com
> >wrote:
>
> > Hello
> >
> > Actually if you look at my implementation, i use that List.sublist()
> > method.
> >
> > It's a little pain to use it to split because you must always take care
> of
> > an out of bound...
> > IMHO, people do not really like to play with array/list indexes... If
> they
> > just want to split a big list of a couple of small sublists,
> > they probably do not like to have to deal with calculating the good index
> > positions, they just want the list to be splitted :)
> >
> > I've looked at some guava classes but wasn't able to find anything to do
> > this: a convenient way to split a list
> >
> >
> >
> > 2011/8/18 David Karlsen <da...@gmail.com>
> >
> > > Guava also has a lot of handy classes for working on collections.
> > >
> > > 2011/8/18 Simone Tripodi <si...@apache.org>
> > >
> > > > Salut Sébastien,
> > > > wouldn't the List#subList(int, int)[1] method be helpful for your
> > > purposes?
> > > > HTH,
> > > > Simo
> > > >
> > > > [1]
> > > >
> > >
> >
> http://download.oracle.com/javase/1.5.0/docs/api/java/util/List.html#subList(int
> > > > ,
> > > > int)
> > > >
> > > >
> > > > http://people.apache.org/~simonetripodi/
> > > > http://www.99soft.org/
> > > >
> > > >
> > > >
> > > > On Thu, Aug 18, 2011 at 2:26 PM, Sébastien Lorber
> > > > <lo...@gmail.com> wrote:
> > > > > Hello,
> > > > >
> > > > >
> > > > > It's not the first time i have to split a big list of hibernate
> > > entities
> > > > > ID's to sublists of 100 items for exemple so that i could load all
> > > these
> > > > > entities 100 in a single request (with a "where id in (<id
> sublist>")
> > > > >
> > > > > Thus I want to iterate easily on sublists of a list, with the
> > > possibility
> > > > to
> > > > > give the sublist a size...
> > > > > I though i would find the tool in apache collections but i didn't
> > find
> > > > it.
> > > > > Perhaps i've missed the class...
> > > > >
> > > > >
> > > > >
> > > > > If there's no tool to do that yet, i think it would be great to
> make
> > > one
> > > > in
> > > > > apache collections.
> > > > >
> > > > > The kinda simple implementation i use at work is the following:
> > > > > http://pastebin.com/CRitkWTG
> > > > >
> > > > >
> > > > > And you use it like that:
> > > > >
> > > > >        // We load vehicles 100 by 100
> > > > >         for ( List<String> idSublist : new
> > > > > SublistIterable<String>(allIds,100) ) {
> > > > >            List<Vehicle> vehiclesSublist =
> > > > vehicleDAO.findByIds(idSublist);
> > > > >            // blablabla
> > > > >        }
> > > > >
> > > >
> > > > ---------------------------------------------------------------------
> > > > To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
> > > > For additional commands, e-mail: dev-help@commons.apache.org
> > > >
> > > >
> > >
> > >
> > > --
> > > --
> > > David J. M. Karlsen - http://www.linkedin.com/in/davidkarlsen
> > >
> >
>
>
>
> --
> Dr Matthew Pocock
> Visitor, School of Computing Science, Newcastle University
> mailto: turingatemyhamster@gmail.com
> gchat: turingatemyhamster@gmail.com
> msn: matthew_pocock@yahoo.co.uk
> irc.freenode.net: drdozer
> tel: (0191) 2566550
> mob: +447535664143
>

Re: [collections] Iterate over sublists of an original list

Posted by Matthew Pocock <tu...@gmail.com>.
The scala collections library has the grouped() method. From the scaladoc:

defgrouped (size: Int <http://www.scala-lang.org/api/current/scala/Int.html>
): Iterator<http://www.scala-lang.org/api/current/scala/collection/Iterator.html>
[List<http://www.scala-lang.org/api/current/scala/collection/immutable/List.html>
[A]]

Partitions elements in fixed size lists.
size

the number of elements per group
returns

An iterator producing lists of size size, except the last will be truncated
if the elements don't divide evenly.

I'm not suggesting you should jump to scala to use its collections library,
or even use scala-library.jar from your Java app, but I do find this method
together with groupBy() one of the more useful bits of functionality. Pity
something like it isn't supported by other 3rd party collections for Java.

Matthew

On 18 August 2011 13:48, Sébastien Lorber <lo...@gmail.com>wrote:

> Hello
>
> Actually if you look at my implementation, i use that List.sublist()
> method.
>
> It's a little pain to use it to split because you must always take care of
> an out of bound...
> IMHO, people do not really like to play with array/list indexes... If they
> just want to split a big list of a couple of small sublists,
> they probably do not like to have to deal with calculating the good index
> positions, they just want the list to be splitted :)
>
> I've looked at some guava classes but wasn't able to find anything to do
> this: a convenient way to split a list
>
>
>
> 2011/8/18 David Karlsen <da...@gmail.com>
>
> > Guava also has a lot of handy classes for working on collections.
> >
> > 2011/8/18 Simone Tripodi <si...@apache.org>
> >
> > > Salut Sébastien,
> > > wouldn't the List#subList(int, int)[1] method be helpful for your
> > purposes?
> > > HTH,
> > > Simo
> > >
> > > [1]
> > >
> >
> http://download.oracle.com/javase/1.5.0/docs/api/java/util/List.html#subList(int
> > > ,
> > > int)
> > >
> > >
> > > http://people.apache.org/~simonetripodi/
> > > http://www.99soft.org/
> > >
> > >
> > >
> > > On Thu, Aug 18, 2011 at 2:26 PM, Sébastien Lorber
> > > <lo...@gmail.com> wrote:
> > > > Hello,
> > > >
> > > >
> > > > It's not the first time i have to split a big list of hibernate
> > entities
> > > > ID's to sublists of 100 items for exemple so that i could load all
> > these
> > > > entities 100 in a single request (with a "where id in (<id sublist>")
> > > >
> > > > Thus I want to iterate easily on sublists of a list, with the
> > possibility
> > > to
> > > > give the sublist a size...
> > > > I though i would find the tool in apache collections but i didn't
> find
> > > it.
> > > > Perhaps i've missed the class...
> > > >
> > > >
> > > >
> > > > If there's no tool to do that yet, i think it would be great to make
> > one
> > > in
> > > > apache collections.
> > > >
> > > > The kinda simple implementation i use at work is the following:
> > > > http://pastebin.com/CRitkWTG
> > > >
> > > >
> > > > And you use it like that:
> > > >
> > > >        // We load vehicles 100 by 100
> > > >         for ( List<String> idSublist : new
> > > > SublistIterable<String>(allIds,100) ) {
> > > >            List<Vehicle> vehiclesSublist =
> > > vehicleDAO.findByIds(idSublist);
> > > >            // blablabla
> > > >        }
> > > >
> > >
> > > ---------------------------------------------------------------------
> > > To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
> > > For additional commands, e-mail: dev-help@commons.apache.org
> > >
> > >
> >
> >
> > --
> > --
> > David J. M. Karlsen - http://www.linkedin.com/in/davidkarlsen
> >
>



-- 
Dr Matthew Pocock
Visitor, School of Computing Science, Newcastle University
mailto: turingatemyhamster@gmail.com
gchat: turingatemyhamster@gmail.com
msn: matthew_pocock@yahoo.co.uk
irc.freenode.net: drdozer
tel: (0191) 2566550
mob: +447535664143

Re: [collections] Iterate over sublists of an original list

Posted by Sébastien Lorber <lo...@gmail.com>.
Hello

Actually if you look at my implementation, i use that List.sublist() method.

It's a little pain to use it to split because you must always take care of
an out of bound...
IMHO, people do not really like to play with array/list indexes... If they
just want to split a big list of a couple of small sublists,
they probably do not like to have to deal with calculating the good index
positions, they just want the list to be splitted :)

I've looked at some guava classes but wasn't able to find anything to do
this: a convenient way to split a list



2011/8/18 David Karlsen <da...@gmail.com>

> Guava also has a lot of handy classes for working on collections.
>
> 2011/8/18 Simone Tripodi <si...@apache.org>
>
> > Salut Sébastien,
> > wouldn't the List#subList(int, int)[1] method be helpful for your
> purposes?
> > HTH,
> > Simo
> >
> > [1]
> >
> http://download.oracle.com/javase/1.5.0/docs/api/java/util/List.html#subList(int
> > ,
> > int)
> >
> >
> > http://people.apache.org/~simonetripodi/
> > http://www.99soft.org/
> >
> >
> >
> > On Thu, Aug 18, 2011 at 2:26 PM, Sébastien Lorber
> > <lo...@gmail.com> wrote:
> > > Hello,
> > >
> > >
> > > It's not the first time i have to split a big list of hibernate
> entities
> > > ID's to sublists of 100 items for exemple so that i could load all
> these
> > > entities 100 in a single request (with a "where id in (<id sublist>")
> > >
> > > Thus I want to iterate easily on sublists of a list, with the
> possibility
> > to
> > > give the sublist a size...
> > > I though i would find the tool in apache collections but i didn't find
> > it.
> > > Perhaps i've missed the class...
> > >
> > >
> > >
> > > If there's no tool to do that yet, i think it would be great to make
> one
> > in
> > > apache collections.
> > >
> > > The kinda simple implementation i use at work is the following:
> > > http://pastebin.com/CRitkWTG
> > >
> > >
> > > And you use it like that:
> > >
> > >        // We load vehicles 100 by 100
> > >         for ( List<String> idSublist : new
> > > SublistIterable<String>(allIds,100) ) {
> > >            List<Vehicle> vehiclesSublist =
> > vehicleDAO.findByIds(idSublist);
> > >            // blablabla
> > >        }
> > >
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
> > For additional commands, e-mail: dev-help@commons.apache.org
> >
> >
>
>
> --
> --
> David J. M. Karlsen - http://www.linkedin.com/in/davidkarlsen
>

Re: [collections] Iterate over sublists of an original list

Posted by David Karlsen <da...@gmail.com>.
Guava also has a lot of handy classes for working on collections.

2011/8/18 Simone Tripodi <si...@apache.org>

> Salut Sébastien,
> wouldn't the List#subList(int, int)[1] method be helpful for your purposes?
> HTH,
> Simo
>
> [1]
> http://download.oracle.com/javase/1.5.0/docs/api/java/util/List.html#subList(int
> ,
> int)
>
>
> http://people.apache.org/~simonetripodi/
> http://www.99soft.org/
>
>
>
> On Thu, Aug 18, 2011 at 2:26 PM, Sébastien Lorber
> <lo...@gmail.com> wrote:
> > Hello,
> >
> >
> > It's not the first time i have to split a big list of hibernate entities
> > ID's to sublists of 100 items for exemple so that i could load all these
> > entities 100 in a single request (with a "where id in (<id sublist>")
> >
> > Thus I want to iterate easily on sublists of a list, with the possibility
> to
> > give the sublist a size...
> > I though i would find the tool in apache collections but i didn't find
> it.
> > Perhaps i've missed the class...
> >
> >
> >
> > If there's no tool to do that yet, i think it would be great to make one
> in
> > apache collections.
> >
> > The kinda simple implementation i use at work is the following:
> > http://pastebin.com/CRitkWTG
> >
> >
> > And you use it like that:
> >
> >        // We load vehicles 100 by 100
> >         for ( List<String> idSublist : new
> > SublistIterable<String>(allIds,100) ) {
> >            List<Vehicle> vehiclesSublist =
> vehicleDAO.findByIds(idSublist);
> >            // blablabla
> >        }
> >
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
> For additional commands, e-mail: dev-help@commons.apache.org
>
>


-- 
--
David J. M. Karlsen - http://www.linkedin.com/in/davidkarlsen

Re: [collections] Iterate over sublists of an original list

Posted by Simone Tripodi <si...@apache.org>.
Salut Sébastien,
wouldn't the List#subList(int, int)[1] method be helpful for your purposes?
HTH,
Simo

[1] http://download.oracle.com/javase/1.5.0/docs/api/java/util/List.html#subList(int,
int)


http://people.apache.org/~simonetripodi/
http://www.99soft.org/



On Thu, Aug 18, 2011 at 2:26 PM, Sébastien Lorber
<lo...@gmail.com> wrote:
> Hello,
>
>
> It's not the first time i have to split a big list of hibernate entities
> ID's to sublists of 100 items for exemple so that i could load all these
> entities 100 in a single request (with a "where id in (<id sublist>")
>
> Thus I want to iterate easily on sublists of a list, with the possibility to
> give the sublist a size...
> I though i would find the tool in apache collections but i didn't find it.
> Perhaps i've missed the class...
>
>
>
> If there's no tool to do that yet, i think it would be great to make one in
> apache collections.
>
> The kinda simple implementation i use at work is the following:
> http://pastebin.com/CRitkWTG
>
>
> And you use it like that:
>
>        // We load vehicles 100 by 100
>         for ( List<String> idSublist : new
> SublistIterable<String>(allIds,100) ) {
>            List<Vehicle> vehiclesSublist = vehicleDAO.findByIds(idSublist);
>            // blablabla
>        }
>

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
For additional commands, e-mail: dev-help@commons.apache.org