常要把大量的数据去重,比如从一100万行TXT文本中一行一行的读取数据,把这些数据进行去重,但是这些数据出现的先后顺序不能改变,请问用什么方式效率会最高!

解决方案 »

  1.   

    感谢2楼的老师,我说的100万行是个最大值,但一般情况下都处理的是10万以内的。还有很多情况是才几千行。所以还请问一下用JAVA类处理去重哪个方法比较好。我在百度上问了同样的问题,一个老师说用LinkedHashSet添加的时候如果遇重就会抛出异常,然后再catch里面continue,我记得在学校的时候我们的老师就说最好不要用异常处理机制来进行判断,顺便在这里问一下各位老师,这种方法可取吗?
      

  2.   

    谁说的用LinkedHashSet添加的时候如果遇重就会抛出异常?乱说的,LinkedHashSet也是Set,set的特征就是对重复的元素只保存一个,LinkedHashSet只是在内部使用链表维护元素插入的顺序而已.
    所以说,用LinkedHashSet就行了
      

  3.   

    linkedHashSet就可以了 。 
      

  4.   

    package com.question;import java.io.BufferedReader;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    import java.io.OutputStream;
    import java.util.LinkedHashSet;/**
     * delete the conflict String.
     * 
     * @author Xxx
     */
    public class Q16 {
        
        /**
         * generate the text. 
         * 
         */
        public void init() {
            
            // write file
            OutputStream outputStream = null;
            try {
                outputStream = new FileOutputStream("C:/init.txt");
                for (int i = 0; i < 100000; i++) {
                    for (int j = 0; j < 2; j++) {
                        outputStream.write(("Hello" + i).getBytes());
                        outputStream.write("\r\n".getBytes());
                    }
                }
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                if (outputStream != null) {
                    outputStream = null;
                }
            }
        }
        
        /**
         * filter the string.
         * 
         * @return
         */
        public LinkedHashSet<String> filter() {
            
            // create a LinkedHashSet project.
            LinkedHashSet<String> linkedHashSet = new LinkedHashSet<String>();
            try {
                
                // read the file.
                InputStream inputStream = new FileInputStream("C:/init.txt");
                InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
                BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
                String line = bufferedReader.readLine();
                
                // add the string to the LinkedHashSet
                while(line != null) {
                    linkedHashSet.add(line);
                    line = bufferedReader.readLine();
                }
                
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            // return the result.
            return linkedHashSet;
        }
        
        @Deprecated
        public static void main(String[] args) {
            Q16 q16 = new Q16();
    //        q16.init();
            LinkedHashSet<String> linkedHashSet = q16.filter();
            System.out.println(linkedHashSet.size());
        }
    }
      

  5.   

    LinkedHashSet 没有用过。楼主自己44就该知道了