Groups keyboard shortcuts have been updated
Dismiss
See shortcuts

Proper use of Sets.SetView..copyInto()

228 views
Skip to first unread message

fishtoprecords

unread,
Jan 3, 2010, 3:35:17 PM1/3/10
to Google Collections Library - users list
I don't quite follow the Javadocs on this.

It says:

public <S extends Set<E>> S copyInto(S set)
Copies the current contents of this set view into an existing set.

Is the "into set" the argument "set" to the function? or is it the "S"
It almost seems to me that this should be void, what's the return
value?

And more fundamentaly, how do I use it?

Set<T> union = new HashSet<T>();
Sets.union(oneSet, anotherSet).copyInto(union);

Or do I use the returned function?

Ad

unread,
Jan 3, 2010, 7:19:32 PM1/3/10
to Google Collections Library - users list
from the source...

// Note: S should logically extend Set<? super E> but can't due to
either
// some javac bug or some weirdness in the spec, not sure which.
public <S extends Set<E>> S copyInto(S set) {
set.addAll(this);
return set;

fishtoprecords

unread,
Jan 3, 2010, 9:21:45 PM1/3/10
to Google Collections Library - users list
On Jan 3, 7:19 pm, Ad <ada...@gmail.com> wrote:
> from the source...
>       set.addAll(this);
>       return set;
>     }

Interesting, This seems to me to be a strange design choice. I'd
either leave the argument Set unchanged and return a new Set,
or have the function be void and clearly note in the API that it has
direct side effects and won't work with an ImmutableSet.

Since its released, too late to change it now.

Kevin Bourrillion

unread,
Jan 4, 2010, 12:55:05 AM1/4/10
to fishtoprecords, Google Collections Library - users list
On Sun, Jan 3, 2010 at 12:35 PM, fishtoprecords <pat2...@gmail.com> wrote:
I don't quite follow the Javadocs on this.

It says:

public <S extends Set<E>> S copyInto(S set)
Copies the current contents of this set view into an existing set.

Is the "into set" the argument "set" to the function?

Since it says "an existing set", I'm not sure what set that could possibly mean other than the one you pass it as an argument.

Returning the set again is just a convenience, a la StringBuilder.append.

    return Sets.union(a, b).copyInto(new TreeSet<Foo>());


--
Kevin Bourrillion @ Google
internal:  http://go/javalibraries
external: guava-libraries.googlecode.com

Kevin Bourrillion

unread,
Jan 4, 2010, 12:57:29 AM1/4/10
to fishtoprecords, Google Collections Library - users list
On Sun, Jan 3, 2010 at 6:21 PM, fishtoprecords <pat2...@gmail.com> wrote:
On Jan 3, 7:19 pm, Ad <ada...@gmail.com> wrote:
> from the source...
>       set.addAll(this);
>       return set;
>     }

Interesting, This seems to me to be a strange design choice. I'd
either leave the argument Set unchanged and return a new Set,

That defeats the whole point of this API.  We can't choose a Set implementation for you.  If we choose HashSet, but you were using TreeSets, or we choose TreeSet but you were using an identity hash set, all hell will break loose.  That's why this is all designed the way it is:  either just use the *view*, and sidestep the implementation decision that way, or let the user specify exactly what kind of set to copy the results into.


or have the function be void and clearly note in the API that it has
direct side effects and won't work with an ImmutableSet.

We clearly noted in the API that it has the *main* effect of modifying the set that is given.


Pat Farrell

unread,
Jan 4, 2010, 2:54:01 AM1/4/10
to Kevin Bourrillion, Google Collections Library - users list
On Mon, Jan 4, 2010 at 12:57 AM, Kevin Bourrillion <kev...@google.com> wrote:
Interesting, This seems to me to be a strange design choice. I'd
either leave the argument Set unchanged and return a new Set,

That defeats the whole point of this API.  We can't choose a Set implementation for you.  If we choose HashSet, but you were using TreeSets, or we choose TreeSet but you were using an identity hash set, all hell will break loose.  That's why this is all designed the way it is:  either just use the *view*, and sidestep the implementation decision that way, or let the user specify exactly what kind of set to copy the results into.



I'm not following you here. We pass in the argument set. You can look at it and use it. Or is there some reflection problem with that.

The prototype would of course have to be a Set, but other APIs in the collection declare that they return a higher level object when they in fact actually return something lower down the inheritance tree. Perhaps that would cause some cast whining in the compiler.


 
or have the function be void and clearly note in the API that it has
direct side effects and won't work with an ImmutableSet.

We clearly noted in the API that it has the *main* effect of modifying the set that is given.

Well, it wasn't clear to me. If it was clear, I would not have written the question.

I'm not sure that the "convenience" Set return is all that useful. Back in the olden days, one would chain assignments and function calls, but that style seems to have fallen out of favor. If the function was void, even I would have seen it clearly.

More generally, I love the Collections, but it has taken me a fairly long time to understand what the Javadocs say. Perhaps the authors are so familiar with what it is implemented that they can't see how mortal users can get confused. The whole ImmutableFoo.builder stuff was really hard for me to grok.


Dimitris Andreou

unread,
Jan 4, 2010, 6:51:30 AM1/4/10
to Pat Farrell, Kevin Bourrillion, Google Collections Library - users list
Pat,

While I understood what Kevin said immediately, I have trouble
understanding what /you/ are saying. Mentioning of reflection
(java.lang.reflect ?), "prototypes" (?), "other APIs in the
collection", or that returning supertypes of the actual returned
values somehow would cause casts and whining from the compiler. There
seems to be some confusion there. The copyInto method has a very
straightforward javadoc - I'm surprized that "an existing set" could
mean any other than the supplied one, since there is no other set in
sight. Nevermind that the next sentence that is even more direct:
"This method has equivalent behavior to set.addAll(this)".

Dimitris

2010/1/4 Pat Farrell <pat2...@gmail.com>:

> --
> Google Collections Library - users list
> http://groups.google.com/group/google-collections-dev?hl=en
>
> To unsubscribe, send email to:
> google-collections...@googlegroups.com

fishtoprecords

unread,
Jan 4, 2010, 2:30:15 PM1/4/10
to Google Collections Library - users list
On Jan 4, 6:51 am, Dimitris Andreou <jim.andr...@gmail.com> wrote:
> The copyInto method has a very straightforward javadoc

I did not find it straightforward. If I had, I would not have posted
the start of this thread.

It it was declared as void, it would have helped me. Perhaps others
are smarter than I am.

Sam Berlin

unread,
Jan 4, 2010, 2:46:22 PM1/4/10
to fishtoprecords, Google Collections Library - users list
The return value is intended to help with method-chaining (sort of like a fluent-style API).  Similar to how classes like ByteBuffer can let you say 'ByteBuffer b = ByteBuffer.allocate(8).position(4).limit(6);'

Perhaps it would help if the javadocs explicitly explained that the return value is intended to help with method-chaining.

Sam


--

Pat Farrell

unread,
Jan 4, 2010, 4:00:08 PM1/4/10
to Sam Berlin, Google Collections Library - users list
On Mon, Jan 4, 2010 at 2:46 PM, Sam Berlin <sbe...@gmail.com> wrote:
Perhaps it would help if the javadocs explicitly explained that the return value is intended to help with method-chaining.

I think this is a good idea, and it doesn't require any changes to the actual API itself.
 

Kevin Bourrillion

unread,
Jan 4, 2010, 4:25:47 PM1/4/10
to Pat Farrell, Sam Berlin, Google Collections Library - users list
Whoops -- it was a complete and total oversight that we forgot the
@return clause on that method, which might have really helped. It'll
get fixed in Guava.

> --
> Google Collections Library - users list
> http://groups.google.com/group/google-collections-dev?hl=en
>
> To unsubscribe, send email to:
> google-collections...@googlegroups.com

--

Pat Farrell

unread,
Jan 4, 2010, 4:45:06 PM1/4/10
to Kevin Bourrillion, Sam Berlin, Google Collections Library - users list
On Mon, Jan 4, 2010 at 4:25 PM, Kevin Bourrillion <kev...@google.com> wrote:
Whoops -- it was a complete and total oversight that we forgot the
@return clause on that method, which might have really helped.  It'll
get fixed in Guava.

Wonderful. Thanks I know that Collections will be subsummed into Guava. But is there any chance of updates to fix bugs or improve the Javadocs between now and whenever Guava condenses out of the Ether?


Kevin Bourrillion

unread,
Jan 4, 2010, 5:40:13 PM1/4/10
to Pat Farrell, Sam Berlin, Google Collections Library - users list
Guava is pretty much condensing out of the ether this week.

Kevin Bourrillion

unread,
Jan 5, 2010, 12:31:25 AM1/5/10
to Pat Farrell, Sam Berlin, Google Collections Library - users list
On Mon, Jan 4, 2010 at 2:40 PM, Kevin Bourrillion <kev...@google.com> wrote:

Guava is pretty much condensing out of the ether this week.

Good as my word :-) and look what we even have here:


I still need to post binaries and other associated administrivia...

fishtoprecords

unread,
Jan 5, 2010, 3:00:09 AM1/5/10
to Google Collections Library - users list
On Jan 5, 12:31 am, Kevin Bourrillion <kev...@google.com> wrote:
> On Mon, Jan 4, 2010 at 2:40 PM, Kevin Bourrillion <kev...@google.com> wrote:
> Guava is pretty much condensing out of the ether this week.
> Good as my word :-) and look what we even have here:

Wow, that is great. I figured that you would have to wait until all
the press hooplala that is happening Jan 5 at 9AM West coast. or even
after whatever annoucements are coming out at CES. This is very cool.

Reply all
Reply to author
Forward
0 new messages