Forums Register Login

collections: why duplicates arent getting eradicated?

+Pie Number of slices to send: Send
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
then whats the use of comparable???
+Pie Number of slices to send: Send
 

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


Sorting.
+Pie Number of slices to send: Send
thanks.....
+Pie Number of slices to send: Send
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).
+Pie Number of slices to send: Send
hey does it mean that i should implement comparator or comparable only while using TreeSets and TreeMaps???
+Pie Number of slices to send: Send
 

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.
+Pie Number of slices to send: Send
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.
    +Pie Number of slices to send: Send
    And Christophe has once again posted something that says succinctly what took me considerably longer to say.
    +Pie Number of slices to send: Send
     

    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 am a man of mystery. Mostly because of this tiny ad:
    a bit of art, as a gift, the permaculture playing cards
    https://gardener-gift.com


    reply
    reply
    This thread has been viewed 1141 times.
    Similar Threads
    ArrayList
    HashMap problem
    JCheckbox not getting checked in JTable
    Traversing an ArrayList
    I don't understand why my JTable will not display data
    More...

    All times above are in ranch (not your local) time.
    The current ranch time is
    Mar 29, 2024 03:30:44.