How to sort HashSet() function data in sequence?
Posted
by vincent low
on Stack Overflow
See other posts from Stack Overflow
or by vincent low
Published on 2010-03-28T18:24:13Z
Indexed on
2010/03/28
18:43 UTC
Read the original article
Hit count: 296
I am new to Java, the function I would like to perform is to load a series of data from a file, into my hashSet() function.
the problem is, I able to enter all the data in sequence, but I can't retrieve it out in sequence base on the account name in the file.
Can anyone help?
below is my code:
public Set retrieveHistory(){ Set dataGroup = new HashSet(); try{
File file = new File("C:\\Documents and Settings\\vincent\\My Documents\\NetBeansProjects\\vincenttesting\\src\\vincenttesting\\vincenthistory.txt");
BufferedReader br = new BufferedReader(new FileReader(file));
String data = br.readLine();
while(data != null){
System.out.println("This is all the record:"+data);
Customer cust = new Customer();
//break the data based on the ,
String array[] = data.split(",");
cust.setCustomerName(array[0]);
cust.setpassword(array[1]);
cust.setlocation(array[2]);
cust.setday(array[3]);
cust.setmonth(array[4]);
cust.setyear(array[5]);
cust.setAmount(Double.parseDouble(array[6]));
cust.settransaction(Double.parseDouble(array[7]));
dataGroup.add(cust);
//then proced to read next customer.
data = br.readLine();
}
br.close();
}catch(Exception e){
System.out.println("error" +e);
}
return dataGroup;
}
public static void main(String[] args) {
FileReadDataModel fr = new FileReadDataModel();
Set customerGroup = fr.retrieveHistory();
System.out.println(e);
for(Object obj : customerGroup){
Customer cust = (Customer)obj;
System.out.println("Cust name :" +cust.getCustomerName());
System.out.println("Cust amount :" +cust.getAmount());
}
© Stack Overflow or respective owner