哈希集合和数组列表表演 [英] Hash Set and Array List performances

查看:197
本文介绍了哈希集合和数组列表表演的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我实现了一个方法,它只是循环围绕一组,其中包含了许多不同模块的数据的CSV文件。这就增加了模块名到HashSet的。 ($如下图所示C $ C)

我已经使用HashSet的,因为它保证没有重复插入,而不是这将不得不使用含有()方法,并遍历列表来检查它是否已经存在一个ArrayList

我相信使用哈希集合具有比数组列表有更好的表现。 我是正确的,说明?

此外,可有人向我解释如何,如果用来为每个数据结构中的表现?和bigOh。

 的HashSet<字符串> modulesUploaded =新的HashSet<字符串>();

    对于(文件F:marksheetFiles){
        尝试 {
            csvFileReader =新CSVFileReader(F);
            csvReader = csvFileReader.readFile();
            csvReader.readHeaders();

            而(csvReader.readRecord()){
                字符串MODULENAME = csvReader.get(模块);

                如果(!moduleName.isEmpty()){
                    modulesUploaded.add(模块名);
                }
            }

        }赶上(IOException异常E){
            e.printStackTrace();
        }

        csvReader.close();
    }
    返回modulesUploaded;
}
 

解决方案

<一个href="https://github.com/chaschev/experiments-with-microbes/blob/master/src/main/java/stackoverflow/q6462055/HashMapVsListMicrobe.java#L25">My实验显示,的HashSet 比一个快的ArrayList 开始的3个元素的集合(含)。

一个完整的成绩表

  |升压|集合的大小|
| 2X | 3元|
| 3X | 10元|
| 6X | 50元|
| 12X | 200元| &LT; =比例532-12 VS 10.000-200元素
| 532x | 10.000元| &LT; =呈线性查找成长为ArrayList中
 

I have implemented a method which simply loops around a set of CSV files that contain data on a number of different module. This then adds the 'moduleName' into a hashSet. (Code shown below)

I have used a hashSet as it guarantees no duplicates are inserted instead of an ArrayList which would have to use the contain() method and iterate through the list to check if it is already there.

I believe using the hash set has a better performance than an array list. Am I correct in stating that?

Also, can somebody explain to me how to work the performance for each data structure if used? and the bigOh.

    HashSet<String> modulesUploaded = new HashSet<String>();

    for (File f: marksheetFiles){
        try {
            csvFileReader = new CSVFileReader(f);
            csvReader = csvFileReader.readFile();
            csvReader.readHeaders();

            while(csvReader.readRecord()){
                String moduleName = csvReader.get("Module");

                if (!moduleName.isEmpty()){
                    modulesUploaded.add(moduleName);
                }
            }

        } catch (IOException e) {
            e.printStackTrace();
        }

        csvReader.close();
    }
    return modulesUploaded; 
}

解决方案

My experiment shows that HashSet is faster than an ArrayList starting at collections of 3 elements inclusively.

A complete results table

| Boost  |  Collection Size  |
|  2x    |       3 elements  |
|  3x    |      10 elements  |
|  6x    |      50 elements  |
|  12x   |     200 elements  |  <= proportion 532-12 vs 10.000-200 elements
|  532x  |  10.000 elements  |  <= shows linear lookup growth for the ArrayList

这篇关于哈希集合和数组列表表演的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆