Return-Path: X-Original-To: apmail-commons-dev-archive@www.apache.org Delivered-To: apmail-commons-dev-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id C136D8E6D for ; Thu, 18 Aug 2011 14:02:18 +0000 (UTC) Received: (qmail 14073 invoked by uid 500); 18 Aug 2011 14:02:17 -0000 Delivered-To: apmail-commons-dev-archive@commons.apache.org Received: (qmail 13921 invoked by uid 500); 18 Aug 2011 14:02:17 -0000 Mailing-List: contact dev-help@commons.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: "Commons Developers List" Delivered-To: mailing list dev@commons.apache.org Received: (qmail 13905 invoked by uid 99); 18 Aug 2011 14:02:16 -0000 Received: from nike.apache.org (HELO nike.apache.org) (192.87.106.230) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 18 Aug 2011 14:02:16 +0000 X-ASF-Spam-Status: No, hits=1.5 required=5.0 tests=FREEMAIL_FROM,HTML_MESSAGE,RCVD_IN_DNSWL_LOW,SPF_PASS X-Spam-Check-By: apache.org Received-SPF: pass (nike.apache.org: domain of lorber.sebastien@gmail.com designates 209.85.215.43 as permitted sender) Received: from [209.85.215.43] (HELO mail-ew0-f43.google.com) (209.85.215.43) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 18 Aug 2011 14:02:11 +0000 Received: by ewy20 with SMTP id 20so1154686ewy.30 for ; Thu, 18 Aug 2011 07:01:50 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :content-type; bh=hkJnI1QzybF5Lqav1A+oCxNTN6OQFKGU8uYe3Qk8W6Q=; b=UKsv8xmU7xUEDVduUjAmSbHU9O7LvccG6fO3yYMewUHY1SO/xc7Zo6fQfnzDXKcGBb 5RUt/FHLigKJAMIirn5yLmtGQFUc69HrdFuF0uIwRrMfNZrr/swVCXHv4Cw2xsI6SlMM IuXdMSAPtFSSCoadWyj/FpYemi+BjY820tXVE= Received: by 10.213.113.10 with SMTP id y10mr207138ebp.12.1313676110286; Thu, 18 Aug 2011 07:01:50 -0700 (PDT) MIME-Version: 1.0 Received: by 10.213.4.131 with HTTP; Thu, 18 Aug 2011 07:01:30 -0700 (PDT) In-Reply-To: References: From: =?ISO-8859-1?Q?S=E9bastien_Lorber?= Date: Thu, 18 Aug 2011 16:01:30 +0200 Message-ID: Subject: Re: [collections] Iterate over sublists of an original list To: Commons Developers List Content-Type: multipart/alternative; boundary=0015174c0ed4e7764e04aac80d07 X-Virus-Checked: Checked by ClamAV on apache.org --0015174c0ed4e7764e04aac80d07 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable 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 > 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.htm= l > > > [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 truncat= ed > if the elements don't divide evenly. > > I'm not suggesting you should jump to scala to use its collections librar= y, > or even use scala-library.jar from your Java app, but I do find this meth= od > together with groupBy() one of the more useful bits of functionality. Pit= y > something like it isn't supported by other 3rd party collections for Java= . > > Matthew > > On 18 August 2011 13:48, S=E9bastien Lorber >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 ind= ex > > positions, they just want the list to be splitted :) > > > > I've looked at some guava classes but wasn't able to find anything to d= o > > this: a convenient way to split a list > > > > > > > > 2011/8/18 David Karlsen > > > > > Guava also has a lot of handy classes for working on collections. > > > > > > 2011/8/18 Simone Tripodi > > > > > > > Salut S=E9bastien, > > > > 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#subL= ist(int > > > > , > > > > int) > > > > > > > > > > > > http://people.apache.org/~simonetripodi/ > > > > http://www.99soft.org/ > > > > > > > > > > > > > > > > On Thu, Aug 18, 2011 at 2:26 PM, S=E9bastien Lorber > > > > 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 al= l > > > these > > > > > entities 100 in a single request (with a "where id in ( 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 idSublist : new > > > > > SublistIterable(allIds,100) ) { > > > > > List vehiclesSublist =3D > > > > 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 > --0015174c0ed4e7764e04aac80d07--