Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #30627 > unrolled thread

how do I merg two collections?

Started byBrian O'Brien <brian.obrien@calgaryscientific.com>
First post2015-01-04 22:55 -0800
Last post2015-01-07 13:40 +0000
Articles 10 — 6 participants

Back to article view | Back to comp.lang.java.programmer


Contents

  how do I  merg two collections? Brian O'Brien <brian.obrien@calgaryscientific.com> - 2015-01-04 22:55 -0800
    how do I  merg two collections? Lew <lewbloch@gmail.com> - 2015-01-04 23:17 -0800
    Re: how do I  merg two collections? Eric Sosman <esosman@comcast-dot-net.invalid> - 2015-01-05 08:57 -0500
      Re: how do I  merg two collections? BOB <brian.obrien@calgaryscientific.com> - 2015-01-05 08:38 -0800
        Re: how do I  merg two collections? Eric Sosman <esosman@comcast-dot-net.invalid> - 2015-01-05 11:52 -0500
    Re: how do I  merg two collections? BOB <brian.obrien@calgaryscientific.com> - 2015-01-05 13:26 -0800
      Re: how do I  merg two collections? Jeff Higgins <jeff@invalid.invalid> - 2015-01-05 17:15 -0500
      Re: how do I  merg two collections? Eric Sosman <esosman@comcast-dot-net.invalid> - 2015-01-05 17:35 -0500
        Re: how do I  merg two collections? Lew <lewbloch@gmail.com> - 2015-01-06 15:38 -0800
          Re: how do I  merg two collections? Martin Gregorie <martin@address-in-sig.invalid> - 2015-01-07 13:40 +0000

#30627 — how do I merg two collections?

FromBrian O'Brien <brian.obrien@calgaryscientific.com>
Date2015-01-04 22:55 -0800
Subjecthow do I merg two collections?
Message-ID<e429296f-b008-4a89-82e5-759d26d97edd@googlegroups.com>
I have two collections and I want to create a third Collection that is the union of the two collecions.

//psuedo code
Collection<String> a = "1","a", "2", "b";
Collection<String> b = "3", "a, "4", "b";
Collection<String> c = a + b;
// would produce..
"1", "a" , "2", "b", "3", "4"

Is there a collection method that does this?

Or must I iterate through the one of the sets and add to the other if it does not 'contain' the object?

TIA
B.



-- 

------------------------------
If you believe you have received this electronic transmission in error, 
please notify the original sender of this email and destroy all copies of 
this communication.

[toc] | [next] | [standalone]


#30628

FromLew <lewbloch@gmail.com>
Date2015-01-04 23:17 -0800
Message-ID<70d83104-2556-484a-bf93-2dfe8c48801c@googlegroups.com>
In reply to#30627
'Set#addAll'

[toc] | [prev] | [next] | [standalone]


#30631

FromEric Sosman <esosman@comcast-dot-net.invalid>
Date2015-01-05 08:57 -0500
Message-ID<m8e56s$r3g$1@dont-email.me>
In reply to#30627
On 1/5/2015 1:55 AM, Brian O'Brien wrote:
> I have two collections and I want to create a third Collection that is the union of the two collecions.
>
> //psuedo code

     How do yuo pronuonce "psuedo?"  :)

> Collection<String> a = "1","a", "2", "b";
> Collection<String> b = "3", "a, "4", "b";
> Collection<String> c = a + b;
> // would produce..
> "1", "a" , "2", "b", "3", "4"
>
> Is there a collection method that does this?
>
> Or must I iterate through the one of the sets and add to the other if it does not 'contain' the object?

     If you want c to contain no duplicated elements and you don't
care about their "order," use a Set:

	c = new HashSet<String>(a).addAll(b);

     If you want to keep the a's and/or b's in their original order
(or in any defined order), or if you want to preserve duplicate a's and
duplicate b's (while eliminating b's that duplicate a's), please
describe in more detail what you're trying to accomplish.

-- 
esosman@comcast-dot-net.invalid
"Don't be afraid of work. Make work afraid of you." -- TLM

[toc] | [prev] | [next] | [standalone]


#30632

FromBOB <brian.obrien@calgaryscientific.com>
Date2015-01-05 08:38 -0800
Message-ID<01479315-b953-48e1-a05c-ba926b9386c1@googlegroups.com>
In reply to#30631
On Monday, January 5, 2015 6:57:22 AM UTC-7, Eric Sosman wrote:
> On 1/5/2015 1:55 AM, Brian O'Brien wrote:
> > I have two collections and I want to create a third Collection that is the union of the two collecions.
> >
> > //psuedo code
> 
>      How do yuo pronuonce "psuedo?"  :)
> 
> > Collection<String> a = "1","a", "2", "b";
> > Collection<String> b = "3", "a, "4", "b";
> > Collection<String> c = a + b;
> > // would produce..
> > "1", "a" , "2", "b", "3", "4"
> >
> > Is there a collection method that does this?
> >
> > Or must I iterate through the one of the sets and add to the other if it does not 'contain' the object?
> 
>      If you want c to contain no duplicated elements and you don't
> care about their "order," use a Set:
> 
> 	c = new HashSet<String>(a).addAll(b);
> 
>      If you want to keep the a's and/or b's in their original order
> (or in any defined order), or if you want to preserve duplicate a's and
> duplicate b's (while eliminating b's that duplicate a's), please
> describe in more detail what you're trying to accomplish.
> 
> -- 
> esosman@comcast-dot-net.invalid
> "Don't be afraid of work. Make work afraid of you." -- TLM


Order in not important, what is important is that the Collection NOT contain any duplicates and remains a Collection.


-- 

------------------------------
If you believe you have received this electronic transmission in error, 
please notify the original sender of this email and destroy all copies of 
this communication.

[toc] | [prev] | [next] | [standalone]


#30633

FromEric Sosman <esosman@comcast-dot-net.invalid>
Date2015-01-05 11:52 -0500
Message-ID<m8efgj$c0k$1@dont-email.me>
In reply to#30632
On 1/5/2015 11:38 AM, BOB wrote:
> On Monday, January 5, 2015 6:57:22 AM UTC-7, Eric Sosman wrote:
>> On 1/5/2015 1:55 AM, Brian O'Brien wrote:
>>> I have two collections and I want to create a third Collection that is the union of the two collecions.
>>>
>>> //psuedo code
>>
>>       How do yuo pronuonce "psuedo?"  :)
>>
>>> Collection<String> a = "1","a", "2", "b";
>>> Collection<String> b = "3", "a, "4", "b";
>>> Collection<String> c = a + b;
>>> // would produce..
>>> "1", "a" , "2", "b", "3", "4"
>>>
>>> Is there a collection method that does this?
>>>
>>> Or must I iterate through the one of the sets and add to the other if it does not 'contain' the object?
>>
>>       If you want c to contain no duplicated elements and you don't
>> care about their "order," use a Set:
>>
>> 	c = new HashSet<String>(a).addAll(b);
>>
>>       If you want to keep the a's and/or b's in their original order
>> (or in any defined order), or if you want to preserve duplicate a's and
>> duplicate b's (while eliminating b's that duplicate a's), please
>> describe in more detail what you're trying to accomplish.
>
> Order in not important, what is important is that the Collection NOT contain any duplicates and remains a Collection.

     Okay, use a Set.  I've shown a HashSet, which is probably the
"go to" flavor for general-purpose use, but other kinds of Set are
also available if you want their special capabilities.

-- 
esosman@comcast-dot-net.invalid
"Don't be afraid of work. Make work afraid of you." -- TLM

[toc] | [prev] | [next] | [standalone]


#30634

FromBOB <brian.obrien@calgaryscientific.com>
Date2015-01-05 13:26 -0800
Message-ID<737e254a-22d8-4b8d-b1e0-4ffe1dd35161@googlegroups.com>
In reply to#30627
Thanks.

It's been quite a while since i programmed in Java... 
I'm a bit confused by the differences between a collection and a set.
It looks to me like they are just subclasses of a common baseclass and provide a different interface to it.  

 Collection<String> c = new ArrayList<String>();

This is how I instantiated the collection.. but I assume I could have said 
Set<String> c = new ArrayList<String>();
and then I would be able handle the array of strings as a set rather than a collection

Am I getting this?

-- 

------------------------------
If you believe you have received this electronic transmission in error, 
please notify the original sender of this email and destroy all copies of 
this communication.

[toc] | [prev] | [next] | [standalone]


#30635

FromJeff Higgins <jeff@invalid.invalid>
Date2015-01-05 17:15 -0500
Message-ID<m8f2h3$1k0$1@dont-email.me>
In reply to#30634
<http://docs.oracle.com/javase/8/docs/technotes/guides/collections/index.html>

[toc] | [prev] | [next] | [standalone]


#30636

FromEric Sosman <esosman@comcast-dot-net.invalid>
Date2015-01-05 17:35 -0500
Message-ID<m8f3i2$5qr$1@dont-email.me>
In reply to#30634
On 1/5/2015 4:26 PM, BOB wrote:
> Thanks.
>
> It's been quite a while since i programmed in Java...
> I'm a bit confused by the differences between a collection and a set.
> It looks to me like they are just subclasses of a common baseclass and provide a different interface to it.
>
>   Collection<String> c = new ArrayList<String>();
>
> This is how I instantiated the collection.. but I assume I could have said
> Set<String> c = new ArrayList<String>();
> and then I would be able handle the array of strings as a set rather than a collection
>
> Am I getting this?

     Sort of -- but only sort of, and not completely.

     First, Collection is not a class at all, but an interface.
Remember interfaces?  An interface specifies a bunch of methods, and
every class that "implements" the interface promises to implement all
those methods.  (Interfaces can do a few other things, too, but their
main feature is that they specify methods that their implementing
classes must provide.)  The Collection interface specifies methods like
contains() and iterator() and size(), and every class that wants to be
a Collection must provide all those methods.  ArrayList and Vector and
HashSet and LinkedList and many others implement Collection.

     Second, Set is also an interface.  It specifies methods like
removeAll() and retainAll(), and all classes that implement Set must
provide these methods.  HashSet and EnumSet and TreeSet and various
other classes implement the Set interface.

     Third, Set is a sub-interface of Collection, meaning that every
class that implements Set must also implement everything Collection
requires.  A class cannot be a Set without also being a Collection:
HashSet provides all the methods of Set *and* all those of Collection.
The reverse, however, is not true: Plenty of classes implement
Collection without implementing Set: ArrayList is a Collection but
not a Set, Stack is a Collection but not a Set, and so on.

     Now to your first line of code:

	Collection<String> c = new ArrayList<String>();

This is fine, because the ArrayList class implements the Collection
interface.  But the second line is not so good:

	Set<String> c = new ArrayList<String>();

The compiler will scold you for this.  Why?  Because the ArrayList
class does *not* implement the Set interface; it is a Collection,
but it is not a Set.

     It seems to me that in the "quite a while" since you last used
Java you've forgotten large chunks of what you must have once known.
Perhaps it's time to re-open an old textbook (or a new one) and
start afresh at Page 1, not at Page 100.  This stuff is pretty basic,
and if you've forgotten the basics, well, ...

-- 
esosman@comcast-dot-net.invalid
"Don't be afraid of work. Make work afraid of you." -- TLM

[toc] | [prev] | [next] | [standalone]


#30641

FromLew <lewbloch@gmail.com>
Date2015-01-06 15:38 -0800
Message-ID<1ba2e19d-c727-4307-967f-a68c72e5c1a0@googlegroups.com>
In reply to#30636
Eric Sosman wrote:
> BOB wrote:
> > Thanks.
> >
> > It's been quite a while since i programmed in Java...
> > I'm a bit confused by the differences between a collection and a set.

RTFM.

> > It looks to me like they are just subclasses of a common baseclass and provide a different interface to it.

Nope.

> >
> >   Collection<String> c = new ArrayList<String>();
> >
> > This is how I instantiated the collection.. but I assume I could have said
> > Set<String> c = new ArrayList<String>();

Nope. You cannot assign incompatible types.

An 'ArrayList' is not a 'Set', it's a 'List', as the name implies.

YOU NEED TO KNOW COLLECTIONS TO USE JAVA! Study them.

> > and then I would be able handle the array of strings as a set rather than a collection
> >
> > Am I getting this?

Nope.

>      Sort of -- but only sort of, and not completely.

He's being generous.

>      First, Collection is not a class at all, but an interface.
> Remember interfaces?  An interface specifies a bunch of methods, and
> every class that "implements" the interface promises to implement all
> those methods.  (Interfaces can do a few other things, too, but their
> main feature is that they specify methods that their implementing
> classes must provide.)  The Collection interface specifies methods like
> contains() and iterator() and size(), and every class that wants to be
> a Collection must provide all those methods.  ArrayList and Vector and
> HashSet and LinkedList and many others implement Collection.

Interfaces and classes are fundamental to understanding Java and it's type-based programming. It's quite close to the beginning of the Tutorials:

http://docs.oracle.com/javase/tutorial/java/index.html

>      Second, Set is also an interface.  It specifies methods like
> removeAll() and retainAll(), and all classes that implement Set must
> provide these methods.  HashSet and EnumSet and TreeSet and various
> other classes implement the Set interface.

Note that 'ArrayList' does not implement 'Set'.

>      Third, Set is a sub-interface of Collection, meaning that every
> class that implements Set must also implement everything Collection
> requires.  A class cannot be a Set without also being a Collection:
> HashSet provides all the methods of Set *and* all those of Collection.
> The reverse, however, is not true: Plenty of classes implement
> Collection without implementing Set: ArrayList is a Collection but
> not a Set, Stack is a Collection but not a Set, and so on.
> 
...
> 	Set<String> c = new ArrayList<String>();
> 
> The compiler will scold you for this.  Why?  Because the ArrayList
> class does *not* implement the Set interface; it is a Collection,
> but it is not a Set.
> 
>      It seems to me that in the "quite a while" since you last used
> Java you've forgotten large chunks of what you must have once known.
> Perhaps it's time to re-open an old textbook (or a new one) and
> start afresh at Page 1, not at Page 100.  This stuff is pretty basic,
> and if you've forgotten the basics, well, ...

http://docs.oracle.com/javase/tutorial/java/index.html

-- 
Lew

[toc] | [prev] | [next] | [standalone]


#30646

FromMartin Gregorie <martin@address-in-sig.invalid>
Date2015-01-07 13:40 +0000
Message-ID<m8jd05$hii$1@dont-email.me>
In reply to#30641
On Tue, 06 Jan 2015 15:38:18 -0800, Lew wrote:

> Eric Sosman wrote:
>>      It seems to me that in the "quite a while" since you last used
>> Java you've forgotten large chunks of what you must have once known.
>> Perhaps it's time to re-open an old textbook (or a new one) and start
>> afresh at Page 1, not at Page 100.  This stuff is pretty basic, and if
>> you've forgotten the basics, well, ...
> 
> http://docs.oracle.com/javase/tutorial/java/index.html
>
It strikes me that the OP may not have a copy of the SDK Classes 
documentation.

To the OP: there are two large pieces of documentation that you really 
should have installed locally. One is the SDK documentation set for the 
Java version you have installed and which other posters have been 
referring yo to. The other is the SDK Classes documentation which 
describes the APIs of all the library classes included in the Java 
version you've installed.  It is *NOT* included on the SDK install and so 
must be separately downloaded and installed. Get it. Install it. Become 
familiar with it: among other things, it lists the interfaces implemented 
by each library class, which seems to be info that you don't have access 
to.
 

-- 
martin@   | Martin Gregorie
gregorie. | Essex, UK
org       |

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.java.programmer


csiph-web