Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / August 2007

Tip: Looking for answers? Try searching our database.

Copying collection without duplicates

Thread view: 
Karsten Wutzke - 10 Aug 2007 15:30 GMT
Hello!

I have the following method overriding Collection.addAll:

@Override
public boolean addAll(int index, Collection<? extends E> cln)
{
    if ( containsAll(cln) )
    {
        return false;
    }

    //build list without dupes (always)
    ArrayList<E> al = new ArrayList<E>(cln.size());

    Iterator<? extends E> itr = cln.iterator();

    while ( itr.hasNext() )
    {
        E elem = itr.next();

        if ( !contains(elem) )
        {
            al.add(elem);
        }
    }

    cln = al;

    //allows dupes and nulls
    return super.addAll(index, cln);
}

Is there any faster way without overriding other methods?

Karsten
Roedy Green - 10 Aug 2007 17:37 GMT
>Is there any faster way without overriding other methods?

To dedup, sort and then compare elt with prev, in one pass. You can
then use Iterator.remove or copy non-dups to a new List.
see http://mindprod.com/products2.html#SORTED
for sample code.
Signature

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com

Danno - 10 Aug 2007 23:29 GMT
> Hello!
>
[quoted text clipped - 33 lines]
>
> Karsten

Yep!

Set<?> uniqueCollection = new TreeSet(collection);
Karsten Wutzke - 11 Aug 2007 19:21 GMT
> > Hello!
>
[quoted text clipped - 37 lines]
>
> Set<?> uniqueCollection = new TreeSet(collection);

Hmm how does this skip duplicates?

Karsten
Patricia Shanahan - 11 Aug 2007 20:01 GMT
>>> Hello!
>>> I have the following method overriding Collection.addAll:
[quoted text clipped - 27 lines]
>
> Hmm how does this skip duplicates?

A set contains no duplicates, so if the collection were the list "A",
"B", "A" the treeset would contain "A", "B". However, the TreeSet
iterator is in compareTo order, not the List order.

If the collection is too large for linear scanning, I would implement
the no-duplicates list using two data structures, a HashSet for
determining which elements are eligible for adding, and a List to
preserve order.

Patricia
Daniel Dyer - 11 Aug 2007 20:14 GMT
>>>> Is there any faster way without overriding other methods?
>>>> Karsten
[quoted text clipped - 6 lines]
> "B", "A" the treeset would contain "A", "B". However, the TreeSet
> iterator is in compareTo order, not the List order.

A LinkedHashSet may be preferable if preserving the order is important:

http://java.sun.com/j2se/1.5.0/docs/api/java/util/LinkedHashSet.html

Dan.

Signature

Daniel Dyer
http//www.uncommons.org

Patricia Shanahan - 11 Aug 2007 21:12 GMT
>>>>> Is there any faster way without overriding other methods?
>>>>> Karsten
[quoted text clipped - 10 lines]
>
> http://java.sun.com/j2se/1.5.0/docs/api/java/util/LinkedHashSet.html

I don't think LinkedHashSet has the ability to addAll at a specified
index. The following is a quote from the original article:

> I have the following method overriding Collection.addAll:
>
> @Override
> public boolean addAll(int index, Collection<? extends E> cln)
> {

Collection does not have a two argument addAll. List does, so I suspect
the new class is supposed to implement List.

Patricia
Danno - 13 Aug 2007 00:06 GMT
> >>>>> Is there any faster way without overriding other methods?
> >>>>> Karsten
[quoted text clipped - 24 lines]
>
> Patricia

Very nice Patricia,
I also found this on commons-collections
http://commons.apache.org/collections/api-release/org/apache/commons/collections
/set/ListOrderedSet.html



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.