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??