Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

TreeSet only adding 1 element....

Status
Not open for further replies.

asiavoices

Programmer
Sep 20, 2001
90
CA
Hello all,

I have a strange situation where when I try to add an object to a TreeSet collection, its only adding the first (1st) one and ignores the rest. There were no errors, etc. when compiling.

The funny thing is that when I use ArrayList, its okay (but not sorted as expected).


Here's part of my code:


public class MyApp
{
private TreeSet mylist;

public MyApp()
{
......
}


public static void main(String[] args)
{
MyApp myapp = new MyApp();
myapp.test();
}


public void test()
{
// Create my stuff objects
doStuff();
System.out.println("\n");
}

public void doStuff()
{
mylist = new TreeSet();

// add 1st element

Car c1 = new Car();
try
{
c1.setCode("12345678");
}
catch (Exception e){
System.out.println(e.toString());
}
try
{
c1.setName("Chrysler");
}
catch (Exception e)
{
System.out.println(e.toString());
}


// add 2nd element

Car c2 = new Car();
try
{
c2.setCode("fg1951235");
}
catch (Exception e){
System.out.println(e.toString());
}
try
{
c2.setName("Ford");
}
catch (Exception e)
{
System.out.println(e.toString());
}

// add 3rd element

Car c3 = new Car();
try
{
c3.setCode("2458878921");
}
catch (Exception e){
System.out.println(e.toString());
}
try
{
c3.setName("BMW");
}
catch (Exception e)
{
System.out.println(e.toString());
}


mylist.add(c1);
mylist.add(c2);
mylist.add(c3);

System.out.println("\nNo. of elements: " + mylist.size());

System.out.println("\nContents in the SortedSet: " + mylist);

Iterator thelist = mylist.iterator();
while (mylist.hasNext())
{
Car currentcar = (Car) thelist.next();
System.out.println("Car code:" + currentcar.getCode() + "\nName: " + currentcar.getName().trim());
}

} // end of MyApp



Results displayed: ==================

No. of elements: 1

Contents in the SortedSet: [null] <-- this is weird should be something like [Car@12453] or similar.

Car code: 12345678
Name: Chrysler


Any ideas why this may be happening?

Cheers,

Christopher









 
TreeSet will eliminate duplicate objects. Try overriding the &quot;equals&quot; method in your Car class to provide a way of telling one from another based on the Car Code.
 
Thanks idarke,

That's interesting because they are not duplicate (as far as the code and name data is concerned).

Can you suggest how I do an equal test?

Thanks,

Christopher
 
I don't really understand how java determines the &quot;natural order&quot; of objects. I assume it's comparing based on the output from the toString() method.

Another thing to try is to implement the Comparable interface on your Car class and write a compareTo method. This will allow you to directly control the sort order. My understanding is that the compareTo method should consider the same things as &quot;equal&quot; that the equals method does.

Hope this helps...
 
Thank you.

I was able to get it to work now. I forgot that when I implemented the comparable interface, I did not code in the compareTo() method.

cheers,

Christopher

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top