I am trying to sort the Set in descending order but i dont know how can anybody help me?and how can i validate that the original set(before saving) is equal from the one that i just save and load from file?
public class TestSaveNumbers{ //implements Comparator{
public static void main(String args[]) {
Numbers nu = new Numbers(); Numbers nu1 = new Numbers();
Set a = new HashSet(); Set b = new HashSet(); Set c= new HashSet(); int max_range=30; int total=4; Integer number1;
Integer number2; int sequence=1; int quantity; // for (int i=0;i<sequence;i++){ quantity=0;
do{ number1 = new Integer((int)(max_range*Math.random()+1)); number2 = new Integer((int)(max_range*Math.random()+1));
//if (!a.contains(number1)){ a.add(number1); //System.out.println("The Elements of First set:"+a); //String astring= String.valueOf(a); //String.valueOf(astring); //st.addState(astring); b.add(num ber2); //set.add(number1);
quantity++; //}
}while(!(quantity==total));
String astring= String.valueOf(a);
String bstring= String.valueOf(b); //System.out.println("The Elements Sorted"+set); System.out.println("The Elements of First set:"+a); System.out.println("The Elements of Second set:"+b);
c.addAll(b); c.addAll(a); System.out.println("The Union of the two sets:"+c); b.retainAll(a); System.out.println ("The Intersection:"+b);
System.out.println(nu); System.out.println(a); if (a==nu){ System.out.println("They Are equal");} // if( nu.toString()==astring){ ///Why i cant get the true value? ///As you can see i tried differend ways //But still i dont understand why doesnt return //true value? //System.out.println("They Are equal");
} public int compare(Object a, Object b) { String aStr, bStr;
aStr = (String) a; bStr = (String) b;
return bStr.compareTo(aStr); } } ///////////////////////////////////////////// public class Numbers implements Serializable { /** * A constant: The maximum number of Random numbers */ // static public int MAXSETS = 3; static public final int MAXSETS = 3;
/** * The integer variable count will hold the number of states in the subset */ private int count = 0;
/** * The array state will hold the States in the subset of states */
// private int number[] = new int[MAXSETS]; private String number[] = new String[MAXSETS]; /** * Constructor method: Reserve space for the 50 Integers */ public Numbers() { for(int i=0;i < MAXSETS;i++) { number = new String(); // total = 0; // for (int index = 0; index < number.length; index++) { // MAXSETS = MAXSETS + number[index]; }
}
/** * addNumbers: adds a number to the collection of Numbers * a string with the new number is passed as a parameter */ public void addNumbers(String newNumber) { // public void addNumbers(int newNumber) { number[count] = newNumber; count++; // System.out.println("scdsddfs"+ state); } public static int ADD(int a) { return a; }
public int ADD(String a) { Integer Ia = new Integer(a); //Integer Ib = new Integer(b); number[count] = a; count++;
return ADD(Ia.intValue()); }
/** * toString: Convert the object to a string */
public String toString() { // int num; String str = count + " "; for(int i=0;i < count;i++){ // num = Integer.parseInt(str);