Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 9 of 9

Thread: Removing Duplicates not working ... new to java

  1. #1
    Junior Member
    Join Date
    Jan 2013
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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.

    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;
    }
    Last edited by jeichenlaubjr; January 25th, 2013 at 01:42 PM. Reason: add comments


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Removing Duplicates not working ... new to java

    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?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Jan 2013
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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.

  4. #4
    Junior Member
    Join Date
    Jan 2013
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Removing Duplicates not working ... new to java

    SelectItem is a component of the web gui, specifically:
    java.lang.Object
    |
    +--javax.faces.model.SelectItem
    Last edited by jeichenlaubjr; January 25th, 2013 at 02:24 PM. Reason: remove duplicate content

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Removing Duplicates not working ... new to java

    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


    SelectItem is a component of the web gui, specifically:
    java.lang.Object
    I'm talking about the definition of the class used below:
    	SelectItem attr2Item = ...
    Where is its definition?
    Does the class override the equals() method?
    If you don't understand my answer, don't ignore it, ask a question.

  6. #6
    Junior Member
    Join Date
    Jan 2013
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Removing Duplicates not working ... new to java

    Quote Originally Posted by Norm View Post
    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.

  7. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default 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".
    If you don't understand my answer, don't ignore it, ask a question.

  8. #8
    Junior Member
    Join Date
    Jan 2013
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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. ]
    Last edited by jeichenlaubjr; January 25th, 2013 at 02:55 PM. Reason: correct spelling

  9. #9
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default 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.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. merging two unsorted arrays and removing the duplicates..help
    By d4divya2005 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: December 7th, 2012, 03:26 PM
  2. linked list printing empty after removing duplicates
    By mia_tech in forum What's Wrong With My Code?
    Replies: 11
    Last Post: June 11th, 2012, 08:57 AM
  3. Removing duplicates from an Array
    By Rizza in forum Collections and Generics
    Replies: 1
    Last Post: February 21st, 2012, 06:38 PM
  4. Java Not Removing Panels?
    By Pantheon8 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: August 17th, 2011, 07:54 PM
  5. Replies: 4
    Last Post: January 27th, 2009, 12:03 AM

Tags for this Thread