Page 2 of 2 FirstFirst 12
Results 21 to 35 of 35
  1. #21
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    Notice that using ARrayList will result in the same "duplication": that's the idea. This duplication arises because you are adding a reference to some object to the collection multiple times. So don't do that.

    Whatever collection you use (ArrayList, TreeSet with a proper comparator etc), make sure each thing you add to the collection has been created with "new". Don't reuse instances.

  2. #22
    Join Date
    Apr 2011
    Posts
    18
    Rep Power
    0

    Default

    thanks, that was the bug I was trying to describe, but very poorly on my part.
    I do have another even better work-around thanks to you, i do not have to make that expensive getdata call for each loop, what I do now is clone the element from getdata before I stick in the tree set for sorting. And all is well!

    Thanks you for you time a help.

    {code}
    package treesetexample;

    import java.util.Comparator;
    import java.util.Iterator;
    import java.util.LinkedList;
    import java.util.List;
    import java.util.TreeSet;

    import java.io.Serializable;

    //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
    // Class TreeSetExample
    //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
    /**
    * The TreeSetExample demonstreates a odity in the sorting of list using a treeset
    *
    * @version 1.0, 04/06/2011
    * @author Douglas Nelson
    * @author copyright Oracle 2011
    * @author Unauthorized use, duplication or modification is strictly prohibited.
    */
    public class TreeSetExample extends Object {
    //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
    // start constructor
    //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
    /**
    * Constructs a TreeSetExample object
    */
    public TreeSetExample() {

    } // end constructor
    //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
    // start getData
    //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
    /**
    * getData - return a list of object to use for this example
    */
    public List<SimpleVO> getData() {

    List<SimpleVO> returnList = new LinkedList<SimpleVO>();

    SimpleVO simpleVO = null;

    for (int i = 0;i < 5;i++) {

    returnList.add(new SimpleVO());

    } // end for

    return(returnList);

    } // end getData
    //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
    // start workaround
    //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
    /**
    * workarond - demonstrate the a work around for this issue
    * @param loopCount number of time to load data into the saveset
    */
    public void workaround(int loopCount) {

    List<SimpleVO> simpleList = null;
    SimpleVO simpleVO = null;
    TreeSet<SimpleVO> saveSet = new TreeSet<SimpleVO>(new simpleComparator());
    SimpleVO tempSimpleVO = null;

    Iterator<SimpleVO> iterator = null;
    Iterator<SimpleVO> printIterator = null;

    int id = 0;

    for (int i = 0; i < loopCount;i++) {

    //**************** This is the workaround **************
    //
    // I must get a fresh set of pointers for each time I
    // needed to iterate through the loop plan to insert,
    // This is a problem my real world case getData
    // function access a database in Europe and is really
    // expensive time wise
    //
    //************************************************** ******
    simpleList = getData();

    iterator = simpleList.iterator();

    //---------------------------
    // add each elemet to the set
    //---------------------------
    while (iterator.hasNext()) {

    simpleVO = iterator.next();

    id++;

    simpleVO.setName("" + id);

    saveSet.add(simpleVO);

    } // end while

    //---------------------------------------------
    // print saveset count at the end of each loop
    //---------------------------------------------
    System.out.println("list count after loop [" + (i + 1) + "] number of elements = [" + saveSet.size() + "] contents:");

    } // end For

    //----------------------------------
    // print the contents of the saveSet
    //----------------------------------
    printIterator = saveSet.iterator();

    while(printIterator.hasNext()) {

    tempSimpleVO = printIterator.next();

    System.out.println(" saveSet simpleVO name = [" + tempSimpleVO.getName() + "]");

    } // end while

    } // end workaround
    //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
    // start runTest
    //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
    /**
    * runTest - Test the insert sorting of a tree set with a list of data
    * @param loopCount number of time to load data into the saveset
    */
    public void runTest(int loopCount) {

    List<SimpleVO> simpleList = getData();
    SimpleVO simpleVO = null;
    TreeSet<SimpleVO> saveSet = new TreeSet<SimpleVO>(new simpleComparator());
    SimpleVO tempSimpleVO = null;

    Iterator<SimpleVO> iterator = null;
    Iterator<SimpleVO> printIterator = null;

    int id = 0;

    for (int i = 0; i < loopCount;i++) {

    iterator = simpleList.iterator();

    //-----------------------------------
    // for each element add it to the set
    //-----------------------------------
    while (iterator.hasNext()) {

    simpleVO = iterator.next();

    tempSimpleVO = simpleVO.clone();

    id++;

    tempSimpleVO.setName("" + id);

    saveSet.add(tempSimpleVO);

    } // end while

    //---------------------------------------------
    // print saveset count at the end of each loop
    //---------------------------------------------
    System.out.println("list count after loop [" + (i + 1) + "] number of elements = [" + saveSet.size() + "] contents:");

    } // end For

    //----------------------------------
    // print the contents of the saveSet
    //----------------------------------
    printIterator = saveSet.iterator();

    while(printIterator.hasNext()) {

    tempSimpleVO = printIterator.next();

    System.out.println(" saveSet simpleVO name = [" + tempSimpleVO.getName() + "]");

    } // end while

    } // end runTest
    //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
    // start main
    //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
    /**
    * main thread
    * @param arg standard main input string array.
    */
    public static void main(String[] arg) {

    System.out.println("Main - Started");

    try {

    TreeSetExample treeSetExample = new TreeSetExample();

    treeSetExample.runTest(1);

    System.out.println("\n");
    System.out.println("\n");
    System.out.println("*************** starting test 1 *********************");

    treeSetExample.runTest(2);

    System.out.println("\n");
    System.out.println("*************** starting workaround *********************");

    treeSetExample.workaround(2);


    } // end try
    catch (Exception any) {

    System.out.println("Exception [" + any.getMessage() + "]");

    } // end catch

    System.out.println("Main - we gone bye-bye...!");

    } // end main
    //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
    // Class SimpleVO
    //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
    /**
    * The SimpleVO is a value object pattern used for this example
    *
    * @version 1.0, 04/06/2011
    * @author Douglas Nelson
    * @author copyright Oracle 2011
    * @author Unauthorized use, duplication or modification is strictly prohibited.
    */
    public class SimpleVO extends Object {
    /**
    * Default user name for Oracle connection.
    */
    private String name = "";

    //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
    // start getName
    //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
    /**
    * Returns the name value associated with this attribute
    *
    * @return String - the Name parameter
    */
    public String getName() {

    return(name);

    } // end getName
    //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
    // start setName
    //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
    /**
    * Sets the name value for this attribute
    *
    * @param newName the Name to be set
    */
    public void setName(String newName) {

    if (newName != null) {

    name = newName.trim();

    } // end if

    } // end setName
    //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
    // start clone
    //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
    /**
    * Return an identical SimplpeVO as this with a different pointer location
    *
    * @return SimpleVO - identical copy of this object
    */
    public SimpleVO clone() {

    SimpleVO simpleVO = new SimpleVO();

    simpleVO.setName(this.name);

    return(simpleVO);

    } // end clone

    } // end SimpleVO
    //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
    // Class simpleComparator
    //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
    /**
    * The SimpleComparator is a comparator object used for sorting simple value objects
    *
    * @version 1.0, 04/08/2011
    * @author Douglas Nelson
    * @author copyright Oracle 2011
    * @author Unauthorized use, duplication or modification is strictly prohibited.
    */
    public class simpleComparator implements Comparator<SimpleVO>, Serializable {
    //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
    // start compare
    //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
    /**
    * compare - return the comparison of two objects
    *
    * @param simpleVO_1 - first object to compare
    * @param simpleVO_2 - second object to compare
    * @return int - greater then zero - insert after, less then zero - insert above, equals zero - no insert
    */
    public int compare(SimpleVO simpleVO_1, SimpleVO simpleVO_2) {

    //-------------------------
    // sort first in first out,
    //-------------------------
    return(simpleVO_1.getName().compareTo(simpleVO_2.g etName()));

    } // end compare

    } // end SimpleComparator

    } // end class TreeSetExample
    {code}

  3. #23
    Join Date
    Apr 2011
    Posts
    18
    Rep Power
    0

    Default

    now begs the question, If the SimpleVO implements cloneable, and since this adversely effects the operation of the treeset. Should the TreeSet be changed to use a clone of the object being added to the treeset.

  4. #24
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,006
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by douglas.nelson@oracle.com View Post
    now begs the question, If the SimpleVO implements cloneable, and since this adversely effects the operation of the treeset. Should the TreeSet be changed to use a clone of the object being added to the treeset.
    No, if you want that C++ like behaviour you can always extend the TreeSet class and override the add( ... ) method.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #25
    Join Date
    Apr 2011
    Posts
    18
    Rep Power
    0

    Default

    very cool idea! in this particular case anyway! since it is a one shot only thing, the add method will do the clone and call the super.add(clone)

    Thanks Doug

  6. #26
    Join Date
    Apr 2011
    Posts
    18
    Rep Power
    0

    Default

    butt luck on the cloning inside of a treeset using a generic object, which is pobably why they are unable to do a clone in the treeset originally,

    clone() has protected access in java.lang.Object error message; this link explains why:

    How are java.lang.Object&#39;s protected methods protected from subclasses? - Stack Overflow

    I am going the the Java Law - you must clone your state changed objects before you add them to a Set. Again thanks for your time you have been very helpful.

  7. #27
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,006
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by douglas.nelson@oracle.com View Post
    butt luck on the cloning inside of a treeset using a generic object, which is pobably why they are unable to do a clone in the treeset originally,

    clone() has protected access in java.lang.Object error message; this link explains why:

    How are java.lang.Object's protected methods protected from subclasses? - Stack Overflow

    I am going the the Java Law - you must clone your state changed objects before you add them to a Set. Again thanks for your time you have been very helpful.
    Read the API documentation of the Cloneable interface; it tells you exactly how to implement a public clone() method; it doesn't forbid proper cloning of objects at all.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  8. #28
    Join Date
    Apr 2011
    Posts
    18
    Rep Power
    0

    Default

    Here what I did that lead me to the answer:

    Create a StateChangeTreeSet,
    then on the Add method try to clone the incoming object.

    Thanks Doug

  9. #29
    Join Date
    Apr 2011
    Posts
    18
    Rep Power
    0

    Default

    the API Docs say clone is protected.

    This is the line of code from the source, it never lies:
    {code}
    protected native Object clone() throws CloneNotSupportedException;
    {code}

  10. #30
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,006
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by douglas.nelson@oracle.com View Post
    the API Docs say clone is protected.

    This is the line of code from the source, it never lies:
    {code}
    protected native Object clone() throws CloneNotSupportedException;
    {code}
    Yep, in the Object class the clone() method is proteceted but nothing on earth forbids you to override it with a public version in a subclass (widening access is allowed in a derived class). You also have to implement the (empty) Cloneable interface in the subclass. It is all in the API documentation which never lies. To be honest, I don't understand why you tried to register TreeSet's behaviour as a bug because there isn't any ... IMHO it's just your lack of understanding the API documentation.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  11. #31
    Join Date
    Apr 2011
    Posts
    18
    Rep Power
    0

    Default

    Granted I give you that my understanding of how a treeSet works was skewed. I doubt if the Java gods, are going the make the interface public so a treeset can clone can object us for me. So I consider the matter investigated and closed!

    If you want to share your views on the following I am all ears (err... eyes):

    1) I pass a treeset pointer to object "A"
    2)I then modify the contents of "A" we will call this modified object "A+"
    3)both pointer "A" and "A+" should point to the same data since "A" is not a prime java object and is passed by reference not by value.
    4) I now pass "A+" to the treeset to compare with "A" the two object already in there.
    5) Questions is why does the compare(A != A+) which is what I am seeing with the duplicates in the treeset. It should in my way of thinking (sometimes skewed), the comparator should resolve the compare("A" = "A+") and not insert the equaled object.

    Thanks Doug

    Thanks Doug

  12. #32
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,006
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by douglas.nelson@oracle.com View Post
    5) Questions is why does the compare(A != A+) which is what I am seeing with the duplicates in the treeset. It should in my way of thinking (sometimes skewed), the comparator should resolve the compare("A" = "A+") and not insert the equaled object.
    Because your Comparator say so, i.e. it always returns 1, meaning that an A compared to an A+ returns 1, but also an A+ compared to an A returns 1. Your Comparator is incorrect and it makes the TreeSet insert everything you feed it, no matter whether or not you (not the TreeSet) consider those objects equal.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  13. #33
    Join Date
    Apr 2011
    Posts
    18
    Rep Power
    0

    Default

    I wrote a little ditty to try to reproduce the latest test case about a comparator to show what I was talking about, The results here just highlight my mis-understanding of the treeset and the results are as expected. The compare(A = A+) is always true and it does not do the insert as I thought it did. The results of this test is one object remains in the set looking like the last modified object.

    results are:

    Main - Started
    simpleVO name = [item 2]
    Main - we gone bye-bye...!

    {code}
    import java.util.Comparator;
    import java.util.Iterator;
    import java.util.LinkedList;
    import java.util.List;
    import java.util.TreeSet;

    import java.io.Serializable;

    //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
    // Class ComparatorTest
    //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
    /**
    * The ComparatorTest demonstreates a odity in the sorting of two objects
    *
    * @version 1.0, 04/10/2011
    * @author Douglas Nelson
    * @author copyright Oracle 2011
    * @author Unauthorized use, duplication or modification is strictly prohibited.
    */
    public class ComparatorTest extends Object {
    //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
    // start constructor
    //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
    /**
    * Constructs a ComparatorTest object
    */
    public ComparatorTest() {

    } // end constructor
    //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
    // start runTest
    //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
    /**
    * runTest - Test the insert sorting of a tree set with a modified object
    */
    public void runTest() {

    TreeSet<SimpleVO> treeSet = new TreeSet<SimpleVO>(new SimpleComparator());

    SimpleVO simpleVO = new SimpleVO();

    simpleVO.setName("item 1");

    treeSet.add(simpleVO);

    simpleVO.setName("item 2");

    treeSet.add(simpleVO);

    //----------------------------------
    // print the contents of the saveSet
    //----------------------------------
    for (SimpleVO tempSimpleVO : treeSet) {

    System.out.println("simpleVO name = [" + tempSimpleVO.getName() + "]");

    } // end while

    } // end runTest
    //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
    // start main
    //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
    /**
    * main thread
    * @param arg standard main input string array.
    */
    public static void main(String[] arg) {

    System.out.println("Main - Started");

    try {

    ComparatorTest comparatorTest = new ComparatorTest();

    comparatorTest.runTest();

    } // end try
    catch (Exception any) {

    System.out.println("Exception [" + any.getMessage() + "]");

    } // end catch

    System.out.println("Main - we gone bye-bye...!");

    } // end main
    //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
    // Class SimpleVO
    //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
    /**
    * The SimpleVO is a value object pattern used for this example
    *
    * @version 1.0, 04/06/2011
    * @author Douglas Nelson
    * @author copyright Oracle 2011
    * @author Unauthorized use, duplication or modification is strictly prohibited.
    */
    public class SimpleVO extends Object implements Cloneable {
    /**
    * Default user name for Oracle connection.
    */
    private String name = "";

    //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
    // start getName
    //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
    /**
    * Returns the name value associated with this attribute
    *
    * @return String - the Name parameter
    */
    public String getName() {

    return(name);

    } // end getName
    //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
    // start setName
    //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
    /**
    * Sets the name value for this attribute
    *
    * @param newName the Name to be set
    */
    public void setName(String newName) {

    if (newName != null) {

    name = newName.trim();

    } // end if

    } // end setName
    //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
    // start clone
    //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
    /**
    * Return an identical SimplpeVO as this with a different pointer location
    *
    * @return SimpleVO - identical copy of this object
    */
    public SimpleVO clone() throws CloneNotSupportedException {

    SimpleVO simpleVO_Clone = (SimpleVO) super.clone();

    simpleVO_Clone.setName(this.name);

    return(simpleVO_Clone);

    } // end clone

    } // end SimpleVO
    //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
    // Class simpleComparator
    //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
    /**
    * The SimpleComparator is a comparator object used for sorting simple value objects
    *
    * @version 1.0, 04/08/2011
    * @author Douglas Nelson
    * @author copyright Oracle 2011
    * @author Unauthorized use, duplication or modification is strictly prohibited.
    */
    public class SimpleComparator implements Comparator<SimpleVO>, Serializable {
    //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
    // start compare
    //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
    /**
    * compare - return the comparison of two objects
    *
    * @param simpleVO_1 - first object to compare
    * @param simpleVO_2 - second object to compare
    * @return int - greater then zero - insert after, less then zero - insert above, equals zero - no insert
    */
    public int compare(SimpleVO simpleVO_1, SimpleVO simpleVO_2) {

    //-------------
    // sort by name
    //-------------
    return(simpleVO_1.getName().compareTo(simpleVO_2.g etName()));

    } // end compare

    } // end SimpleComparator

    } // end class ComparatorTest

    {code}

  14. #34
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,006
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by douglas.nelson@oracle.com View Post
    I wrote a little ditty to try to reproduce the latest test case about a comparator to show what I was talking about, The results here just highlight my mis-understanding of the treeset and the results are as expected. The compare(A = A+) is always true and it does not do the insert as I thought it did. The results of this test is one object remains in the set looking like the last modified object.
    I don't see a question; is everything working as you expected now? If not (and I really don't know where this thread is going to), maybe you can tell us in simple words what you have in mind.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  15. #35
    Join Date
    Apr 2011
    Posts
    18
    Rep Power
    0

    Default

    Yeppers, All is well, thanks to the efforts of this group I am better educated in the ways of the Treeset. I have a clear understanding why the Treeset can't clone an object. I understand why I can not sort stateChanged only objects.
    The Treeset is working perfectly (or at least as designed).

    So for all of you how took the time to help me out, I thank you personally and you will be in my prayers this evening.

    Thanks Doug

Page 2 of 2 FirstFirst 12

Similar Threads

  1. TreeSet weirdness
    By Bulska in forum New To Java
    Replies: 3
    Last Post: 03-18-2011, 03:29 PM
  2. Please Help - TreeSet
    By Riftara in forum New To Java
    Replies: 1
    Last Post: 10-21-2010, 08:33 PM
  3. TreeSet Demonstration
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-15-2008, 07:34 PM
  4. ClassCastException in TreeSet
    By pHew in forum New To Java
    Replies: 2
    Last Post: 01-16-2008, 12:20 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •