我是Java的新手,我想要执行的功能是从文件加载一系列数据到我的hashSet()函数中。如何按顺序对HashSet()函数数据进行排序?
问题是,我能够按顺序输入所有数据,但无法依据文件中的帐户名称依次检索它。
任何人都可以帮忙吗?
下面是我的代码:
公共设置retrieveHistory(){ 设置dataGroup时=新的HashSet(); 尝试{
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());
}
如果使用hashmap?感谢您的评论 – 2010-03-28 18:34:37
好吧我得到它与...使用linkedHashSet!非常感谢 !! – 2010-03-28 18:37:17