andrew222
January 31st, 2009, 06:52 PM
I have a question to ask about Java 6. Today I tried to declare a TreeSet with a Generic type as Double as seen below..I was putting primitive double values which are member fields of a class I designed into the TreeSet.
TreeSet<Double> ts = new TreeSet<Double>();
Then after doing so, Eclipse told me that I can't do such an operation in Java 6 and had to change the compliance to work with Java 5.0.
What I can't figure out is why? I made a prototype that puts straight primitive double values into a TreeSet with no error. Now my deductive reasoning tells me that it has a problem using a primitive field of my class.
Does anyoneone know what specific changes were made to the API to make this happen?
Here is the whole program below...
import java.util.*;
class SP
{
String name;
double percentageChange;
SP()
{this.name=null;this.percentageChange=0.0;}
SP (String i_name, double i_percentageChange)
{
this.name = i_name;
this.percentageChange = i_percentageChange;
}
}
public class TreeSetDemo
{
public static void main(String [] args)
{
System.out.println("S&P's Best and Worst Performing Stocks of 2008");
System.out.println("http://seekingalpha.com/article/112981-s-p-s-best-and-worst-of-2008\n\n");
SP lb = new SP("Lehman Brothers", -99.96);
SP wm = new SP("Washington Mutual", -99.84);
SP wr = new SP("Wrigley", 36.58);
SP fd = new SP("Family Dollar", 35.57);
SP fm = new SP("Fannie Mae", -98.10);
SP frm = new SP("Freddie Mac", -97.86);
SP aig = new SP("AIG", -97.31);
SP cc = new SP("Circuit City", -96.90);
SP ab= new SP("Anheuser-Busch", 31.03);
SP ust = new SP("UST", 26.61);
SP am = new SP("Amgen", 24.35);
SP bph = new SP("Barr Pharma", 23.92);
SP ggp = new SP("General Growth Properties", -96.87);
SP ew = new SP("EW Scripps", -95.09);
SP abk = new SP("Ambac Financial", -94.96);
SP bs = new SP("Bear Stearns", -89.43);
SP nc = new SP("National City", -89.00);
SP gf = new SP("Genworth Financial", -88.88);
SP dd = new SP("Developers Diversified",-87.26);
SP [] spArr = {lb, wm, wr, fd,fm,frm, aig,cc,ab,ust,am,bph,ggp,ew,abk,bs,nc,gf,dd};
/*I have Java 6 installed and had to change worspace compliance to Java 5.0 in order to
make the declaration below work*/
TreeSet<Double> ts = new TreeSet<Double>();
System.out.println("Name" + ": " + "Percentage Change" );
System.out.println("-------------------------");
for(int i =0; i < spArr.length; i++)
{
ts.add(spArr[i].percentageChange);
System.out.println(spArr[i].name + ": " + spArr[i].percentageChange + '\n');
}
System.out.println("\nS&P 2008 stocks percentage change values above sorted from least to greatest ");
System.out.println("There are "+ts.size() + " elements in this TreSet");
System.out.println("The TreeSet dectects the lowest element as: " + ts.first());
System.out.println(ts + "\n\n");
System.out.println("Between the range: 20.0--30.0");
System.out.println(ts.subSet(20.00,30.00));
System.out.println("\nBetween the range: 30.0--40.0");
System.out.println(ts.subSet(30.00,40.00));
System.out.println("\nBetween the range: -100.0--(-90.0)");
System.out.println(ts.subSet(-100.00,-90.00));
System.out.println("\nBetween the range: -90.0--(-80.0)");
System.out.println(ts.subSet(-90.00,-80.00));
}
}
Ok, here is the prototype I talked about
import java.util.*;
public class demo {
public static void main(String [] args)
{
double [] d = {11.11,22.22,33.33};
TreeSet<Double> ts = new TreeSet<Double>();
ts.add(d[0]);
}
}
TreeSet<Double> ts = new TreeSet<Double>();
Then after doing so, Eclipse told me that I can't do such an operation in Java 6 and had to change the compliance to work with Java 5.0.
What I can't figure out is why? I made a prototype that puts straight primitive double values into a TreeSet with no error. Now my deductive reasoning tells me that it has a problem using a primitive field of my class.
Does anyoneone know what specific changes were made to the API to make this happen?
Here is the whole program below...
import java.util.*;
class SP
{
String name;
double percentageChange;
SP()
{this.name=null;this.percentageChange=0.0;}
SP (String i_name, double i_percentageChange)
{
this.name = i_name;
this.percentageChange = i_percentageChange;
}
}
public class TreeSetDemo
{
public static void main(String [] args)
{
System.out.println("S&P's Best and Worst Performing Stocks of 2008");
System.out.println("http://seekingalpha.com/article/112981-s-p-s-best-and-worst-of-2008\n\n");
SP lb = new SP("Lehman Brothers", -99.96);
SP wm = new SP("Washington Mutual", -99.84);
SP wr = new SP("Wrigley", 36.58);
SP fd = new SP("Family Dollar", 35.57);
SP fm = new SP("Fannie Mae", -98.10);
SP frm = new SP("Freddie Mac", -97.86);
SP aig = new SP("AIG", -97.31);
SP cc = new SP("Circuit City", -96.90);
SP ab= new SP("Anheuser-Busch", 31.03);
SP ust = new SP("UST", 26.61);
SP am = new SP("Amgen", 24.35);
SP bph = new SP("Barr Pharma", 23.92);
SP ggp = new SP("General Growth Properties", -96.87);
SP ew = new SP("EW Scripps", -95.09);
SP abk = new SP("Ambac Financial", -94.96);
SP bs = new SP("Bear Stearns", -89.43);
SP nc = new SP("National City", -89.00);
SP gf = new SP("Genworth Financial", -88.88);
SP dd = new SP("Developers Diversified",-87.26);
SP [] spArr = {lb, wm, wr, fd,fm,frm, aig,cc,ab,ust,am,bph,ggp,ew,abk,bs,nc,gf,dd};
/*I have Java 6 installed and had to change worspace compliance to Java 5.0 in order to
make the declaration below work*/
TreeSet<Double> ts = new TreeSet<Double>();
System.out.println("Name" + ": " + "Percentage Change" );
System.out.println("-------------------------");
for(int i =0; i < spArr.length; i++)
{
ts.add(spArr[i].percentageChange);
System.out.println(spArr[i].name + ": " + spArr[i].percentageChange + '\n');
}
System.out.println("\nS&P 2008 stocks percentage change values above sorted from least to greatest ");
System.out.println("There are "+ts.size() + " elements in this TreSet");
System.out.println("The TreeSet dectects the lowest element as: " + ts.first());
System.out.println(ts + "\n\n");
System.out.println("Between the range: 20.0--30.0");
System.out.println(ts.subSet(20.00,30.00));
System.out.println("\nBetween the range: 30.0--40.0");
System.out.println(ts.subSet(30.00,40.00));
System.out.println("\nBetween the range: -100.0--(-90.0)");
System.out.println(ts.subSet(-100.00,-90.00));
System.out.println("\nBetween the range: -90.0--(-80.0)");
System.out.println(ts.subSet(-90.00,-80.00));
}
}
Ok, here is the prototype I talked about
import java.util.*;
public class demo {
public static void main(String [] args)
{
double [] d = {11.11,22.22,33.33};
TreeSet<Double> ts = new TreeSet<Double>();
ts.add(d[0]);
}
}