Removing Duplicates not working ... new to java
I inherited this class what I am trying to do is:
1) create a Set of SelectItem (works)
2) query the database for records (works)
3) grab the column from the recordset call attribute2 (works)
4) if the attribute satisfies three condiitions (works)
5) add it to the Set showSet (doesn't work correctly).
What I am seeing is the elements to be added to the Set showSet are coming back as references to memory locations and as each is distinct they all get added to the Set. What I need to do (and can't figure out) is how to insert elements into the Set as values so that there is no duplication. 4 values are returned with one repeat.
Looking forward to your guidance.
Code :
public List<SelectItem> getActiveAttr2Vals(int industrycode, String polymerVal, String techVal, String firstAttrib){
Set<SelectItem> showSet = new HashSet<SelectItem>();
try{
Query qrya2 = em.createQuery("select distinct p from Profile p where p.industryType = ?1 and lower(p.polymer) = ?2 " + "and lower(p.technology) = ?3 and lower(p.attribute1) = ?4 and p.statusFlag = 'Active'");
qrya2.setParameter(1, industrycode);
qrya2.setParameter(2, polymerVal.toLowerCase());
qrya2.setParameter(3, techVal.toLowerCase());
qrya2.setParameter(4, firstAttrib.toLowerCase());
List<Profile> tableList = qrya2.getResultList();
//problematic part
for (Profile profile : tableList) {
SelectItem attr2Item = new SelectItem(profile.getAttribute2());
if(profile.getAttribute2Label() != null && profile.getAttribute2() != null && !profile.getAttribute2().equalsIgnoreCase("")){
showSet.add(attr2Item);
//end problematic part
}else{
System.out.println("Attribute2 is NULL & hence not added...");
}
}
}catch (Exception e) {
e.printStackTrace();
}
return showSet;
}
Re: Removing Duplicates not working ... new to java
Quote:
how to insert elements into the Set as values so that there is no duplication.
By definition a Set only contains unique elements. Where and how are you seeing duplicates?
Can you post what you are seeing that makes you think there are duplicates?
What is the definition of: SelectItem?
What does the equals() method return for two SelectItem objects with the same contents?
Re: Removing Duplicates not working ... new to java
set up two System.out.println statements
System.out.println("attr2Item--->"+attr2Item);
System.out.println("attr2Item--->"+attr2Item.getValue());
right before the showSet.add(attr2Item);
in my cmd console I am seeing:
14:03:46,785 INFO [STDOUT] Attribute 2 tableList size-->4
14:03:46,785 INFO [STDOUT] attr2Item--->javax.faces.model.SelectItem@7a39c6
14:03:46,786 INFO [STDOUT] attr2Item--->Sparkle (Large)
14:03:46,786 INFO [STDOUT] attr2Item--->javax.faces.model.SelectItem@670cfa
14:03:46,820 INFO [STDOUT] attr2Item--->Pearl (Medium)
14:03:46,827 INFO [STDOUT] attr2Item--->javax.faces.model.SelectItem@184f772
14:03:46,829 INFO [STDOUT] attr2Item--->Satin (Small)
14:03:46,831 INFO [STDOUT] attr2Item--->javax.faces.model.SelectItem@917ef9
14:03:46,833 INFO [STDOUT] attr2Item--->Satin (Small)
14:03:46,835 INFO [STDOUT] showSet is: java.util.HashSet of size 4
14:03:46,837 INFO [STDOUT] showList is: java.util.ArrayList of size 4
seems to me that the Set is applying the equals() to the ...@<location>, not the value.
Re: Removing Duplicates not working ... new to java
SelectItem is a component of the web gui, specifically:
java.lang.Object
|
+--javax.faces.model.SelectItem
Re: Removing Duplicates not working ... new to java
Quote:
Set is applying the equals() to the ...@<location>, not the value.
The Set class can't/doesn't look inside of an object.
Which are the duplicates? Your posts don't have any comments like:
<<<<<<<<< First of two
...
<<<<<<<<< Second that is duplicate of First
Quote:
SelectItem is a component of the web gui, specifically:
java.lang.Object
I'm talking about the definition of the class used below:
Code :
SelectItem attr2Item = ...
Where is its definition?
Does the class override the equals() method?
Re: Removing Duplicates not working ... new to java
Quote:
Originally Posted by
Norm
The Set class can't/doesn't look inside of an object.
That's what I thought based on what I am seeing. My quest/question is how to overcome that and pass to the Set showSet values it can apply the equals() method to effective and remove any duplicates.
Re: Removing Duplicates not working ... new to java
Read the API doc for the Set class. It's explained there. SelectItem needs to override equals() and hashcode() if you want to tell the Set class that two objects are "duplicates".
Re: Removing Duplicates not working ... new to java
As I said I am new to java.
So could you explain to me the difference between:
Set<SelectedItem> showSet = new HashSet<SelectItem>();
and
Set showSet = new HashSet();
[testing to see if there really isn't such a thing as a dumb question. :">]
Re: Removing Duplicates not working ... new to java
The first uses generics notation which allows the compiler to check the usage of the object and do casts for you.