This page is no longer maintained — Please continue to the home page at www.scala-lang.org

iterating over java.util.Collection instances

2 replies
Ishaaq Chandy
Joined: 2009-02-16,
User offline. Last seen 42 years 45 weeks ago.
Hi all,
What is the simplest way to iterate over a java.util.Collection that I am receiving from a third party? I understand that I can convert it to an array and then work with it but that would be less than ideal as the collection is large and streaming in, I'd rather not pull it all into memory.

I know I can do it with the Iterable and a while loop - but I was wondering if there is a nicer way to do this.

Thanks,
Ishaaq
Dean Wampler
Joined: 2008-12-26,
User offline. Last seen 42 years 45 weeks ago.
Re: iterating over java.util.Collection instances
How is it "streaming in"? If it's coming over some sort of I/O as text, XML, etc, then you could read it into whatever Scala collection you want. If it's coming in binary, like RMI, then you might see if the Scala Streams classes and objects would work.

If you already have a Java collection in memory, you could use an appropriate subclass of scala.collection.jcl.CollectionWrapper, e.g., scala.collection.jcl.HashSet to wrap a java.util.HashSet. Then you get the usual Scala routines, like "foreach", etc.

HTH,
dean

On Mon, Feb 23, 2009 at 4:39 PM, Ishaaq Chandy <ishaaq@gmail.com> wrote:
Hi all,
What is the simplest way to iterate over a java.util.Collection that I am receiving from a third party? I understand that I can convert it to an array and then work with it but that would be less than ideal as the collection is large and streaming in, I'd rather not pull it all into memory.

I know I can do it with the Iterable and a while loop - but I was wondering if there is a nicer way to do this.

Thanks,
Ishaaq



--
Dean Wampler
twitter: @deanwampler, @chicagoscala
http://groups.google.com/group/chicagoscala
http://www.objectmentor.com
http://www.polyglotprogramming.com
http://www.aspectprogramming.com
http://aquarium.rubyforge.org
http://www.contract4j.org
Ishaaq Chandy
Joined: 2009-02-16,
User offline. Last seen 42 years 45 weeks ago.
Re: iterating over java.util.Collection instances
Ok, thanks Dean - I'll look into the scala streams classes

Ishaaq

2009/2/24 Dean Wampler <deanwampler@gmail.com>
How is it "streaming in"? If it's coming over some sort of I/O as text, XML, etc, then you could read it into whatever Scala collection you want. If it's coming in binary, like RMI, then you might see if the Scala Streams classes and objects would work.

If you already have a Java collection in memory, you could use an appropriate subclass of scala.collection.jcl.CollectionWrapper, e.g., scala.collection.jcl.HashSet to wrap a java.util.HashSet. Then you get the usual Scala routines, like "foreach", etc.

HTH,
dean

On Mon, Feb 23, 2009 at 4:39 PM, Ishaaq Chandy <ishaaq@gmail.com> wrote:
Hi all,
What is the simplest way to iterate over a java.util.Collection that I am receiving from a third party? I understand that I can convert it to an array and then work with it but that would be less than ideal as the collection is large and streaming in, I'd rather not pull it all into memory.

I know I can do it with the Iterable and a while loop - but I was wondering if there is a nicer way to do this.

Thanks,
Ishaaq



--
Dean Wampler
twitter: @deanwampler, @chicagoscala
http://groups.google.com/group/chicagoscala
http://www.objectmentor.com
http://www.polyglotprogramming.com
http://www.aspectprogramming.com
http://aquarium.rubyforge.org
http://www.contract4j.org

Copyright © 2012 École Polytechnique Fédérale de Lausanne (EPFL), Lausanne, Switzerland