读txt文件每行是一个手机号码记录在10万行以上判断是否有重复的手机号码ArrayList<String> list = new ArrayList<String>();
String str = in.readLine();
循环一行行往里加,contains()判断list里是否有重复.
如果有重复,跳出循环,提示用户.是否有更高效的方法?请教.

解决方案 »

  1.   

    可以把
    把文件中的每行读出
    set.add();全部加完后,判断set的size和文件的行数
    set的size小于行数,则有重复
      

  2.   

    实在不想再写一遍了。http://topic.csdn.net/u/20081226/16/68ba2e25-7cb9-47c5-86a7-0e9438f4dcd8.html
    看这个帖子3楼的回复,如果能明白位排序的思想的话,lz就知道这个题怎么做了。用这个算法的时间复杂性是o(n)。
    手机号码的前三位是有限的几种可能,可以分类讨论。后面是八位,为一个10万条的数据量申请一个几亿的位空间确实有点浪费(十亿位大约是30M),但是比起用List或者HashSet之类的容器来做,在时间复杂性上还是很有优势的。
      

  3.   

    因为几十万行的数据并不大,完全可以在内存中操作,不用readLine()一行行读.
    你可以运行一下下面程序比较一下(测试数据50万条,第一条和最后一条重复):
    public static void main(String[] args) throws IOException {
    generateInputFile();
    func_slower();
    func_faster();
    }

    public static void func_faster() throws IOException {
    long start = System.currentTimeMillis();
    File file = new File("c:\\numbers.txt");
    HashSet<String> set = new HashSet<String>();
    FileInputStream is = new FileInputStream(file);
    byte[] bytes = new byte[is.available()];
    is.read(bytes);
    String string = new String(bytes);
    int index = 0;
    int length = string.length();
    do{
    String num = string.substring(index, index + 11);
    if (set.contains(num)) {
    System.out.println(num + " duplicate...");
    break;
    } else {
    set.add(num);
    }
    } while((index += 12) < length);
    long end = System.currentTimeMillis();
    System.out.println(end - start);


    }

    public static void func_slower() throws IOException {
    long start = System.currentTimeMillis();
    HashSet<String> set = new HashSet<String>();
    File file = new File("c:\\numbers.txt");
    BufferedReader reader = new BufferedReader(new FileReader(file));
    String line = reader.readLine();
    while(line != null) {
    if (set.contains(line)) {
    System.out.println(line + " duplicate...");
    break;
    } else {
    set.add(line);
    }
    line = reader.readLine();
    }
    long end = System.currentTimeMillis();
    System.out.println(end - start);
    }

    private static void generateInputFile(){

    long n = 13500000000L;
    File file = new File("c:\\numbers.txt");
    BufferedWriter writer = null;
    try {
    writer = new BufferedWriter(new FileWriter(file));
    for (int i = 0; i < 500000; i++) {
    writer.write(String.valueOf(++n)+"\n");
    }
    writer.write(String.valueOf(13500000001L)+"\n");
    } catch (IOException e) {
    e.printStackTrace();
    } finally {
    if (writer != null) {
    try {
    writer.flush();
    writer.close();
    } catch (IOException ignore) {
    }

    }
    }
    }