The Artima Developer Community
Sponsored Link

Java Answers Forum
Sort HashSet Descending order

1 reply on 1 page. Most recent reply: Dec 15, 2002 4:39 PM by Singh M.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 1 reply on 1 page
Lena Kokoromiti

Posts: 4
Nickname: mike0303
Registered: Dec, 2002

Sort HashSet Descending order Posted: Dec 14, 2002 2:22 PM
Reply to this message Reply
Advertisement
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?


import java.util.*;
import java.io.*;
import Numbers;


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);



nu.addNumbers(astring);
nu1.addNumbers("Second set:"+bstring);
//nu.ADD(astring);
System.out.println(
"array1.equals( a ) = " + a.equals( nu) );
String fstring= String.valueOf(nu);
try {
// FileDialog dialog = new FileDialog(f, "select file");

String sourceFile="a:\\Numbers.dat";
FileOutputStream foStream = new FileOutputStream(sourceFile);
ObjectOutputStream ooStream = new ObjectOutputStream(foStream);
ooStream.writeObject(nu);
ooStream.writeObject(nu1);

foStream.flush();
foStream.close();
} catch (Exception e) {
System.out.println("Error during output: "+e.toString());

}




try {

FileInputStream fiStream = new FileInputStream("a:\\Numbers.dat");
ObjectInputStream oiStream = new ObjectInputStream(fiStream);
nu = (Numbers) oiStream.readObject();
nu1= (Numbers) oiStream.readObject();





fiStream.close();
} catch (Exception e) {
System.out.println("Error during input: "+e.toString());
}


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);

str = number;
// str = str+" "+number;

}
// System.out.println("scdsddfs"+ str);
return str;
}}


Singh M.

Posts: 154
Nickname: ms
Registered: Mar, 2002

Re: Sort HashSet Descending order Posted: Dec 15, 2002 4:39 PM
Reply to this message Reply
Use TreeSet instead of HashSet.

Flat View: This topic has 1 reply on 1 page
Topic: jcp,jsp,servlets,ejb Previous Topic   Next Topic Topic: ANother  try(interesting prob)

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use