• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

collections: why duplicates arent getting eradicated?

 
Ranch Hand
Posts: 187
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
maggie karve
Ranch Hand
Posts: 187
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
then whats the use of comparable???
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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


Sorting.
 
maggie karve
Ranch Hand
Posts: 187
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks.....
 
author and jackaroo
Posts: 12200
280
Mac IntelliJ IDE Firefox Browser Oracle C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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).
 
maggie karve
Ranch Hand
Posts: 187
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hey does it mean that i should implement comparator or comparable only while using TreeSets and TreeMaps???
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 12200
280
Mac IntelliJ IDE Firefox Browser Oracle C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
    Posts: 12200
    280
    Mac IntelliJ IDE Firefox Browser Oracle C++ Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    And Christophe has once again posted something that says succinctly what took me considerably longer to say.
     
    Christophe Verré
    Sheriff
    Posts: 14691
    16
    Eclipse IDE VI Editor Ubuntu
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    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
     
    Don't get me started about those stupid light bulbs.
    reply
      Bookmark Topic Watch Topic
    • New Topic