java 如何按顺序对HashSet()函数数据进行排序?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2533993/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 21:38:16  来源:igfitidea点击:

How to sort HashSet() function data in sequence?

javasortinghashset

提问by vincent low

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.

我是 Java 新手,我想要执行的功能是将文件中的一系列数据加载到我的 hashSet() 函数中。

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

    }

回答by zellio

Directly from the HashSetclass javadoc

直接来自HashSet类 javadoc

This class implements the Set interface, backed by a hash table (actually a HashMap instance). It makes no guarantees as to the iteration order of the set; in particular, it does not guarantee that the order will remain constant over time. This class permits the null element.

这个类实现了 Set 接口,由一个哈希表(实际上是一个 HashMap 实例)支持。它不保证集合的迭代顺序;特别是,它不保证订单会随着时间的推移保持不变。此类允许空元素。

If you are using this class there is no way to guarantee order. You will need to introduce another data structure in order to do this. Such as ArrayListor LinkedHashSet.

如果您使用此类,则无法保证顺序。为此,您需要引入另一种数据结构。例如ArrayListLinkedHashSet

回答by Ran Biron

java.util.Set is a unique (element may appear only once), non-ordered, hash-based (objects must fulfill the Object.hashCode() contract) collection.

java.util.Set 是一个唯一的(元素可能只出现一次)、无序的、基于哈希的(对象必须满足 Object.hashCode() 契约)集合。

You probably want an ordered collection. LinkedHashSet is a unique, ordered (by insertion order), hash based collection.

你可能想要一个有序的集合。LinkedHashSet 是一个唯一的、有序的(按插入顺序)、基于散列的集合。