RFE Proposal Set Operations - RFC

Greetings, this is a proposal for a Request for Enhancement in the JDK. It

is posted here for the review and comment. Basically an RFC on a RFE. =)

Please indicate your comments on the matter.

The proposal is to have a set of operations for perfomring Non-destructive

mathematical set operations on collections. Additionally, the addition of

set operations to rename the old operations int java.util.Collection

interface to names that are mathematically accurate; so that

removeAll(Collection c) would have an alias of difference(Collection c) and

retainAll(Collection c) would have a alias of intersection(Collection c).

The following methods would be added to the java.util.Collections class.

/**

* Performs the non-descturctive union of two collections.

* The resulting collection is the union of the two collections with no

duplicates.

* @author Robert Simmons Jr.

* @param a The first collection of objects.

* @param b The second collection of objects.

* @return The collection wise union of and b.

*/

publicfinalstatic Collection collectionUnion(final Collection a,final

Collection b){

Collection results =new HashSet(a);

Iterator iter = b.iterator();

while (iter.hasNext())

results.add(iter.next());

return results;

}

/**

* Performs the intersection of two collections.

* The resulting collection is the intersection of the two collections with

no duplicates.

* @author Robert Simmons Jr.

* @param a The first collection of objects.

* @param b The second collection of objects.

* @return The collection wise intersection of a and b.

*/

publicfinalstatic Collection collectionIntersection(final Collection a,

final Collection b){

Collection results =new HashSet();

Iterator iter = a.iterator();

Object element =null;

while (iter.hasNext()){

element = iter.next();

if (b.contains(element)) results.add(element);

}

return results;

}

/**

* Performs the difference of two collections.

* Removes all elements from a that appear in b.

* @author Robert Simmons Jr.

* @param a The first collection of objects.

* @param b The second collection of objects.

* @return The collection wise difference of b elements removed from a.

*/

publicfinalstatic Collection collectionDifference(final Collection a,

final Collection b){

Collection results =new HashSet(a);

Iterator iter = b.iterator();

while (iter.hasNext())

results.remove(iter.next());

return results;

}

/**

* Performs the XOR union of two collections.

* The resulting collection is the union of the two collections with objects

that appear in

* a and b left out.

* @author Robert Simmons Jr.

* @param a The first collection of objects.

* @param b The second collection of objects.

* @return The collection wise union of a and b excluding any objects that

appear in both a and b.

*/

publicfinalstatic Collection collectionXORUnion(final Collection a,final

Collection b){

Collection results =new HashSet(a);

Iterator iter = b.iterator();

Object element =null;

while (iter.hasNext()){

element = iter.next();

if (!(b.contains(element))) results.add(element);

}

return results;

}

/**

* Performs the union of two maps.

* The resulting map is the union of the two maps with no duplicates.

* When working with set operations on a map, the set is assumed to be the

key set. For

* this reason, if both sets have the same key but different values then the

value from

* set b will beused as the value.

* @author Robert Simmons Jr.

* @param a The first map of objects.

* @param b The second map of objects.

* @return The map wise union of and b.

*/

publicfinalstatic Map mapUnion(final Map a,final Map b){

Map results =new HashMap(a);

Iterator iter = b.keySet().iterator();

Object key =null;

while (iter.hasNext()){

key = iter.next();

results.put(key, b.get(key));

}

return results;

}

/**

* Performs the intersection of two maps.

* The resulting map is the intersection of the two maps with no duplicates.

* When working with set operations on a map, the set is assumed to be the

key set. For

* this reason, if both sets have the same key but different values then the

value from

* set b will beused as the value.

* @author Robert Simmons Jr.

* @param a The first map of objects.

* @param b The second map of objects.

* @return The map wise intersection of a and b.

*/

publicfinalstatic Map mapIntersection(final Map a,final Map b){

Map results =new HashMap();

Iterator iter = a.keySet().iterator();

Set bKeys = b.keySet();

Object key =null;

while (iter.hasNext()){

key = iter.next();

if (bKeys.contains(key)) results.put(key, b.get(key));

}

return results;

}

/**

* Performs the difference of two maps.

* Removes all elements from a that appear in b.

* In this case, the set is determined to be the set of keys. The set of

keys from

* b will be used to remove keys from a.

* @author Robert Simmons Jr.

* @param a The first map of objects.

* @param b The second map of objects.

* @return The map wise difference of b elements removed from a.

*/

publicfinalstatic Map mapDifference(final Map a,final Map b){

Map results =new HashMap(a);

Iterator iter = b.keySet().iterator();

while (iter.hasNext())

results.remove(iter.next());

return results;

}

/**

* Performs the XOR union of two maps.

* The resulting map is the union of the two maps with objects that appear

in

* a and b left out.

* When working with set operations on a map, the set is assumed to be the

key set. For

* this reason, if both sets have the same key but different values then the

value from

* set b will beused as the value.

* @author Robert Simmons Jr.

* @param a The first map of objects.

* @param b The second map of objects.

* @return The map wise union of a and b excluding any objects that appear

in both a and b.

*/

publicfinalstatic Map mapXORUnion(final Map a,final Map b){

Map results =new HashMap();

Iterator iter = a.keySet().iterator();

Set bKeys = b.keySet();

Object key =null;

while (iter.hasNext()){

key = iter.next();

if (!(bKeys.contains(key))) results.put(key, b.get(key));

}

return results;

}

Transcript of mail session with Sun java developer after bug submission:

Hi Robert Simmons Jr.,

Thank you for the feedback.

The method names in java.util.Set conform to the parent interface,

java.util.Collection. As for the destruction situation, returning a new set

also would involve creating and populating a new set.

If you would like to pursue this Request for Feature Enhancement (RFE)

further,

I would suggest that you first post this idea to a newsgroup and request

comment from other Java developers. The issues that we have discussed are a

good starting point.

You can view a list of Java newsgroups at:

http://java.sun.com/aboutJava/newsgroups.html

Based on the responses, please feel free to submit a new RFE containing a

link to the newsgroup. Thank you for your time.

Regards,

Jonathan

"Simmons, Robert" wrote:

>

> I understand your reply now. However I put it to you that the names of

these

> operations are HORRIBLY named. They should be named according to the

> mathematical operations. Further, they are destructive to the set being

> checked and I dont want that. The set operations should give me back a set

> and not destroy the current set. If they destroy the current set then I

have

> to spend time copying the thing before I run the set operation which is

> truly not performant. Do an intersection of 400,000 elements with another

fo

> 300,000 elements and you have to first copy one and then the retain all

> method has to iterate through the entire set. This is very bad logic. In

the

> code I gave you, you have to only iterate through the set and not copy it.

>

-- Original Bug Report-

category : java

release : 1.4

subcategory : classes_util

type : rfe

synopsis : java.util.Collections Class Should Implement Logical Set

Operations

description : FULL PRODUCT VERSION :

This issue is pertinent to all java versions including the new 1.4 version.

FULL OPERATING SYSTEM VERSION : Occurs in all operating

Systems.

ADDITIONAL OPERATING SYSTEMS : NA

A DESCRIPTION OF THE PROBLEM :

The java.util.Collections class is a class that is used to

manipulate sets and change them into various forms. Missing

from this is a feature that would define set operations,

such as Union, Intersection, and so on on the sets. This

would be extremely beneficial. I have written an extension

to this class that does just that and would like to submit

it for donation. All I ask is that the javadoc tag naming

me as author be retained.

This bug can be reproduced always.

- BEGIN SOURCE -

<!-- snip source .. see above -->

- END SOURCE -

CUSTOMER WORKAROUND :

Writing your own classes to do this, however it is so basic

that it should be in there.

workaround :

suggested_val :

cust_name : Robert Simmons Jr.

cust_email : robert.simmons@ingenium-ag.com

jdcid : Derisor (old one was gnuish)

keyword : webbug

company : Ingeniums Pharmaceuticals AG

hardware : x86

OSversion : Linux

bugtraqID : 0

dateCreated : 2002-03-11 13:31:18.3

dateEvaluated : 2002-04-11 18:08:17.247

--

Robert Simmons Jr.

Senior Software Engineer

Ingenium Pharmaceuticals, Munich Germany.

-

--

Robert Simmons Jr.

Senior Software Engineer

Ingenium Pharmaceuticals, Munich Germany.

-

--

Robert Simmons Jr.

Senior Software Engineer

Ingenium Pharmaceuticals, Munich Germany.

-

[14424 byte] By [Derisor] at [2007-9-27 2:55:20]
# 1
Bump for prime time americans.
Derisor at 2007-7-4 23:25:16 > top of Java-index,Archived Forums,Java Programming...
# 2
Additional Bump. Seeking comments.
Derisor at 2007-7-4 23:25:16 > top of Java-index,Archived Forums,Java Programming...