0

code

    public class Society  {

    private String address;
    private String name;
    private Integer noOfFlats;
 
    public Society(String address, String name, int noOfFlats) {
        this.address = address;
        this.name = name;
        this.noOfFlats = noOfFlats;
    }
    
public class SocietySet {

  Set<Society> socSet = new HashSet<>();

public void addSocToset(Society society) {
  socSet.add(society);
}

public void printSocSet() {
  for (Society society : socSet) {
   System.out.println("[ "+society.getName()+" ,"+society.getAddress()+"             
   ,"+society.getNoOfFlats()+" ]");
  }
}

Main Method

public static void main(String[] args) {  
      
 SocietySet societySet = new SocietySet(); // initialized object of class
      
  Society society1 = new Society("pune","kamalapark",15); 
  Society society2 = new  Society("pune","kamalapark",15);
  Society society3 = new Society("pune","dsk",50);
    
  societySet.addSocToset(society1);
  societySet.addSocToset(society2);
  societySet.addSocToset(society3);
  societySet.printSocSet();
}
}

its printing same values of first two societies.

 output :
[ kamalapark ,pune ,15 ]
[ kamalapark ,pune ,15 ]
[ dsk ,pune ,50 ]

where it should technically print unique values only, what should be done to stop it from printing common values??

Prasad Kadu
  • 23
  • 1
  • 4

5 Answers5

3

A Set by definition cannot contain two objects with 'equal' values.

Your problem is that your Society class does not have any particular concept of two Society objects being equal.

Society needs to define methods equal() and hashCode().

user13784117
  • 1,124
  • 4
  • 4
0

When you put an object into a Hashset it uses the object's hashcode value to determine where to put the object in the Set. But it also compares the object's hashcode to the hashcode of all the other objects in the Hash Set, and if there's no matching hashcode,the HashSet assumes that this new object is not a duplicate. HashSet finds a matching hashcode for two objects. one you're inserting and one already in the set-the HashSet will then call one of the object's equals() methods to see if these hashcode-matched objects really are equal. And if they're equal, the HashSet knows that the object you're attempting to add is a duplicate of something in the Set, so the add doesn't happen.

Override this equals method to check for equality of object along with hashcode. public boolean equals(Object o)

Adya
  • 1,084
  • 9
  • 17
0

You will need to override equals and hascode method.

@Override
public boolean equals(Object other) {
if(!other instanceof Society) 
    return false;

 Society o = (Society)other;

 return o.address.equals(address) && o.name.equals(name) && o.noOfFlats.equals(noOfFlats)
}

@Override
public int hashCode() 
{  
    // your hascode implementation. 
} 

For detailed explaination: https://www.geeksforgeeks.org/override-equalsobject-hashcode-method/

Nitin Singhal
  • 215
  • 2
  • 11
0

Add the below 2 methods to your Society class.You must first use the equals() and hashcode() methods to verify that the element is unique.

    @Override
    public boolean equals(Object obj) {
            Society otherObj = (Society) obj;
            return this.name.equals(otherObj .name) &&
                     this.noOfFlats.equals(otherObj .noOfFlats) &&
                     this.address.equals(otherObj .address) &&
                    this.hashCode() == otherObj .hashCode();
    }

    @Override
        public int hashCode() {
            return (43 + 777);
    }
0

I believe the root of your issue is not technical, but conceptual.

Your situation turns around the idea of Sets rejecting duplicates and it is true, Sets work exlusively with unique values. But the thing is that applies only to literals, that is single values individually defined by the user (things like "pune","kamalapark" and 15, when added to a set individually, as independent elements).

However, when, as in your code, a Set is made of objects, not literals, then to make it comply with uniqueness you should use hash and equal methods. In this thread we already have answers and comments covering this thing.

Hope my explanation makes your situation clearer.

TomateFraiche
  • 134
  • 1
  • 2
  • 11