From: Arthi J on 16 Jun 2010 00:39 public class Drink implements Comparable { public String address; public String name; public int compareTo(Object o) { return 1; } } import java.util.*; public class testClass { public static void main(String args[]) { Drink one = new Drink(); Drink two = new Drink(); one.address = "b"; two.address = "a"; one.name= "Coffee"; two.name= "Tea"; TreeSet set = new TreeSet(); set.add(one); set.add(two); Iterator it = set.iterator(); while(it.hasNext()) { Drink temp = (Drink) it.next(); System.out.println(temp.name); System.out.println(temp.address); } } } Output: ----------- Coffee b Tea a ---------- Why is it that sorting occurs only based on 'Name' string here and not on 'Address' string when inserted into TreeSet?
From: markspace on 16 Jun 2010 00:50 Arthi J wrote: > Why is it that sorting occurs only based on 'Name' string here and not > on 'Address' string when inserted into TreeSet? I don't think that's what's occuring at all. > public class Drink implements Comparable { > public String address; > public String name; > > public int compareTo(Object o) { > return 1; > } > } Is this a homework question? Look at this method again and tell me what the sorting is based on.
From: Tom Anderson on 16 Jun 2010 07:13 On Tue, 15 Jun 2010, Arthi J wrote: > public class Drink implements Comparable { > public String address; > public String name; > > public int compareTo(Object o) { > return 1; > } > } > > import java.util.*; > public class testClass > { > public static void main(String args[]) > { > Drink one = new Drink(); > Drink two = new Drink(); > one.address = "b"; > two.address = "a"; > one.name= "Coffee"; > two.name= "Tea"; > > TreeSet set = new TreeSet(); > set.add(one); > set.add(two); > Iterator it = set.iterator(); > while(it.hasNext()) > { > Drink temp = (Drink) it.next(); > System.out.println(temp.name); > System.out.println(temp.address); > } > } > } > > Output: > ----------- > Coffee > b > Tea > a > ---------- > Why is it that sorting occurs only based on 'Name' string here and not > on 'Address' string when inserted into TreeSet? You're kidding, right? tom -- In the long run, we are all dead. -- John Maynard Keynes
From: Lew on 16 Jun 2010 07:16 On 06/16/2010 12:39 AM, Arthi J wrote: > public class Drink implements Comparable { Comparable<what?> > public String address; > public String name; > > public int compareTo(Object o) { > return 1; Where's the "compare" in "compareTo()"? I don't see any comparison. By the way, where's your indentation? > } > } > > import java.util.*; > public class testClass Class names should begin with an upper-case letter. > { > public static void main(String args[]) > { > Drink one = new Drink(); > Drink two = new Drink(); > one.address = "b"; > two.address = "a"; > one.name= "Coffee"; > two.name= "Tea"; > > TreeSet set = new TreeSet(); TreeSet<what?> > set.add(one); > set.add(two); > Iterator it = set.iterator(); Iterator<what?> > while(it.hasNext()) > { > Drink temp = (Drink) it.next(); > System.out.println(temp.name); > System.out.println(temp.address); > } > } > } > > Output: > ----------- > Coffee > b > Tea > a > ---------- > Why is it that sorting occurs only based on 'Name' string here and not > on 'Address' string when inserted into TreeSet? "compareTo()" is supposed to compare to something. ("what?") You forgot your generics. Your indentation is inconsistent and irregular and not standard. -- Lew
From: Daniel Pitts on 17 Jun 2010 16:26 On 6/15/2010 9:39 PM, Arthi J wrote: > Why is it that sorting occurs only based on 'Name' string here and not > on 'Address' string when inserted into TreeSet? Because your teacher is failing at his/her job and your book is out of date. I suggest replacing both as soon as possible. If you can not replace your instructor, at least supplement the book with a recent one explaining Java. -- Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>
|
Next
|
Last
Pages: 1 2 Prev: HTTP Request timeout in appserver Next: How to pass a class as argument? |