• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

query

 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Why adding drip object the second time gives me exception??Please explain what happens when i add drip object for the first time and when i add drip object the second time
 
Greenhorn
Posts: 9
jQuery Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

gunjan khanuja wrote:

Why adding drip object the second time gives me exception??Please explain what happens when i add drip object for the first time and when i add drip object the second time



In the above code

Set set=new TreeSet();

Set set which is a TreeSet accepts an object that is very true when developer adds an object new Drip() to code, code will not fail. Now when developer will add another object new Drip() the code will fail.

Now replace the code with a fresh code

Set set=new TreeSet();
set.add(new Integer(10)); // replace person object with Integer object
set.add(new Integer(20)); // replace person object with Integer object
System.out.println(set);

The above code will not fail.

Now lets dig in to the concept.

TreeSet is a class belongs to Set interface, being a set it does not accept duplicates. First object insertion goes fine here,Now when we are trying to add the second object then there must be a criteria based on which the 2 object should be comparable. And in the above code there is no such criteria, for uniqueness the set internally use compareable interface to check object equality which is not implemented by the Person class,So in this case developer have to implement the comparable interface in his Person Class.

There are fourteen classes in the Java 2 SDK, version 1.2, that implements the Comparable interface.

BigDecimal, BigInteger, Byte, Double, Float, Integer, Long, Short, Character, CollationKey, Date, File, ObjectStreamField, String

So when developer will try to add the above objects they won’t give any problem, as developer is facing in above scenario.

Below code for Drip class will solve the developer’s problem.

public class Drip implements Comparable{
int i;
Drip(int i){
i = this.i;
}


public int compareTo(Object o1) {
if (this.i == ((Drip) o1).i)
return 0;
else if ((this.i) > ((Drip) o1).i)
return 1;
else
return -1;

}
}

Hope this helps....


 
Ranch Hand
Posts: 66
VI Editor Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

set internally use compareable interface to check object equality

- Seriously? Please check the API if you're not sure about how things work. And please don't give incorrect explanations.

The explanation given by Vanitha is wrong on three things:
1. Set (and any implementation of Set) uses equals() to check the equality (kind of obvious, i'd say, by the term equality). And here is the API: http://docs.oracle.com/javase/7/docs/api/java/util/Set.html#add(E) and not Comparable.
2. And Set.add(E e) never throws any checked exception. it just returns true if the object is successfully added and false if the object is already present.
3. You haven't overridden the equals() and hashcode() method, so, your code should add two different Drip() objects to the set.

However, you're trying to use a TreeSet - which is a sorted set. And in order to sort, you need to implement the Comparable interface. This is what the TreeSet API says:

Note that the ordering maintained by a set (whether or not an explicit comparator is provided) must be consistent with equals if it is to correctly implement the Set interface. (See Comparable or Comparator for a precise definition of consistent with equals.) This is so because the Set interface is defined in terms of the equals operation, but a TreeSet instance performs all key comparisons using its compareTo (or compare) method, so two keys that are deemed equal by this method are, from the standpoint of the set, equal. The behavior of a set is well-defined even if its ordering is inconsistent with equals; it just fails to obey the general contract of the Set interface.

 
Prabaharan Gopalan
Ranch Hand
Posts: 66
VI Editor Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And your code fails not because they're equal but because they're not comparable. And for a TreeSet to sort things, they must be comparable.
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
gunjan khanuja, in the future, please UseAMeaningfulSubjectLine.(⇐click)
 
reply
    Bookmark Topic Watch Topic
  • New Topic