aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes collections: why duplicates arent getting eradicated? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "collections: why duplicates arent getting eradicated?" Watch "collections: why duplicates arent getting eradicated?" New topic
Author

collections: why duplicates arent getting eradicated?

maggie karve
Ranch Hand

Joined: Dec 29, 2009
Posts: 187
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14688
    
  16

Please use your post to ask questions, not the title only.

why duplicates arent getting eradicated???

ArrayList allows you to add duplicates. Use a Set instead.


[My Blog]
All roads lead to JavaRanch
maggie karve
Ranch Hand

Joined: Dec 29, 2009
Posts: 187
then whats the use of comparable???
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14688
    
  16

maggie joseph wrote:then whats the use of comparable???

Sorting.
maggie karve
Ranch Hand

Joined: Dec 29, 2009
Posts: 187
thanks.....
Andrew Monkhouse
author and jackaroo
Marshal Commander

Joined: Mar 28, 2003
Posts: 11525
    
100

Perhaps you need to rethink what your application is doing, and what you want it to do.

As Christophe mentions, the Comparable interface allows you to define how you want your objects to be compared, which is usually used when sorting instances of your objects. Your class implements Comparable with the following method:

This basically states that whenever you are comparing two instances of Collection1, you are only interested in what the value is within the c instance variable. Since c is an Integer, and the Integer class implements the Comparable interface, your ArrayList will be sorted according to the natural sort order of Integers.

(A side effect of your using the Integer compareTo method is that you do not really need to override the equals or the hashCode methods. Which is just as well, as there is a bug in your equals method where o can only be null at line 14).


The Sun Certified Java Developer Exam with J2SE 5: paper version from Amazon, PDF from Apress, Online reference: Books 24x7 Personal blog
maggie karve
Ranch Hand

Joined: Dec 29, 2009
Posts: 187
hey does it mean that i should implement comparator or comparable only while using TreeSets and TreeMaps???
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14688
    
  16

maggie joseph wrote:hey does it mean that i should implement comparator or comparable only while using TreeSets and TreeMaps???

No. You can use it with Collections#sort, to sort an Collection class, like an ArrayList.
Andrew Monkhouse
author and jackaroo
Marshal Commander

Joined: Mar 28, 2003
Posts: 11525
    
100

Not necessarily. You can use it anytime you have objects that you want to compare that you want to specify how the comparison works.

You used the example above of having various Collection1 objects inserted into a List, and having them sort by the numerical value of the internal variable 'c' - a perfectly reasonable usage.

In exactly the same way, you might want to have a class that contains a person's name and country (extremely simplified example):

In such a case, you might want to have sort order determined by lastName then firstName, except when the country entered is one of the countries that has family name first. This could give me a sorted list such as:

  • Last, First, Country
  • Abrahams, John, England
  • Shen, Fox, China
  • Johanson, Donald, USA


  • As you can see, I want Mr Fox from China sorted between Mr Abrahams from England and Mr Johanson from USA, even though a "standard" sort based on the name in the "first-name" field does not work.

    Another example might be when you want to compare currencies. Which is the greater sum: $ 0.50 USD, or ¥ 30 JPY ? If you must keep the original currency, then you probably want to have a specialized comparator behind the scenes to handle the comparison since you cannot just compare the numbers directly.
    Andrew Monkhouse
    author and jackaroo
    Marshal Commander

    Joined: Mar 28, 2003
    Posts: 11525
        
    100

    And Christophe has once again posted something that says succinctly what took me considerably longer to say.
    Christophe Verré
    Sheriff

    Joined: Nov 24, 2005
    Posts: 14688
        
      16

    Andrew Monkhouse wrote:And Christophe has once again posted something that says succinctly what took me considerably longer to say.

    You can call this a lack of professionalism, or plain laziness
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: collections: why duplicates arent getting eradicated?